save code

This commit is contained in:
白茶清欢 2022-01-25 20:14:47 +08:00
parent 5233c30f60
commit 3260dcbaf9

View File

@ -8,6 +8,7 @@
package json_tool package json_tool
import ( import (
"git.zhangdeman.cn/zhangdeman/gopkg/util"
"reflect" "reflect"
"strings" "strings"
@ -39,6 +40,7 @@ func NewDataFilter(source string, filterRule []*FilterDataRule) *DataFilter {
return &DataFilter{ return &DataFilter{
source: source, source: source,
filterRule: filterRule, filterRule: filterRule,
hasDealDiffPath: make(map[string]string),
} }
} }
@ -51,6 +53,7 @@ type DataFilter struct {
source string source string
filterRule []*FilterDataRule filterRule []*FilterDataRule
itemKeyToSlice bool itemKeyToSlice bool
hasDealDiffPath map[string]string
} }
// Filter 数据过滤 // Filter 数据过滤
@ -167,6 +170,15 @@ func (df *DataFilter) formatRule() {
item.MapKey = item.MapKey + ".[]" item.MapKey = item.MapKey + ".[]"
} else { } else {
// source 是数组, map也是数组, 检测数组层级匹配 // source 是数组, map也是数组, 检测数组层级匹配
sourcePathArr := strings.Split(item.SourceKey, ".[].")
mapPathArr := strings.Split(item.MapKey, ".[].")
if len(sourcePathArr) == len(mapPathArr) {
// 数组层级深度相同,无需特殊处理
continue
}
// 数组层级深度不同,重新对对齐数据
diffArr := sourcePathArr[0: len(sourcePathArr) - len(mapPathArr)]
df.dealDiffArr(diffArr)
} }
} else { } else {
if df.pathIsArrayValue(item.MapKey) { if df.pathIsArrayValue(item.MapKey) {
@ -186,3 +198,25 @@ func (df *DataFilter) formatRule() {
} }
} }
} }
// dealDiffArr 提取数据映射关系
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:04 下午 2022/1/25
func (df *DataFilter) dealDiffArr(diffArr []string) {
diffArrStr := strings.Join(diffArr, ".[].")
if _, exist := df.hasDealDiffPath[diffArrStr]; exist {
// 已经处理过, 不再重复处理
return
}
JSONObject, _ := gabs.Consume(df.source)
newPath := util.GenRandomString("", 8)
_, _ = JSONObject.ArrayCount()
newDataList := gabs.New()
// 没处理过, 开始处理
for index, path := range diffArr {
}
JSONObject.SetP(newPath, newDataList.String())
}