优化filter逻辑
This commit is contained in:
parent
47726c20b8
commit
a28bc364e4
48
filter.go
48
filter.go
@ -8,19 +8,14 @@
|
|||||||
package filter
|
package filter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.zhangdeman.cn/zhangdeman/json_filter/abstract"
|
"git.zhangdeman.cn/zhangdeman/json_filter/abstract"
|
||||||
"git.zhangdeman.cn/zhangdeman/json_filter/implement"
|
"git.zhangdeman.cn/zhangdeman/json_filter/implement"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/json_filter/gjson_hack"
|
|
||||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
|
||||||
"github.com/tidwall/gjson"
|
|
||||||
"github.com/tidwall/sjson"
|
|
||||||
|
|
||||||
"errors"
|
"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 {
|
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,
|
jsonRaad: jsonRead,
|
||||||
jsonWrite: jsonWrite,
|
jsonWrite: jsonWrite,
|
||||||
sourceData: sourceData,
|
sourceData: sourceData,
|
||||||
formatResult: "",
|
|
||||||
filterRuleList: filterRuleList,
|
filterRuleList: filterRuleList,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -47,7 +41,6 @@ func NewFilterWithJson(sourceData string, filterRuleList []MapRule, jsonRead abs
|
|||||||
func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
|
func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
|
||||||
return &filter{
|
return &filter{
|
||||||
sourceData: sourceData,
|
sourceData: sourceData,
|
||||||
formatResult: "{}",
|
|
||||||
jsonRaad: implement.NewGjsonRead(sourceData),
|
jsonRaad: implement.NewGjsonRead(sourceData),
|
||||||
jsonWrite: implement.NewSjsonWrite(),
|
jsonWrite: implement.NewSjsonWrite(),
|
||||||
filterRuleList: filterRuleList,
|
filterRuleList: filterRuleList,
|
||||||
@ -63,7 +56,6 @@ type filter struct {
|
|||||||
jsonRaad abstract.IJsonRead
|
jsonRaad abstract.IJsonRead
|
||||||
jsonWrite abstract.IJsonWrite
|
jsonWrite abstract.IJsonWrite
|
||||||
sourceData string
|
sourceData string
|
||||||
formatResult string
|
|
||||||
filterRuleList []MapRule
|
filterRuleList []MapRule
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,33 +140,10 @@ func (f *filter) setResult(rule MapRule) error {
|
|||||||
err error
|
err error
|
||||||
formatVal any
|
formatVal any
|
||||||
)
|
)
|
||||||
|
if formatVal, err = f.jsonRaad.Value(rule.SourcePath, rule.DataType.String(), rule.DefaultValue); nil != err {
|
||||||
sourceResult := gjson.Get(f.sourceData, rule.SourcePath)
|
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 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 reflect.TypeOf(formatVal).Kind() == reflect.Map {
|
if err = f.jsonWrite.Set(rule.TargetPath, formatVal); nil != err {
|
||||||
// 获取的数据是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 {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -204,7 +173,7 @@ func (f *filter) getAllFinalData(res *[]string, resultList []gjson.Result, pathA
|
|||||||
//
|
//
|
||||||
// Date : 21:18 2022/12/31
|
// Date : 21:18 2022/12/31
|
||||||
func (f *filter) String() string {
|
func (f *filter) String() string {
|
||||||
return f.formatResult
|
return f.jsonWrite.Result()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Byte 获取格式化之后的字节数组
|
// Byte 获取格式化之后的字节数组
|
||||||
@ -221,9 +190,10 @@ func (f *filter) Byte() []byte {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 21:20 2022/12/31
|
// Date : 21:20 2022/12/31
|
||||||
func (f *filter) Parse(receiver interface{}) error {
|
func (f *filter) Parse(receiver any) error {
|
||||||
if nil == receiver {
|
if nil == receiver {
|
||||||
return errors.New("receiver is nil")
|
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