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] =?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 {