From 89b8411fb2af79790670551b5f28195e62d9a235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 10 Sep 2021 11:36:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9F=BA=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=8F=90=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json_tool/{parse.go => filter.go} | 36 +++++++++++++++++------------- safe/data.go | 37 ------------------------------- 2 files changed, 20 insertions(+), 53 deletions(-) rename json_tool/{parse.go => filter.go} (74%) delete mode 100644 safe/data.go diff --git a/json_tool/parse.go b/json_tool/filter.go similarity index 74% rename from json_tool/parse.go rename to json_tool/filter.go index 64a4f3c..983cc70 100644 --- a/json_tool/parse.go +++ b/json_tool/filter.go @@ -25,8 +25,11 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 10:43 下午 2021/3/14 -func NewParseJSONTree(data interface{}) *ParseJSONTree { - return &ParseJSONTree{data: data} +func NewFilter(data interface{}, rule map[string]string) *Filter { + return &Filter{ + data: data, + rule: rule, + } } // ParseJSONTree 解析json树 @@ -34,34 +37,35 @@ func NewParseJSONTree(data interface{}) *ParseJSONTree { // Author : go_developer@163.com<白茶清欢> // // Date : 10:41 下午 2021/3/14 -type ParseJSONTree struct { +type Filter struct { data interface{} + rule map[string]string } -// Parse 解析数据 +// Result 数据过滤结果 // // Author : go_developer@163.com<白茶清欢> // // Date : 10:44 下午 2021/3/14 -func (pjt *ParseJSONTree) Parse(pathList []string) (*DynamicJSON, error) { - if !pjt.isLegalData() { +func (f *Filter) Result() (*DynamicJSON, error) { + if !f.isLegalData() { return nil, errors.New("非法的数据,无法转换成json") } result := NewDynamicJSON() - byteData, _ := json.Marshal(pjt.data) + byteData, _ := json.Marshal(f.data) source := string(byteData) - for _, path := range pathList { + for extraDataPath, newDataPath := range f.rule { // 为数组的处理 - pathArr := strings.Split(path, ".[].") + pathArr := strings.Split(extraDataPath, ".[].") val := gjson.Get(source, pathArr[0]) isComplexType := val.IsArray() || val.IsObject() if len(pathArr) == 1 { - result.SetValue(pathArr[0], val.Raw, isComplexType) + result.SetValue(newDataPath, val.Value(), isComplexType) continue } // 支持list再抽取一层,处于性能考虑,这么限制,不做递归无限深度处理 // 还能继续抽取,就认为是 []map[string]interface{} - ketList := strings.Split(pathArr[1], "|") + ketList := strings.Split(pathArr[1], ".") for idx, item := range val.Array() { data := item.Map() for _, key := range ketList { @@ -80,8 +84,8 @@ func (pjt *ParseJSONTree) Parse(pathList []string) (*DynamicJSON, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 10:46 下午 2021/3/14 -func (pjt *ParseJSONTree) isLegalData() bool { - val := reflect.ValueOf(pjt.data) +func (f *Filter) isLegalData() bool { + val := reflect.ValueOf(f.data) switch val.Kind() { case reflect.Slice: @@ -91,9 +95,9 @@ func (pjt *ParseJSONTree) isLegalData() bool { ok bool err error ) - if byteData, ok = pjt.data.([]byte); ok { + if byteData, ok = f.data.([]byte); ok { // 字节数组转map或者slice - if err = json.Unmarshal(byteData, &pjt.data); nil != err { + if err = json.Unmarshal(byteData, &f.data); nil != err { return false } return true @@ -107,7 +111,7 @@ func (pjt *ParseJSONTree) isLegalData() bool { case reflect.Ptr: // 指针 var err error - if pjt.data, err = util.StructToMap(pjt.data); nil != err { + if f.data, err = util.StructToMap(f.data); nil != err { return false } return true diff --git a/safe/data.go b/safe/data.go deleted file mode 100644 index 5ef1750..0000000 --- a/safe/data.go +++ /dev/null @@ -1,37 +0,0 @@ -// Package safe ... -// -// Description : 按需返回对外暴露的字段 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2021-03-10 6:38 下午 -package safe - -import ( - "strings" - - "github.com/buger/jsonparser" -) - -// Filter 按需输出数据 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 6:40 下午 2021/3/10 -func Filter(source []byte, filter map[string]string) ([]byte, error) { - var ( - bt []byte - setErr error - ) - for result, item := range filter { - fieldList := strings.Split(item, ".") - val, _, _, err := jsonparser.Get(source, fieldList...) - if nil != err { - return nil, err - } - if bt, setErr = jsonparser.Set(bt, val, strings.Split(result, ".")...); nil != setErr { - return nil, setErr - } - } - return bt, nil -}