按照模板格式配置数组, 数据过滤时逐层展开

This commit is contained in:
2024-11-29 15:51:33 +08:00
parent 69066c35c2
commit 7743ee14ce
4 changed files with 100 additions and 5 deletions

View File

@ -8,6 +8,7 @@
package gjson_hack
import (
"errors"
"fmt"
"github.com/tidwall/gjson"
"strings"
@ -93,9 +94,9 @@ func doExpandPath(gjsonResult gjson.Result, rootPath string, hasChildren bool, p
// 每一项是对象或者数组
if pathOption.UnfoldArray {
// 展开数组
for idx, itemRes := range arrayList {
doExpandPath(itemRes, rootPath+"."+fmt.Sprintf("%v", idx), true, pathOption, pathResult)
doExpandPath(itemRes, rootPath+".{{idx}}", true, pathOption, pathResult)
for _, itemRes := range arrayList {
// doExpandPath(itemRes, rootPath+"."+fmt.Sprintf("%v", idx), true, pathOption, pathResult)
doExpandPath(itemRes, rootPath+"."+ArrayIdxTpl, true, pathOption, pathResult)
}
} else {
// 不展开数组
@ -108,7 +109,7 @@ func doExpandPath(gjsonResult gjson.Result, rootPath string, hasChildren bool, p
return
}
}
if strings.HasSuffix(rootPath, ".#") || strings.HasSuffix(rootPath, "{{idx}}") {
if strings.HasSuffix(rootPath, ".#") || strings.HasSuffix(rootPath, ArrayIdxTpl) {
// 处理不展开类型数组
return
}
@ -122,3 +123,46 @@ func doExpandPath(gjsonResult gjson.Result, rootPath string, hasChildren bool, p
}
return
}
// ExpandArrayPath 根据真实数据展开数组路径
//
// 路径中若是包含 {{idx}} 占位符, 说明是需要展开的数组, 根据数组的时机数据, 进行数组的逐级展开
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:13 2024/11/29
func ExpandArrayPath(jsonStr string, pathConfig 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 !strings.Contains(pathConfig, ArrayIdxTpl) {
// 不是数组模板配置, 无需展开
expendArrayResult.PathList = append(expendArrayResult.PathList, pathConfig)
return nil
}
pathConfigArr := strings.Split(pathConfig, ArrayIdxTpl)
pathConfigArr[0] = strings.TrimSuffix(pathConfigArr[0], ".")
suffixPathTpl := strings.TrimPrefix(strings.Join(pathConfigArr[1:], ArrayIdxTpl), ".")
valueResult := gjson.Parse(jsonStr).Get(pathConfigArr[0])
if !valueResult.Exists() {
// 路径不存在, 无需设置具体值
return nil
}
if !valueResult.IsArray() {
// 不是数组,不要继续再向后展开了
expendArrayResult.PathList = append(expendArrayResult.PathList, pathConfigArr[0])
return nil
}
// 继续展开子项
for idx, _ := range valueResult.Array() {
idxStr := fmt.Sprintf("%v", idx)
if err := ExpandArrayPath(jsonStr, pathConfigArr[0]+"."+idxStr+"."+suffixPathTpl, expendArrayResult); nil != err {
return err
}
}
return nil
}