增加map冲突性配置的处理
This commit is contained in:
27
filter.go
27
filter.go
@ -11,7 +11,9 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.zhangdeman.cn/zhangdeman/consts"
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@ -52,7 +54,7 @@ type filter struct {
|
|||||||
func (f *filter) Deal() error {
|
func (f *filter) Deal() error {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
formatVal interface{}
|
formatVal any
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, rule := range f.filterRuleList {
|
for _, rule := range f.filterRuleList {
|
||||||
@ -67,6 +69,27 @@ func (f *filter) Deal() error {
|
|||||||
if formatVal, err = f.getValue(rule.DataType, sourceResult, rule.DefaultValue); nil != err {
|
if formatVal, err = f.getValue(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())
|
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 {
|
||||||
|
// 获取的数据是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 f.formatResult, err = sjson.Set(f.formatResult, rule.TargetPath, formatVal); nil != err {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -173,7 +196,7 @@ func (f *filter) Parse(receiver interface{}) error {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 12:25 2022/7/4
|
// Date : 12:25 2022/7/4
|
||||||
func (f *filter) getValue(dataType string, sourceValue gjson.Result, defaultValue string) (interface{}, error) {
|
func (f *filter) getValue(dataType string, sourceValue gjson.Result, defaultValue string) (any, error) {
|
||||||
sourceValueStr := defaultValue
|
sourceValueStr := defaultValue
|
||||||
if sourceValue.Exists() {
|
if sourceValue.Exists() {
|
||||||
str := sourceValue.String()
|
str := sourceValue.String()
|
||||||
|
Reference in New Issue
Block a user