From 3814638e1cb52ed8ec44e30b75f3615968aa103b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 31 Mar 2025 16:38:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=85=88format,=20?= =?UTF-8?q?=E5=9C=A8=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- validate.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/validate.go b/validate.go index 2a63152..ac4e8e0 100644 --- a/validate.go +++ b/validate.go @@ -20,6 +20,7 @@ import ( "github.com/tidwall/gjson" "github.com/tidwall/sjson" "strings" + jsonFilter "git.zhangdeman.cn/zhangdeman/json_filter" ) var validatorInstance *validator.Validate @@ -35,8 +36,23 @@ func init() { // // Date : 15:12 2025/3/18 func Run(sourceData []byte, fieldList []StructField) ([]byte, error) { + mapRuleList := make([]jsonFilter.MapRule, 0) + for _, itemParam := range fieldList { + mapRuleList = append(mapRuleList, jsonFilter.MapRule{ + SourcePath: itemParam.SourcePath, + TargetPath: itemParam.TargetPath, + Required: false, + DataType: itemParam.Type, + DefaultValue: itemParam.DefaultValue, + }) + } + filterInstance := jsonFilter.NewFilter(string(sourceData), mapRuleList) + if err := filterInstance.Deal(); nil != err { + return nil, err + } + formatByteData := filterInstance.Byte() handleInstance := &handle{ - sourceData: sourceData, + sourceData: formatByteData, fieldList: fieldList, parentFieldTable: map[string]bool{}, } @@ -70,6 +86,9 @@ type handle struct { // Run 执行验证 func (h *handle) Run() ([]byte, error) { + if len(h.fieldList) == 0 { + return []byte("{}"), nil + } for _, field := range h.fieldList { if len(field.Errmsg) == 0 { field.Errmsg = field.JsonTag + " : 参数校验不通过" From b9b1cba508d0e25a301bb6f2a5ecf9b2b6ec2f02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 31 Mar 2025 17:15:45 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dvalidate=20path=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- validate.go | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/validate.go b/validate.go index ac4e8e0..667dde8 100644 --- a/validate.go +++ b/validate.go @@ -20,7 +20,6 @@ import ( "github.com/tidwall/gjson" "github.com/tidwall/sjson" "strings" - jsonFilter "git.zhangdeman.cn/zhangdeman/json_filter" ) var validatorInstance *validator.Validate @@ -36,23 +35,8 @@ func init() { // // Date : 15:12 2025/3/18 func Run(sourceData []byte, fieldList []StructField) ([]byte, error) { - mapRuleList := make([]jsonFilter.MapRule, 0) - for _, itemParam := range fieldList { - mapRuleList = append(mapRuleList, jsonFilter.MapRule{ - SourcePath: itemParam.SourcePath, - TargetPath: itemParam.TargetPath, - Required: false, - DataType: itemParam.Type, - DefaultValue: itemParam.DefaultValue, - }) - } - filterInstance := jsonFilter.NewFilter(string(sourceData), mapRuleList) - if err := filterInstance.Deal(); nil != err { - return nil, err - } - formatByteData := filterInstance.Byte() handleInstance := &handle{ - sourceData: formatByteData, + sourceData: sourceData, fieldList: fieldList, parentFieldTable: map[string]bool{}, } @@ -118,7 +102,11 @@ func (h *handle) Run() ([]byte, error) { // 支持嵌套结构体 fieldTag := h.generateTag(field) // 这里需要设置为对应类型的零值就行, 此处传入值的目的只是为了确认数据类型 - h.dynamicStruct.AddField(field.JsonTag, "", consts.GetDataTypeDefaultValue(field.Type), fieldTag, false) + path := field.TargetPath + if path == "" { + path = field.JsonTag + } + h.dynamicStruct.AddField(path, "", consts.GetDataTypeDefaultValue(field.Type), fieldTag, false) } val := h.dynamicStruct.Build().New() if err := serialize.JSON.UnmarshalWithNumber([]byte(h.formatVal), &val); nil != err {