From 3260dcbaf95ebef61e3e3a26ad8c1037cab30fbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 25 Jan 2022 20:14:47 +0800 Subject: [PATCH] save code --- json_tool/gabs.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/json_tool/gabs.go b/json_tool/gabs.go index 25ef7d4..88e1050 100644 --- a/json_tool/gabs.go +++ b/json_tool/gabs.go @@ -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()) +}