gjson的二次处理 #6
@ -29,3 +29,16 @@ type PathResult struct {
|
||||
List []string // 全部路径列表
|
||||
ValueTable map[string]gjson.Result // 路径对应的值
|
||||
}
|
||||
|
||||
const (
|
||||
ArrayIdxTpl = "{{idx}}"
|
||||
)
|
||||
|
||||
// ExpendArrayResult 展开数组的结果
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:46 2024/11/29
|
||||
type ExpendArrayResult struct {
|
||||
PathList []string `json:"path_list"` // 路径列表
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -129,3 +129,41 @@ func TestPathOnlyFinallyPathWithUnfoldArray(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpandArrayPath(t *testing.T) {
|
||||
mapData := map[string]any{
|
||||
"person_list": []map[string]any{
|
||||
{"name": "zhang", "age": 10},
|
||||
{"name": "li", "age": 20},
|
||||
{"name": "wang", "age": 30},
|
||||
},
|
||||
"company_info": map[string]any{
|
||||
"address": "Beijing",
|
||||
"email": "xxx@xxx.com",
|
||||
},
|
||||
"sex": "man",
|
||||
"user_list": [][]map[string]any{
|
||||
[]map[string]any{
|
||||
{"name": "zhang", "age": 10},
|
||||
{"name": "li", "age": 20},
|
||||
{"name": "wang", "age": 30},
|
||||
},
|
||||
[]map[string]any{
|
||||
{"name": "zhang", "age": 10},
|
||||
{"name": "li", "age": 20},
|
||||
{"name": "wang", "age": 30},
|
||||
},
|
||||
[]map[string]any{
|
||||
{"name": "zhang", "age": 10},
|
||||
{"name": "li", "age": 20},
|
||||
{"name": "wang", "age": 30},
|
||||
},
|
||||
},
|
||||
}
|
||||
byteData, _ := json.Marshal(mapData)
|
||||
jsonStr := string(byteData)
|
||||
// fmt.Println(jsonStr)
|
||||
var pathExpendRes = &ExpendArrayResult{PathList: nil}
|
||||
ExpandArrayPath(jsonStr, "user_list.{{idx}}.{{idx}}.age", pathExpendRes)
|
||||
fmt.Println(pathExpendRes)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Package gjson_hack ...
|
||||
// Package gjson_hack 精确地类型转换
|
||||
//
|
||||
// Description : gjson_hack ...
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user