From 2634c53b79ebcb54e7233cd264dd25f347e50567 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, 2 Sep 2023 12:35:03 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=8F=90=E5=8F=96=E8=A7=84?= =?UTF-8?q?=E5=88=99=E8=84=B1=E6=95=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tool/gabs.go | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/tool/gabs.go b/tool/gabs.go index 1b3fdff..52d6736 100644 --- a/tool/gabs.go +++ b/tool/gabs.go @@ -19,10 +19,6 @@ import ( "github.com/tidwall/sjson" ) -const ( - virtualRoot = "__virtual__root" -) - // FilterOption 过滤选项 // // Author : go_developer@163.com<白茶清欢> @@ -58,6 +54,11 @@ func NewDataFilter(source string, filterRule []*FilterDataRule, filterOption *Fi filterOption.LogInstance = os.Stdout log.SetOutput(filterOption.LogInstance) } + // 去除末尾的 .[] + for _, item := range filterRule { + item.MapKey = strings.TrimRight(item.MapKey, ".[]") + item.SourceKey = strings.TrimRight(item.SourceKey, ".[]") + } return &DataFilter{ source: source, filterRule: filterRule, @@ -97,6 +98,7 @@ func (df *DataFilter) Filter() (string, error) { if err = df.setKV(itemRule); nil != err { return "", err } + continue } sourcePathArr := df.getArrPathList(itemRule.SourceKey) mapPathArr := df.getArrPathList(itemRule.MapKey) @@ -104,11 +106,19 @@ func (df *DataFilter) Filter() (string, error) { df.logPrint(logLevelFatal, "映射的层级深度大于数据源深度", "source_path => "+itemRule.SourceKey, "map_path => "+itemRule.MapKey) return "", fmt.Errorf("映射的层级深度大于数据源深度, source_path => %v map_path => %v", itemRule.SourceKey, itemRule.MapKey) } + + // 映射至非数组字段 if !mapIsArr { if err = df.setValue(itemRule.MapKey, df.getDataAsSlice(df.source, df.getArrPathList(itemRule.SourceKey))); nil != err { df.logPrint(logLevelFatal, "映射非数组, 数据源为数组, 设置失败", "source_path => "+itemRule.SourceKey, "map_path => "+itemRule.MapKey, " err => "+err.Error()) return "", fmt.Errorf("映射的层级深度大于数据源深度, source_path => %v map_path => %v", itemRule.SourceKey, itemRule.MapKey) } + continue + } + + if len(mapPathArr) == len(sourcePathArr) { + // 数组深度一致 + continue } } @@ -124,8 +134,27 @@ func (df *DataFilter) isArrPath(path string) bool { return strings.Contains(path, "[]") } +// setSameDeepArr 设置同深度的数组 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:19 2023/9/2 +func (df *DataFilter) setSameDeepArr(sourceVal string, sourcePath string, mapPath string) error { + sourcePathArr := df.getArrPathList(sourcePath) + mapPathArr := df.getArrPathList(mapPath) + for idx, itemSourcePath := range sourcePathArr { + sourceValueArr := gjson.Get(sourceVal, sourcePath).Array() + } + return nil +} + // a.[].b.[].c.[].d // e.[].f.[].g +// getDataAsSlice 抽取制定深度,生成list +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:17 2023/9/2 func (df *DataFilter) getDataAsSlice(sourceData string, pathList []string) []interface{} { //fmt.Println(sourceData, pathList) result := make([]interface{}, 0)