From 0ca808c4fb7abd5ba7d4e156fc77151640f9922f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 30 Nov 2024 17:07:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E7=BB=84=E6=8C=89=E7=85=A7=E7=9C=9F?= =?UTF-8?q?=E5=AE=9E=E5=B1=82=E7=BA=A7=E5=B1=95=E5=BC=80=E5=90=8E,=20?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90=E6=96=B0=E7=9A=84=E6=98=A0?= =?UTF-8?q?=E5=B0=84=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gjson_hack/define.go | 3 ++- gjson_hack/path.go | 23 ++++++++++++++++++++--- gjson_hack/path_test.go | 10 +++++++--- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/gjson_hack/define.go b/gjson_hack/define.go index 855f4cf..577d477 100644 --- a/gjson_hack/define.go +++ b/gjson_hack/define.go @@ -40,5 +40,6 @@ const ( // // Date : 15:46 2024/11/29 type ExpendArrayResult struct { - PathList []string `json:"path_list"` // 路径列表 + PathList []string `json:"path_list"` // 路径列表 + PathMap map[string]string `json:"path_map"` // 数据源路径 => 目标路径的处理 } diff --git a/gjson_hack/path.go b/gjson_hack/path.go index 3f06cee..2a4bc38 100644 --- a/gjson_hack/path.go +++ b/gjson_hack/path.go @@ -10,8 +10,9 @@ package gjson_hack import ( "errors" "fmt" - "github.com/tidwall/gjson" "strings" + + "github.com/tidwall/gjson" ) // newDefaultPathOption 默认路径展开选项 @@ -131,22 +132,37 @@ func doExpandPath(gjsonResult gjson.Result, rootPath string, hasChildren bool, p // Author : go_developer@163.com<白茶清欢> // // Date : 11:13 2024/11/29 -func ExpandArrayPath(jsonStr string, pathConfig string, expendArrayResult *ExpendArrayResult) error { +func ExpandArrayPath(jsonStr string, pathConfig string, mapConfig string, expendArrayResult *ExpendArrayResult) error { if nil == expendArrayResult { return errors.New("expendArrayResult can not be nil") } if nil == expendArrayResult.PathList { expendArrayResult.PathList = make([]string, 0) } + if nil == expendArrayResult.PathMap { + expendArrayResult.PathMap = make(map[string]string) + } + if len(mapConfig) == 0 { + mapConfig = pathConfig + } if !strings.Contains(pathConfig, ArrayIdxTpl) { // 不是数组模板配置, 无需展开 expendArrayResult.PathList = append(expendArrayResult.PathList, pathConfig) + expendArrayResult.PathMap[pathConfig] = mapConfig return nil } + mapConfigArr := strings.Split(pathConfig, ArrayIdxTpl) + mapConfigArr[0] = strings.TrimSuffix(mapConfigArr[0], ".") + mapSuffixPathTpl := strings.TrimPrefix(strings.Join(mapConfigArr[1:], ArrayIdxTpl), ".") pathConfigArr := strings.Split(pathConfig, ArrayIdxTpl) pathConfigArr[0] = strings.TrimSuffix(pathConfigArr[0], ".") suffixPathTpl := strings.TrimPrefix(strings.Join(pathConfigArr[1:], ArrayIdxTpl), ".") + + if len(mapConfigArr) != len(pathConfigArr) { + return errors.New("mapConfig depth not equal pathConfig deep") + } + valueResult := gjson.Parse(jsonStr).Get(pathConfigArr[0]) if !valueResult.Exists() { // 路径不存在, 无需设置具体值 @@ -155,12 +171,13 @@ func ExpandArrayPath(jsonStr string, pathConfig string, expendArrayResult *Expen if !valueResult.IsArray() { // 不是数组,不要继续再向后展开了 expendArrayResult.PathList = append(expendArrayResult.PathList, pathConfigArr[0]) + expendArrayResult.PathMap[pathConfigArr[0]] = mapConfigArr[0] return nil } // 继续展开子项 for idx, _ := range valueResult.Array() { idxStr := fmt.Sprintf("%v", idx) - if err := ExpandArrayPath(jsonStr, pathConfigArr[0]+"."+idxStr+"."+suffixPathTpl, expendArrayResult); nil != err { + if err := ExpandArrayPath(jsonStr, pathConfigArr[0]+"."+idxStr+"."+suffixPathTpl, mapConfigArr[0]+"."+idxStr+"."+mapSuffixPathTpl, expendArrayResult); nil != err { return err } } diff --git a/gjson_hack/path_test.go b/gjson_hack/path_test.go index 6b2409b..16a6211 100644 --- a/gjson_hack/path_test.go +++ b/gjson_hack/path_test.go @@ -10,8 +10,9 @@ package gjson_hack import ( "encoding/json" "fmt" - "github.com/tidwall/gjson" "testing" + + "github.com/tidwall/gjson" ) func TestPath(t *testing.T) { @@ -163,7 +164,10 @@ func TestExpandArrayPath(t *testing.T) { byteData, _ := json.Marshal(mapData) jsonStr := string(byteData) // fmt.Println(jsonStr) - var pathExpendRes = &ExpendArrayResult{PathList: nil} - ExpandArrayPath(jsonStr, "user_list.{{idx}}.{{idx}}.age", pathExpendRes) + var pathExpendRes = &ExpendArrayResult{ + PathList: nil, + PathMap: nil, + } + ExpandArrayPath(jsonStr, "user_list.{{idx}}.{{idx}}.age", "", pathExpendRes) fmt.Println(pathExpendRes) }