基于gjson,递归获取全部key的路径

This commit is contained in:
2024-11-27 16:07:32 +08:00
parent d115e1c5ba
commit 435edb0ea3
3 changed files with 247 additions and 0 deletions

122
gjson_hack/path.go Normal file
View File

@ -0,0 +1,122 @@
// Package gjson_hack ...
//
// Description : gjson_hack ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-11-27 11:58
package gjson_hack
import (
"fmt"
"github.com/tidwall/gjson"
"strings"
)
// newDefaultPathOption 默认路径展开选项
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:06 2024/11/27
func newDefaultPathOption() *PathOption {
return &PathOption{
UnfoldArray: true,
MaxDeep: 0,
OnlyFinalPath: false,
}
}
// newPathResult ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:17 2024/11/27
func newPathResult() *PathResult {
return &PathResult{
List: make([]string, 0),
ValueTable: make(map[string]gjson.Result),
}
}
// Path 查看全部路径
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:03 2024/11/27
func Path(jsonStr string, pathOption *PathOption) (*PathResult, error) {
gjsonResult := gjson.Parse(jsonStr)
pathResult := newPathResult()
if nil == pathOption {
pathOption = newDefaultPathOption()
}
doExpandPath(gjsonResult, "", true, pathOption, pathResult)
return pathResult, nil
}
// doExpandPath 展开路径
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:57 2024/11/27
func doExpandPath(gjsonResult gjson.Result, rootPath string, hasChildren bool, pathOption *PathOption, pathResult *PathResult) {
if gjsonResult.IsObject() {
// 处理object
gjsonResult.ForEach(func(key, value gjson.Result) bool {
newRootPath := ""
if len(rootPath) == 0 {
newRootPath = key.String()
} else {
newRootPath = rootPath + "." + key.String()
}
if value.IsArray() || value.IsObject() {
if !pathOption.OnlyFinalPath {
// 中间key路径也存储下来
pathResult.List = append(pathResult.List, newRootPath)
pathResult.ValueTable[newRootPath] = value
}
doExpandPath(value, newRootPath, true, pathOption, pathResult)
} else {
pathResult.List = append(pathResult.List, newRootPath)
pathResult.ValueTable[newRootPath] = value
}
return true
})
}
if gjsonResult.IsArray() {
arrayList := gjsonResult.Array()
if len(arrayList) == 0 {
pathResult.List = append(pathResult.List, rootPath)
pathResult.ValueTable[rootPath] = gjsonResult
return
}
if arrayList[0].IsObject() || arrayList[0].IsArray() {
// 每一项是对象或者数组
if pathOption.UnfoldArray {
// 展开数组
for idx, itemRes := range arrayList {
doExpandPath(itemRes, rootPath+"."+fmt.Sprintf("%v", idx), true, pathOption, pathResult)
}
} else {
// 不展开数组
doExpandPath(arrayList[0], rootPath+".#", true, pathOption, pathResult)
}
} else {
// 每一项是基础类型
pathResult.List = append(pathResult.List, rootPath)
pathResult.ValueTable[rootPath] = gjsonResult
return
}
}
if strings.HasSuffix(rootPath, ".#") {
// 处理不展开类型数组
return
}
if pathOption.OnlyFinalPath && hasChildren {
return
}
if _, exist := pathResult.ValueTable[rootPath]; !exist {
pathResult.List = append(pathResult.List, rootPath)
pathResult.ValueTable[rootPath] = gjsonResult
}
return
}