数据结构支持设置默认值能力
This commit is contained in:
		
							
								
								
									
										18
									
								
								define.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								define.go
									
									
									
									
									
								
							| @ -14,23 +14,23 @@ import ( | ||||
| const ( | ||||
| 	TagErrMsg       = "err"       // json结构体中, 错误信息 tag 字段 | ||||
| 	TagValidate     = "validator" // json结构体中, 验证规则 rule 字段 | ||||
| 	TagDefaultValue = "d"         // json结构体中默认值标签 | ||||
| 	TagDefaultValue = "default"   // json结构体中默认值标签 | ||||
| ) | ||||
|  | ||||
| // Rule 规则定义 | ||||
| type Rule struct { | ||||
| 	Tag  string   `json:"tag" dc:"规则标签,如:required, email, phone, etc."` // 规则标签,如:required, email, phone, etc. | ||||
| 	Args []string `json:"args" dc:"规则参数"`                                // 规则参数,如: Tag=min, Args=[1], 则会转化成验证规则 min=1 | ||||
| 	Args []string `json:"args" dc:"规则参数"`                               // 规则参数,如: Tag=min, Args=[1], 则会转化成验证规则 min=1 | ||||
| } | ||||
|  | ||||
| // StructField 结构体字段定义 | ||||
| type StructField struct { | ||||
| 	JsonTag      string          `json:"json_tag" dc:"字段对外输出的json标签"`     // 结构体字段名称, 不单独设置, 从 TargetPath 解析 | ||||
| 	Type         consts.DataType `json:"type" dc:"字段的数据类型"`                 // 字段的数据类型 | ||||
| 	Required     bool            `json:"required" dc:"是否必传"`                   // 对应验证规则的 required 属性, 在非必传的情况下, 字段不存在, 不会进行规则验证, 字段存在, 才会进行验证, Required == false 时, 会在 RuleList 检测是否有required 规则 | ||||
| 	RuleList     []Rule          `json:"rule_list" dc:"字段规则列表"`              // 字段规则列表 | ||||
| 	DefaultValue string          `json:"default_value" dc:"字段默认值"`            // 字段默认值, 仅对非必传字段生效, 统一用字符串类型, 后面会转换成 FieldType 类型 | ||||
| 	SourcePath   string          `json:"source_path" dc:"读取数据的数据源路径"`    // 读取数据的数据源路径 | ||||
| 	TargetPath   string          `json:"target_path" dc:"数据设置哪一个路径"`      // 目标数据路径 | ||||
| 	JsonTag      string          `json:"json_tag" dc:"字段对外输出的json标签"` // 结构体字段名称, 不单独设置, 从 TargetPath 解析 | ||||
| 	Type         consts.DataType `json:"type" dc:"字段的数据类型"`           // 字段的数据类型 | ||||
| 	Required     bool            `json:"required" dc:"是否必传"`          // 对应验证规则的 required 属性, 在非必传的情况下, 字段不存在, 不会进行规则验证, 字段存在, 才会进行验证, Required == false 时, 会在 RuleList 检测是否有required 规则 | ||||
| 	RuleList     []Rule          `json:"rule_list" dc:"字段规则列表"`       // 字段规则列表 | ||||
| 	DefaultValue string          `json:"default_value" dc:"字段默认值"`    // 字段默认值, 仅对非必传字段生效, 统一用字符串类型, 后面会转换成 FieldType 类型 | ||||
| 	SourcePath   string          `json:"source_path" dc:"读取数据的数据源路径"` // 读取数据的数据源路径 | ||||
| 	TargetPath   string          `json:"target_path" dc:"数据设置哪一个路径"`  // 目标数据路径 | ||||
| 	Errmsg       string          `json:"errmsg" dc:"规则验证不通过时, 报错的信息"` // 规则验证不通过时, 报错的信息 | ||||
| } | ||||
|  | ||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							| @ -7,6 +7,7 @@ require ( | ||||
| 	git.zhangdeman.cn/zhangdeman/dynamic-struct v0.0.0-20250319072714-eab2a7abde63 | ||||
| 	git.zhangdeman.cn/zhangdeman/json_filter v0.0.0-20250321103029-786c03293a28 | ||||
| 	git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd | ||||
| 	github.com/creasty/defaults v1.8.0 | ||||
| 	github.com/go-playground/validator/v10 v10.25.0 | ||||
| 	github.com/tidwall/gjson v1.18.0 | ||||
| 	github.com/tidwall/sjson v1.2.5 | ||||
|  | ||||
							
								
								
									
										2
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.sum
									
									
									
									
									
								
							| @ -22,6 +22,8 @@ github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg | ||||
| github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= | ||||
| github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ= | ||||
| github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg= | ||||
| github.com/creasty/defaults v1.8.0 h1:z27FJxCAa0JKt3utc0sCImAEb+spPucmKoOdLHvHYKk= | ||||
| github.com/creasty/defaults v1.8.0/go.mod h1:iGzKe6pbEHnpMPtfDXZEr0NVxWnPTjb1bbDy08fPzYM= | ||||
| github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||||
| github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||||
| github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= | ||||
|  | ||||
| @ -15,6 +15,7 @@ import ( | ||||
| 	dynamicStructGenerate "git.zhangdeman.cn/zhangdeman/dynamic-struct" | ||||
| 	"git.zhangdeman.cn/zhangdeman/json_filter/gjson_hack" | ||||
| 	"git.zhangdeman.cn/zhangdeman/serialize" | ||||
| 	"github.com/creasty/defaults" | ||||
| 	"github.com/go-playground/validator/v10" | ||||
| 	"github.com/tidwall/gjson" | ||||
| 	"github.com/tidwall/sjson" | ||||
| @ -101,6 +102,10 @@ func (h *handle) Run() ([]byte, error) { | ||||
| 	if err := serialize.JSON.UnmarshalWithNumber([]byte(h.formatVal), &val); nil != err { | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	if err := defaults.Set(&val); nil != err { | ||||
| 		// 默认值设置失败 | ||||
| 		return nil, err | ||||
| 	} | ||||
| 	if err := validatorInstance.Struct(val); nil != err { | ||||
| 		return nil, GetValidateErr(val, err, TagErrMsg) | ||||
| 	} | ||||
| @ -231,6 +236,9 @@ func (h *handle) generateTag(field StructField) string { | ||||
| 	// 验证规则tag | ||||
| 	tagList = append(tagList, fmt.Sprintf(`%s:"%s"`, TagValidate, strings.Join(validateRuleList, ","))) | ||||
| 	// 默认值 | ||||
| 	if field.DefaultValue == "-" && (field.Type == consts.DataTypeString || field.Type == consts.DataTypeStringPtr) { | ||||
| 		field.DefaultValue = "" | ||||
| 	} | ||||
| 	tagList = append(tagList, fmt.Sprintf(`%s:"%s"`, TagDefaultValue, field.DefaultValue)) | ||||
| 	return strings.Join(tagList, " ") | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user