From 435b339604ec0a4c8d6fbf57a1209d4e2b8c9b29 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, 31 Aug 2023 21:06:35 +0800 Subject: [PATCH] save code --- tool/gabs.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tool/gabs.go b/tool/gabs.go index c83307b..fae0428 100644 --- a/tool/gabs.go +++ b/tool/gabs.go @@ -21,6 +21,10 @@ import ( "github.com/Jeffail/gabs" ) +const ( + virtualRoot = "__virtual__root" +) + // FilterDataRule 参数过滤规则 // // Author : go_developer@163.com<白茶清欢> @@ -39,6 +43,19 @@ type FilterDataRule struct { // // Date : 2022/1/22 9:50 PM func NewDataFilter(source string, filterRule []*FilterDataRule) *DataFilter { + + jsonTree := gjson.Parse(source) + isVirtual := false + if jsonTree.IsArray() { + source = fmt.Sprintf(`{"%v":%v}`, virtualRoot, source) + isVirtual = true + } + if isVirtual { + for _, item := range filterRule { + item.MapKey = virtualRoot + "." + item.MapKey + item.SourceKey = virtualRoot + "." + item.SourceKey + } + } return &DataFilter{ source: source, filterRule: filterRule, @@ -56,6 +73,7 @@ type DataFilter struct { filterRule []*FilterDataRule itemKeyToSlice bool hasDealDiffPath map[string]string + isVirtualRoot bool } // Filter 数据过滤 @@ -116,7 +134,11 @@ func (df *DataFilter) Filter() (string, error) { return "", err } } - return jsonObject.String(), nil + final := jsonObject.String() + if df.isVirtualRoot { + return gjson.Get(final, virtualRoot).String(), nil + } + return final, nil } // UserItemToSlice 支持多个独立的字段合并到slice中