From 19813fa9ee5a5ef3eadf3531950c29c840ab9a0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 27 Jan 2022 12:06:24 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E5=B1=82=E7=BA=A7=E6=95=B0=E7=BB=84=E6=95=B0=E6=8D=AE=E5=AF=B9?= =?UTF-8?q?=E9=BD=90=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json_tool/gabs.go | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/json_tool/gabs.go b/json_tool/gabs.go index 71553d5..e8ede3b 100644 --- a/json_tool/gabs.go +++ b/json_tool/gabs.go @@ -76,17 +76,6 @@ func (df *DataFilter) Filter() (string, error) { // 创建数据的根结点 jsonObject = gabs.New() 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 不是以[]结尾, 自动格式化 // 目标位置, 是一个数组 if df.pathIsArrayValue(item.MapKey) { @@ -180,8 +169,11 @@ func (df *DataFilter) formatRule() { continue } // 数组层级深度不同,重新对对齐数据 - diffArr := sourcePathArr[0 : len(sourcePathArr)-len(mapPathArr)-1] - df.dealDiffArr(diffArr) + diffArr := sourcePathArr[0 : len(sourcePathArr)-len(mapPathArr)+1] + if newPath := df.dealDiffArr(diffArr); len(newPath) > 0 { + sourcePathArr[len(sourcePathArr)-len(mapPathArr)] = newPath + item.SourceKey = strings.Join(sourcePathArr[len(sourcePathArr)-len(mapPathArr):], ".[].") + } } } else { if df.pathIsArrayValue(item.MapKey) { @@ -207,20 +199,21 @@ func (df *DataFilter) formatRule() { // Author : go_developer@163.com<白茶清欢> // // Date : 5:04 下午 2022/1/25 -func (df *DataFilter) dealDiffArr(diffArr []string) { +func (df *DataFilter) dealDiffArr(diffArr []string) string { + if len(diffArr) == 0 { + return "" + } + diffArrStr := strings.Join(diffArr, ".[].") if _, exist := df.hasDealDiffPath[diffArrStr]; exist { // 已经处理过, 不再重复处理 - return - } - if len(diffArr) == 0 { - return + return df.hasDealDiffPath[diffArrStr] } // 没处理过, 开始处理 jsonResultList := df.getArrayData(df.source, diffArr) if len(jsonResultList) == 0 { - return + return "" } newPath := util.GenRandomString("", 8) var result map[string]interface{} @@ -234,6 +227,8 @@ func (df *DataFilter) dealDiffArr(diffArr []string) { } df.source = JSONObject.String() + df.hasDealDiffPath[diffArrStr] = newPath + return newPath } // getArrayData 获取数据 @@ -248,7 +243,8 @@ func (df *DataFilter) getArrayData(source string, pathArr []string) []gjson.Resu resultList := make([]gjson.Result, 0) dataList := gjson.Get(source, pathArr[0]).Array() for idx := 0; idx < len(dataList); idx++ { - resultList = append(resultList, df.getArrayData(gjson.Get(source, dataList[idx].String()).String(), pathArr[1:])...) + // sourceData := gjson.Get(source, dataList[idx].String()).String() + resultList = append(resultList, df.getArrayData(dataList[idx].String(), pathArr[1:])...) } return resultList } From 51808626a99b700e7841e665dfe7538660255a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 27 Jan 2022 14:23:41 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E5=8F=8D=E5=B0=84NPE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json_tool/gabs.go | 7 +++++-- json_tool/json_test.go | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/json_tool/gabs.go b/json_tool/gabs.go index e8ede3b..077b57f 100644 --- a/json_tool/gabs.go +++ b/json_tool/gabs.go @@ -129,7 +129,11 @@ func (df *DataFilter) UserItemToSlice() { // // Date : 2022/1/23 12:45 AM 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") { return "int64" } @@ -243,7 +247,6 @@ func (df *DataFilter) getArrayData(source string, pathArr []string) []gjson.Resu resultList := make([]gjson.Result, 0) dataList := gjson.Get(source, pathArr[0]).Array() for idx := 0; idx < len(dataList); idx++ { - // sourceData := gjson.Get(source, dataList[idx].String()).String() resultList = append(resultList, df.getArrayData(dataList[idx].String(), pathArr[1:])...) } return resultList diff --git a/json_tool/json_test.go b/json_tool/json_test.go index e8cb6ef..b479147 100644 --- a/json_tool/json_test.go +++ b/json_tool/json_test.go @@ -267,7 +267,7 @@ func TestDataFilterDiffArr(t *testing.T) { rule := []*FilterDataRule{ // {SourceKey: "name", MapKey: "slice.[]", 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) filter := NewDataFilter(string(byteData), rule)