优化filter逻辑
This commit is contained in:
parent
47726c20b8
commit
a28bc364e4
48
filter.go
48
filter.go
@ -8,19 +8,14 @@
|
||||
package filter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/zhangdeman/json_filter/abstract"
|
||||
"git.zhangdeman.cn/zhangdeman/json_filter/implement"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/json_filter/gjson_hack"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
|
||||
"errors"
|
||||
"git.zhangdeman.cn/zhangdeman/json_filter/gjson_hack"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func NewFilterWithJson(sourceData string, filterRuleList []MapRule, jsonRead abstract.IJsonRead, jsonWrite abstract.IJsonWrite) *filter {
|
||||
@ -34,7 +29,6 @@ func NewFilterWithJson(sourceData string, filterRuleList []MapRule, jsonRead abs
|
||||
jsonRaad: jsonRead,
|
||||
jsonWrite: jsonWrite,
|
||||
sourceData: sourceData,
|
||||
formatResult: "",
|
||||
filterRuleList: filterRuleList,
|
||||
}
|
||||
}
|
||||
@ -47,7 +41,6 @@ func NewFilterWithJson(sourceData string, filterRuleList []MapRule, jsonRead abs
|
||||
func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
|
||||
return &filter{
|
||||
sourceData: sourceData,
|
||||
formatResult: "{}",
|
||||
jsonRaad: implement.NewGjsonRead(sourceData),
|
||||
jsonWrite: implement.NewSjsonWrite(),
|
||||
filterRuleList: filterRuleList,
|
||||
@ -63,7 +56,6 @@ type filter struct {
|
||||
jsonRaad abstract.IJsonRead
|
||||
jsonWrite abstract.IJsonWrite
|
||||
sourceData string
|
||||
formatResult string
|
||||
filterRuleList []MapRule
|
||||
}
|
||||
|
||||
@ -148,33 +140,10 @@ func (f *filter) setResult(rule MapRule) error {
|
||||
err error
|
||||
formatVal any
|
||||
)
|
||||
|
||||
sourceResult := gjson.Get(f.sourceData, rule.SourcePath)
|
||||
if formatVal, err = gjson_hack.Value(rule.DataType, sourceResult, rule.DefaultValue); nil != err {
|
||||
return fmt.Errorf("%s = %v can not convert to %s : %s", rule.SourcePath, sourceResult.Value(), rule.DataType, err.Error())
|
||||
if formatVal, err = f.jsonRaad.Value(rule.SourcePath, rule.DataType.String(), rule.DefaultValue); nil != err {
|
||||
return fmt.Errorf("%s = %v can not convert to %s : %s", rule.SourcePath, gjson.Get(f.sourceData, rule.SourcePath).String(), rule.DataType, err.Error())
|
||||
}
|
||||
if reflect.TypeOf(formatVal).Kind() == reflect.Map {
|
||||
// 获取的数据是map类型, 处理数据覆盖
|
||||
// eg : 配置如下两个规则 process.id(string) 、process(map[string]any)
|
||||
// 若输入数据的process.id为int类型, 则格式化后的process.id必为 string, 应为 process.id 规则的控制更精细
|
||||
gjsonVal := gjson.Get(f.formatResult, rule.TargetPath)
|
||||
if gjsonVal.Exists() && gjsonVal.IsObject() {
|
||||
var (
|
||||
existRes = map[string]any{}
|
||||
formatRes = map[string]any{}
|
||||
)
|
||||
// 已存在, 且是对象
|
||||
_ = serialize.JSON.UnmarshalWithNumber([]byte(gjsonVal.String()), &existRes)
|
||||
if err = serialize.JSON.Transition(formatVal, &formatRes); nil != err {
|
||||
return errors.New("conflict data path config deal fail : " + err.Error())
|
||||
}
|
||||
for k, v := range existRes {
|
||||
formatRes[k] = v
|
||||
}
|
||||
formatVal = formatRes // 重新赋值 formatVal
|
||||
}
|
||||
}
|
||||
if f.formatResult, err = sjson.Set(f.formatResult, rule.TargetPath, formatVal); nil != err {
|
||||
if err = f.jsonWrite.Set(rule.TargetPath, formatVal); nil != err {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@ -204,7 +173,7 @@ func (f *filter) getAllFinalData(res *[]string, resultList []gjson.Result, pathA
|
||||
//
|
||||
// Date : 21:18 2022/12/31
|
||||
func (f *filter) String() string {
|
||||
return f.formatResult
|
||||
return f.jsonWrite.Result()
|
||||
}
|
||||
|
||||
// Byte 获取格式化之后的字节数组
|
||||
@ -221,9 +190,10 @@ func (f *filter) Byte() []byte {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:20 2022/12/31
|
||||
func (f *filter) Parse(receiver interface{}) error {
|
||||
func (f *filter) Parse(receiver any) error {
|
||||
if nil == receiver {
|
||||
return errors.New("receiver is nil")
|
||||
}
|
||||
return json.Unmarshal(f.Byte(), receiver)
|
||||
return f.jsonWrite.MapWithReceiver(receiver)
|
||||
// return json.Unmarshal(f.Byte(), receiver)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user