-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.go
51 lines (45 loc) · 1.01 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package collectjs
import (
"strings"
"github.com/tidwall/gjson"
"github.com/tkeel-io/collectjs/pkg/json/jsonparser"
)
func Byte(raw string) []byte {
return []byte(raw)
}
func path2JSONPARSER(path string) []string {
keys := []string{}
if len(path) > 0 {
if path[0] == '"' && path[len(path)-1] == '"' {
return []string{path[1 : len(path)-1]}
}
path = strings.Replace(path, "[", ".[", -1)
keys = strings.Split(path, ".")
}
if len(keys) > 0 && keys[0] == "" {
return keys[1:]
}
return keys
}
func path2GJSON(path string) string {
path = strings.Replace(path, "[", ".", -1)
path = strings.Replace(path, "]", "", -1)
if len(path) > 0 && path[0] == '.' {
return path[1:]
}
return path
}
func convertType(res gjson.Result) jsonparser.ValueType {
switch res.Type {
case gjson.False, gjson.True:
return jsonparser.Boolean
case gjson.Number:
return jsonparser.Number
case gjson.String:
return jsonparser.String
case gjson.JSON:
return jsonparser.Object
default:
return jsonparser.NotExist
}
}