From 1fd13e1ce7d4c42b1f8d83a38afc32b044655f8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 25 Sep 2024 17:17:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- run.go | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/run.go b/run.go index 1e8f0c5..44d3bcd 100644 --- a/run.go +++ b/run.go @@ -23,9 +23,9 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 14:18 2024/4/29 -func RunForStruct(sourceData any, ruleList []*define.FieldRule) (map[string]any, error) { +func RunForStruct(sourceData any, ruleList []*define.FieldRule, receiver any) error { if nil == sourceData { - return map[string]any{}, nil + return nil } byteData, _ := json.Marshal(sourceData) var ( @@ -36,12 +36,9 @@ func RunForStruct(sourceData any, ruleList []*define.FieldRule) (map[string]any, d := json.NewDecoder(bytes.NewReader(byteData)) d.UseNumber() if err = d.Decode(&sourceMapData); nil != err { - return nil, err + return err } - if err = Run(sourceMapData, ruleList); nil != err { - return nil, err - } - return sourceMapData, nil + return Run(sourceMapData, ruleList, receiver) } // Run 运行参数验证 @@ -49,7 +46,7 @@ func RunForStruct(sourceData any, ruleList []*define.FieldRule) (map[string]any, // Author : go_developer@163.com<白茶清欢> // // Date : 14:13 2024/4/29 -func Run(sourceData map[string]any, ruleList []*define.FieldRule) error { +func Run(sourceData map[string]any, ruleList []*define.FieldRule, receiver any) error { for _, itemRule := range ruleList { itemRule.DefaultValue = nil } @@ -57,11 +54,12 @@ func Run(sourceData map[string]any, ruleList []*define.FieldRule) error { if nil != err { return err } - sourceData = make(map[string]any) - d := json.NewDecoder(bytes.NewReader(byteData)) - d.UseNumber() - if err := d.Decode(&sourceData); nil != err { - return err + if nil != receiver { + d := json.NewDecoder(bytes.NewReader(byteData)) + d.UseNumber() + if err := d.Decode(&receiver); nil != err { + return err + } } return nil } @@ -76,6 +74,7 @@ func RunWithResult(sourceData map[string]any, ruleList []*define.FieldRule) ([]b sourceData = make(map[string]any) } byteData, _ := json.Marshal(sourceData) + resultByteData, _ := json.Marshal(sourceData) for _, itemRule := range ruleList { if len(itemRule.Path) == 0 { // 未指定验证数据位置 @@ -89,11 +88,11 @@ func RunWithResult(sourceData map[string]any, ruleList []*define.FieldRule) ([]b } else { if !itemRule.DisableRewrite { // 更新数据 - byteData, _ = sjson.SetBytes(byteData, itemRule.Path, formatValue) + resultByteData, _ = sjson.SetBytes(resultByteData, itemRule.Path, formatValue) } } } - return byteData, nil + return resultByteData, nil } // checkRuleConditionRequiredRule 校验有条件必传