增加run with result

This commit is contained in:
白茶清欢 2024-06-12 15:44:43 +08:00
parent fb01d88681
commit 34962e0cd4

31
run.go
View File

@ -50,9 +50,28 @@ func RunForStruct(sourceData any, ruleList []*define.FieldRule) (map[string]any,
//
// Date : 14:13 2024/4/29
func Run(sourceData map[string]any, ruleList []*define.FieldRule) error {
if nil == sourceData || len(sourceData) == 0 {
byteData, err := RunWithResult(sourceData, ruleList)
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
}
return nil
}
// RunWithResult 运行并返回结果
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:42 2024/6/12
func RunWithResult(sourceData map[string]any, ruleList []*define.FieldRule) ([]byte, error) {
if nil == sourceData {
sourceData = make(map[string]any)
}
byteData, _ := json.Marshal(sourceData)
for _, itemRule := range ruleList {
if len(itemRule.Path) == 0 {
@ -63,7 +82,7 @@ func Run(sourceData map[string]any, ruleList []*define.FieldRule) error {
checkRuleConditionRequiredRule(byteData, itemRule)
inputFieldVal := gjson.GetBytes(byteData, itemRule.Path)
if formatRule, err := validate(byteData, inputFieldVal, itemRule); nil != err {
return err
return nil, err
} else {
if !itemRule.DisableRewrite {
// 更新数据
@ -71,13 +90,7 @@ func Run(sourceData map[string]any, ruleList []*define.FieldRule) error {
}
}
}
sourceData = make(map[string]any)
d := json.NewDecoder(bytes.NewReader(byteData))
d.UseNumber()
if err := d.Decode(&sourceData); nil != err {
return err
}
return nil
return byteData, nil
}
// checkRuleConditionRequiredRule 校验有条件必传