Compare commits

..

No commits in common. "1634f1bc5fc1db228de5e0cdfdf9897ca66c659c" and "3a4f5332c9d90ed6986914e2eca3b4438f91c101" have entirely different histories.

2 changed files with 11 additions and 30 deletions

View File

@ -20,23 +20,12 @@ import (
"git.zhangdeman.cn/zhangdeman/gopkg/util"
)
// MapDataRule 数据映射结果
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:09 PM 2022/1/17
type MapDataRule struct {
MapKey string
DefaultValue string
IsComplexType bool
}
// NewFilter 获取解析的实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:43 下午 2021/3/14
func NewFilter(data interface{}, rule map[string]MapDataRule) *Filter {
func NewFilter(data interface{}, rule map[string]string) *Filter {
return &Filter{
data: data,
rule: rule,
@ -50,7 +39,7 @@ func NewFilter(data interface{}, rule map[string]MapDataRule) *Filter {
// Date : 10:41 下午 2021/3/14
type Filter struct {
data interface{}
rule map[string]MapDataRule
rule map[string]string
}
// Result 数据过滤结果
@ -65,12 +54,12 @@ func (f *Filter) Result() (*DynamicJSON, error) {
result := NewDynamicJSON()
byteData, _ := json.Marshal(f.data)
source := string(byteData)
for extraDataPath, newDataRule := range f.rule {
for extraDataPath, newDataPath := range f.rule {
// 为数组的处理
pathArr := strings.Split(extraDataPath, ".[].")
val := gjson.Get(source, pathArr[0])
if len(pathArr) == 1 {
f.SetValue(result, newDataRule.MapKey, val.Value(), false, 0)
f.SetValue(result, newDataPath, val.Value(), false, 0)
continue
}
// 支持list再抽取一层,处于性能考虑,这么限制,不做递归无限深度处理
@ -80,11 +69,9 @@ func (f *Filter) Result() (*DynamicJSON, error) {
data := item.Map()
for _, key := range ketList {
if v, exist := data[key]; exist {
result.SetValue(strings.ReplaceAll(newDataRule.MapKey, "[]", fmt.Sprintf("[%d]", idx)), data[key].Value(), v.IsObject() || v.IsArray())
} else {
// 结果集中不存在对应key,设置默认值
result.SetValue(strings.ReplaceAll(newDataRule.MapKey, "[]", fmt.Sprintf("[%d]", idx)), newDataRule.DefaultValue, newDataRule.IsComplexType)
result.SetValue(strings.ReplaceAll(newDataPath, "[]", fmt.Sprintf("[%d]", idx)), data[key].Raw, v.IsObject() || v.IsArray())
}
// 结果集中不存在对应key,丢弃
}
}
}

View File

@ -69,19 +69,13 @@ func TestSelect(t *testing.T) {
"table": []map[string]interface{}{
{"name": "alex", "age": 18, "number": 1},
{"name": "bob", "age": 28, "number": 2},
{"name": "bob", "age": 28, "number": 2, "list": []int{1, 2, 3}},
},
}
rule := map[string]MapDataRule{
"name": {
MapKey: "user_name",
DefaultValue: "用户姓名默认值",
IsComplexType: false,
},
"extra.age": {MapKey: "user_age", DefaultValue: "用户年龄默认值", IsComplexType: false},
"extra.height": {MapKey: "user_height", DefaultValue: "扩展高度默认值", IsComplexType: false},
"table.[].name": {MapKey: "slice.[].name_modify", DefaultValue: "列表姓名默认值", IsComplexType: false},
"table.[].list": {MapKey: "slice.[].data_list", DefaultValue: "[\"567\",\"678\",\"789\"]", IsComplexType: true},
rule := map[string]string{
"name": "user_name",
"extra.age": "user_age",
"extra.height": "user_height",
"table.[].name": "slice.[].name",
}
filter := NewFilter(source, rule)
d, e := filter.Result()