fix both modify

This commit is contained in:
白茶清欢 2022-01-30 23:36:15 +08:00
commit 5cc0d61e82
2 changed files with 20 additions and 23 deletions

View File

@ -14,7 +14,6 @@ import (
"strings" "strings"
"git.zhangdeman.cn/zhangdeman/gopkg/util" "git.zhangdeman.cn/zhangdeman/gopkg/util"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
@ -76,17 +75,6 @@ func (df *DataFilter) Filter() (string, error) {
// 创建数据的根结点 // 创建数据的根结点
jsonObject = gabs.New() jsonObject = gabs.New()
for _, item := range df.filterRule { for _, item := range df.filterRule {
if df.pathIsArrayValue(item.SourceKey) {
// 数组, 特殊处理
// 0. 判断目标路径是否为数组
// 1. 判断数据源数组深度与目标数组深度是否一致
sourcePathArr := strings.Split(item.SourceKey, ".[].")
mapPathArr := strings.Split(item.MapKey, ".[].")
if len(sourcePathArr) != len(mapPathArr) {
// return "", errors.New("slice转化原始数据深度与目标数据深度不一致")
}
continue
}
// 数据源路径不识数组, 多个key写入到同一个map key, 并且map key 不是以[]结尾, 自动格式化 // 数据源路径不识数组, 多个key写入到同一个map key, 并且map key 不是以[]结尾, 自动格式化
// 目标位置, 是一个数组 // 目标位置, 是一个数组
if df.pathIsArrayValue(item.MapKey) { if df.pathIsArrayValue(item.MapKey) {
@ -140,7 +128,11 @@ func (df *DataFilter) UserItemToSlice() {
// //
// Date : 2022/1/23 12:45 AM // Date : 2022/1/23 12:45 AM
func (df *DataFilter) getValueType(valueResult gjson.Result) string { func (df *DataFilter) getValueType(valueResult gjson.Result) string {
dataType := reflect.TypeOf(valueResult.Value()).String() dataTypeVal := reflect.TypeOf(valueResult.Value())
if nil == dataTypeVal {
return "NIL"
}
dataType := dataTypeVal.String()
if strings.Contains(dataType, "int") { if strings.Contains(dataType, "int") {
return "int64" return "int64"
} }
@ -180,8 +172,11 @@ func (df *DataFilter) formatRule() {
continue continue
} }
// 数组层级深度不同,重新对对齐数据 // 数组层级深度不同,重新对对齐数据
diffArr := sourcePathArr[0 : len(sourcePathArr)-len(mapPathArr)-1] diffArr := sourcePathArr[0 : len(sourcePathArr)-len(mapPathArr)+1]
df.dealDiffArr(diffArr, sourcePathArr[len(sourcePathArr)-len(mapPathArr)-1:], mapPathArr) if newPath := df.dealDiffArr(diffArr, sourcePathArr[len(sourcePathArr)-len(mapPathArr)-1:], mapPathArr); len(newPath) > 0 {
sourcePathArr[len(sourcePathArr)-len(mapPathArr)] = newPath
item.SourceKey = strings.Join(sourcePathArr[len(sourcePathArr)-len(mapPathArr):], ".[].")
}
} }
} else { } else {
if df.pathIsArrayValue(item.MapKey) { if df.pathIsArrayValue(item.MapKey) {
@ -207,20 +202,20 @@ func (df *DataFilter) formatRule() {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 5:04 下午 2022/1/25 // Date : 5:04 下午 2022/1/25
func (df *DataFilter) dealDiffArr(diffArr []string, sourceAfterArr []string, mapArr []string) { func (df *DataFilter) dealDiffArr(diffArr []string, sourceAfterArr []string, mapArr []string) string {
if len(diffArr) == 0 {
return ""
}
diffArrStr := strings.Join(diffArr, ".[].") diffArrStr := strings.Join(diffArr, ".[].")
if _, exist := df.hasDealDiffPath[diffArrStr]; exist { if _, exist := df.hasDealDiffPath[diffArrStr]; exist {
// 已经处理过, 不再重复处理 // 已经处理过, 不再重复处理
return return df.hasDealDiffPath[diffArrStr]
}
if len(diffArr) == 0 {
return
} }
// 没处理过, 开始处理 // 没处理过, 开始处理
jsonResultList := df.getArrayData(df.source, diffArr) jsonResultList := df.getArrayData(df.source, diffArr)
if len(jsonResultList) == 0 { if len(jsonResultList) == 0 {
return return ""
} }
newPath := util.GenRandomString("", 8) newPath := util.GenRandomString("", 8)
var result map[string]interface{} var result map[string]interface{}
@ -234,6 +229,8 @@ func (df *DataFilter) dealDiffArr(diffArr []string, sourceAfterArr []string, map
} }
df.source = JSONObject.String() df.source = JSONObject.String()
df.hasDealDiffPath[diffArrStr] = newPath
return newPath
} }
// getArrayData 获取数据 // getArrayData 获取数据
@ -248,7 +245,7 @@ func (df *DataFilter) getArrayData(source string, pathArr []string) []gjson.Resu
resultList := make([]gjson.Result, 0) resultList := make([]gjson.Result, 0)
dataList := gjson.Get(source, pathArr[0]).Array() dataList := gjson.Get(source, pathArr[0]).Array()
for idx := 0; idx < len(dataList); idx++ { for idx := 0; idx < len(dataList); idx++ {
resultList = append(resultList, df.getArrayData(gjson.Get(source, dataList[idx].String()).String(), pathArr[1:])...) resultList = append(resultList, df.getArrayData(dataList[idx].String(), pathArr[1:])...)
} }
return resultList return resultList
} }

View File

@ -267,7 +267,7 @@ func TestDataFilterDiffArr(t *testing.T) {
rule := []*FilterDataRule{ rule := []*FilterDataRule{
// {SourceKey: "name", MapKey: "slice.[]", DefaultValue: "用户姓名默认值"}, // {SourceKey: "name", MapKey: "slice.[]", DefaultValue: "用户姓名默认值"},
{SourceKey: "table.[].user_list.[].name", MapKey: "user_list.[].detail.name", DefaultValue: "用户姓名默认值"}, {SourceKey: "table.[].user_list.[].name", MapKey: "user_list.[].detail.name", DefaultValue: "用户姓名默认值"},
{SourceKey: "table.[].user_list.[].age", MapKey: "user_list.[]detail.age", DefaultValue: "用户姓名默认值"}, {SourceKey: "table.[].user_list.[].age", MapKey: "user_list.[].detail.age", DefaultValue: "用户姓名默认值"},
} }
byteData, _ := json.Marshal(source) byteData, _ := json.Marshal(source)
filter := NewDataFilter(string(byteData), rule) filter := NewDataFilter(string(byteData), rule)