save code
This commit is contained in:
parent
5233c30f60
commit
3260dcbaf9
@ -8,6 +8,7 @@
|
||||
package json_tool
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/gopkg/util"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
@ -39,6 +40,7 @@ func NewDataFilter(source string, filterRule []*FilterDataRule) *DataFilter {
|
||||
return &DataFilter{
|
||||
source: source,
|
||||
filterRule: filterRule,
|
||||
hasDealDiffPath: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,6 +53,7 @@ type DataFilter struct {
|
||||
source string
|
||||
filterRule []*FilterDataRule
|
||||
itemKeyToSlice bool
|
||||
hasDealDiffPath map[string]string
|
||||
}
|
||||
|
||||
// Filter 数据过滤
|
||||
@ -167,6 +170,15 @@ func (df *DataFilter) formatRule() {
|
||||
item.MapKey = item.MapKey + ".[]"
|
||||
} else {
|
||||
// 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 {
|
||||
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())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user