修复raw异常

This commit is contained in:
白茶清欢 2022-01-17 18:09:50 +08:00
parent 3a4f5332c9
commit 42938cda83
2 changed files with 30 additions and 11 deletions

View File

@ -20,12 +20,23 @@ 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]string) *Filter {
func NewFilter(data interface{}, rule map[string]MapDataRule) *Filter {
return &Filter{
data: data,
rule: rule,
@ -39,7 +50,7 @@ func NewFilter(data interface{}, rule map[string]string) *Filter {
// Date : 10:41 下午 2021/3/14
type Filter struct {
data interface{}
rule map[string]string
rule map[string]MapDataRule
}
// Result 数据过滤结果
@ -54,12 +65,12 @@ func (f *Filter) Result() (*DynamicJSON, error) {
result := NewDynamicJSON()
byteData, _ := json.Marshal(f.data)
source := string(byteData)
for extraDataPath, newDataPath := range f.rule {
for extraDataPath, newDataRule := range f.rule {
// 为数组的处理
pathArr := strings.Split(extraDataPath, ".[].")
val := gjson.Get(source, pathArr[0])
if len(pathArr) == 1 {
f.SetValue(result, newDataPath, val.Value(), false, 0)
f.SetValue(result, newDataRule.MapKey, val.Value(), false, 0)
continue
}
// 支持list再抽取一层,处于性能考虑,这么限制,不做递归无限深度处理
@ -69,9 +80,11 @@ func (f *Filter) Result() (*DynamicJSON, error) {
data := item.Map()
for _, key := range ketList {
if v, exist := data[key]; exist {
result.SetValue(strings.ReplaceAll(newDataPath, "[]", fmt.Sprintf("[%d]", idx)), data[key].Raw, v.IsObject() || v.IsArray())
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)
}
// 结果集中不存在对应key,丢弃
}
}
}

View File

@ -69,13 +69,19 @@ 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]string{
"name": "user_name",
"extra.age": "user_age",
"extra.height": "user_height",
"table.[].name": "slice.[].name",
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},
}
filter := NewFilter(source, rule)
d, e := filter.Result()