-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdel.go
53 lines (47 loc) · 989 Bytes
/
del.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
52
53
package djson
import (
"strings"
"github.com/tidwall/sjson"
)
func deleteJsonWithPaths(s string, paths []string) (j string, err error) {
j = s
if len(paths) == 0 {
return
}
normal, deep := splitPaths(paths)
if len(normal) > 0 {
j, err = deleteNormal(j, normal)
if err != nil {
return
}
}
if len(deep) > 0 {
j, err = deleteDeep(j, deep)
}
return
}
func deleteNormal(s string, paths []string) (j string, err error) {
for _, path := range paths {
if j == "" {
j = string(s)
}
j, err = sjson.Delete(j, path)
if err != nil {
return j, err
}
}
return j, err
}
func splitPaths(paths []string) (normalPaths []string, deepPaths []string) {
for _, path := range paths {
if strings.Index(path, "*") == -1 && strings.Index(path, "#") == -1 {
normalPaths = append(normalPaths, path)
} else {
deepPaths = append(deepPaths, path)
}
}
return
}
func deleteDeep(s string, paths []string) (string, error) {
return jsonDeleteString(s, paths)
}