gjson+sjson hack升级 #8
@@ -7,7 +7,19 @@
|
|||||||
// Date : 2024-12-03 11:36
|
// Date : 2024-12-03 11:36
|
||||||
package sjson_hack
|
package sjson_hack
|
||||||
|
|
||||||
import "strings"
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/json_filter/gjson_hack"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||||
|
"github.com/tidwall/sjson"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
reg = regexp.MustCompile(`({\{\#.*?\#\}\})`)
|
||||||
|
)
|
||||||
|
|
||||||
// Set 设置路径的值
|
// Set 设置路径的值
|
||||||
//
|
//
|
||||||
@@ -15,25 +27,42 @@ import "strings"
|
|||||||
//
|
//
|
||||||
// Date : 11:36 2024/12/3
|
// Date : 11:36 2024/12/3
|
||||||
func Set(jsonRes string, path string, value any) (string, error) {
|
func Set(jsonRes string, path string, value any) (string, error) {
|
||||||
if !isPathHasSpecialChar(path) {
|
fmt.Println(jsonRes, value)
|
||||||
// 不包含特殊字符
|
var (
|
||||||
}
|
err error
|
||||||
return "", nil
|
res string = jsonRes
|
||||||
|
)
|
||||||
|
|
||||||
|
// 包含特殊字符串, 匹配出特殊字符串
|
||||||
|
specialKeyList := getSpecialKeyList(path)
|
||||||
|
specialKeyTale := map[string]string{}
|
||||||
|
for _, item := range specialKeyList {
|
||||||
|
// 替换掉占位字符串
|
||||||
|
specialKeyTale[item] = wrapper.StringFromRandom(64, "").Md5().Value
|
||||||
|
path = strings.ReplaceAll(path, item, specialKeyTale[item])
|
||||||
}
|
}
|
||||||
|
|
||||||
// isPathHasSpecialChar 判断路径中是否包含特殊字符
|
if res, err = sjson.Set(res, path, value); nil != err {
|
||||||
|
return "", errors.New(path + " -> set json fail:" + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将特殊字符串替换回来
|
||||||
|
for sourceKey, convertKey := range specialKeyTale {
|
||||||
|
res = strings.ReplaceAll(res, convertKey, gjson_hack.GetRealKeyName(sourceKey))
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getSpecialKeyList 获取特殊key列表
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 11:38 2024/12/3
|
// Date : 17:35 2024/12/5
|
||||||
func isPathHasSpecialChar(path string) bool {
|
func getSpecialKeyList(path string) []string {
|
||||||
specialCharList := []string{
|
matchList := reg.FindAllString(path, -1)
|
||||||
".",
|
if len(matchList) == 0 {
|
||||||
|
return make([]string, 0)
|
||||||
}
|
}
|
||||||
for _, itemChar := range specialCharList {
|
return matchList
|
||||||
if strings.Contains(path, itemChar) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|||||||
18
sjson_hack/set_test.go
Normal file
18
sjson_hack/set_test.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
// Package sjson_hack ...
|
||||||
|
//
|
||||||
|
// Description : sjson_hack ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2024-12-05 17:32
|
||||||
|
package sjson_hack
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSet(t *testing.T) {
|
||||||
|
res, err := Set("{}", "{{#a.b#}}.c.{{#c.d#}}.e", "test")
|
||||||
|
fmt.Println(res, err)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user