-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.go
More file actions
37 lines (34 loc) · 908 Bytes
/
utils.go
File metadata and controls
37 lines (34 loc) · 908 Bytes
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
package jsonmap
import "strings"
func createPath(path string) []string {
var keys []string
infos := strings.Split(path, ".")
var tmp strings.Builder
for _, k := range infos {
if len(k) > 0 && k[len(k)-1] == '\\' {
tmp.WriteString(k[:len(k)-1])
tmp.WriteString(".")
continue
} else {
tmp.WriteString(k)
}
parts := strings.Split(tmp.String(), "[")
for _, p := range parts {
t := strings.TrimSpace(strings.TrimSuffix(p, "]"))
if len(t) > 0 {
keys = append(keys, t)
}
}
tmp.Reset()
}
return keys
}
// EscapePath to escape a path
// Example
// - By default jsonmap.Set("message.raw", "hello world !")
// => { "message": { "raw": "hello world !" }}
// - With escape jsonmap.Set(jsonmap.EscapePath("message.raw"), "hello world !")
// => { "message.raw": "hello world !" }}
func EscapePath(path string) string {
return strings.Replace(path, ".", "\\.", -1)
}