feature/upgrade_filter #7

Merged
zhangdeman merged 14 commits from feature/upgrade_filter into master 2024-12-02 14:35:30 +08:00
3 changed files with 29 additions and 7 deletions
Showing only changes of commit 0ca808c4fb - Show all commits

View File

@ -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"` // 数据源路径 => 目标路径的处理
}

View File

@ -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
}
}

View File

@ -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)
}