-
Notifications
You must be signed in to change notification settings - Fork 490
Expand file tree
/
Copy pathtools.go
More file actions
125 lines (117 loc) · 2.54 KB
/
tools.go
File metadata and controls
125 lines (117 loc) · 2.54 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package simplejson
import (
"bytes"
"fmt"
"strings"
)
var logbuff bytes.Buffer
func writeLog(frm string, arg ...interface{}) {
logbuff.WriteString(fmt.Sprintf(frm, arg...))
logbuff.WriteString("\n")
}
func (j *Json) Merge(data *Json) error {
dmp, err := data.Map()
if err != nil {
return err
}
for k, v := range dmp {
switch v.(type) {
case map[string]interface{}:
if _, f := j.CheckGet(k); !f {
j.Set(k, make(map[string]interface{}))
}
j.Get(k).Merge(Wrap(v))
default:
j.Set(k, v)
}
}
return nil
}
func (j *Json) compilePath(path string, stack []string) (*Json, error) {
for _, v := range stack {
if v == path {
return nil, fmt.Errorf("Circular reference: %s in %s", path, stack)
}
}
stack = append(stack, path)
if branch := j.GetPath(strings.Split(path, "/")...); !branch.Empty() {
return branch.compile(j, stack), nil
}
return nil, fmt.Errorf("Path not found: %s", path)
}
func (j *Json) compile(root *Json, stack []string) *Json {
switch j.data.(type) {
case map[string]interface{}:
out := New()
acc := New()
mp, _ := j.Map()
for k, v := range mp {
if k != "$include" {
if str, ok := v.(string); ok && len(str) > 0 && str[0] == 36 {
path := str[1:len(str)]
br, err := root.compilePath(path, stack)
if err != nil {
writeLog(err.Error())
continue
}
out.Set(k, br.Interface())
} else {
br := Wrap(v).compile(root, stack)
out.Set(k, br.Interface())
}
}
}
if fld, f := mp["$include"]; f {
if str, ok := fld.(string); ok {
fld = make([]interface{}, 1)
(fld.([]interface{}))[0] = str
}
if arr, ok := fld.([]interface{}); ok {
for _, v := range arr {
if path, ok := v.(string); ok {
br, err := root.compilePath(path, stack)
if err != nil {
writeLog(err.Error())
continue
}
acc.Merge(br)
}
}
}
}
if acc.Len() > 0 {
acc.Merge(out)
out = acc
}
return out
case []interface{}:
out := NewArray(j.Len())
mp, _ := j.Array()
for _, v := range mp {
br := Wrap(v).compile(root, stack)
out.AddArray(br)
}
return out
case string:
if str, _ := j.String(); len(str) > 0 && str[0] == 36 {
path := str[1:len(str)]
br, err := root.compilePath(path, stack)
if err != nil {
writeLog(err.Error())
return j
}
return br
}
}
return j
}
func (j *Json) Compile() (*Json, error) {
logbuff.Reset()
stack := make([]string, 0, 10)
var err error
out := j.compile(j, stack)
if logbuff.Len() > 0 {
err = fmt.Errorf(logbuff.String())
}
return out, err
}