-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjsnm.go
208 lines (197 loc) · 4 KB
/
jsnm.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package jsnm
import (
"fmt"
"reflect"
"strconv"
"strings"
)
// No Cache Get
func (j *Jsnm) NCGet(path ...string) *Jsnm {
if j == nil || len(path) <= 0 {
return j
}
// first step: get data from mapdata
map_data, ok := j.raw_data.(map[string]interface{})
if !ok {
return nil
}
cur, ok := map_data[path[0]]
if !ok {
return nil
}
// second step: cache the data
subj := NewJsnm(cur)
if len(path) == 1 {
return subj
}
return subj.NCGet(path[1:]...)
}
// Cache PathGet
func (j *Jsnm) PathGet(path ...string) *Jsnm {
if j == nil || len(path) <= 0 {
return j
}
jm := j
var cache_jm *Jsnm
var exist bool
var sub_data interface{}
for _, subpath := range path {
if jm.cache == nil {
jm.cache = make(map[string]*Jsnm)
//fmt.Printf("**make cache,subpath:%s, path:%#v\n",subpath,path)
} else {
if cache_jm, exist = jm.cache[subpath]; exist {
jm = cache_jm
continue
}
}
if jm.map_data == nil {
jm.map_data = make(MapData)
if map_data, ok := jm.raw_data.(map[string]interface{}); ok {
//fmt.Printf("@@cache map_data,subpath:%s, path:%#v\n",subpath,path)
jm.map_data = map_data
} else {
return nil
}
}
sub_data, exist = jm.map_data[subpath]
if !exist {
return nil
}
sub_jm := NewJsnm(sub_data)
jm.cache[subpath] = sub_jm
//fmt.Printf("##cache jsnm, subpath:%s\n",subpath)
jm = sub_jm
}
return jm
}
// Cache Get
func (j *Jsnm) Get(path string) *Jsnm {
if j == nil {
return nil
}
// first step: get data from cache
if nil == j.cache {
j.cache = make(map[string]*Jsnm)
//fmt.Println("**make cache")
} else {
if cache_data, ok := j.cache[path]; ok {
return cache_data
}
}
// second step: get data from mapdata
if j.map_data == nil {
if map_data, ok := j.raw_data.(map[string]interface{}); ok {
j.map_data = map_data
//fmt.Println("@@cache map_data, path:",path)
} else {
return nil
}
}
cur, ok := j.map_data[path]
if !ok {
return nil
}
// third step: cache the data
will_cache_data := NewJsnm(cur)
//fmt.Println("##cache jsnm, path:",path)
j.cache[path] = will_cache_data
return will_cache_data
}
// Cache Arr
func (j *Jsnm) Arr() []*Jsnm {
if j == nil {
return nil
}
if j.arr_data != nil {
return j.arr_data
}
arr, ok := (j.raw_data).([]interface{})
if !ok {
return nil
}
ret := make([]*Jsnm, 0, len(arr))
for _, vry := range arr {
ret = append(ret, NewJsnm(vry))
}
//fmt.Println("cache arr_data")
j.arr_data = ret
return ret
}
// Cache ArrLocs
func (j *Jsnm) ArrLocs(locs ...int) *Jsnm {
if len(locs) <= 0 {
return nil
}
subarr := j.ArrLoc(locs[0])
l := len(locs)
for i := 1; i < l; i++ {
if subarr == nil {
return nil
}
subarr = subarr.ArrLoc(locs[i])
}
return subarr
}
// Cache ArrLoc i
func (j *Jsnm) ArrLoc(i int) *Jsnm {
if j == nil {
return nil
}
if nil != j.arr_data {
//if i >= len(j.arr_data) {
// return nil
//}
if j.arr_data[i] != nil && i < len(j.arr_data) {
return j.arr_data[i]
}
}
arr, ok := (j.raw_data).([]interface{})
if !ok || i >= len(arr) {
return nil
}
arr_cache := make([]*Jsnm, len(arr))
//fmt.Println("cache arr_data")
j.arr_data = arr_cache
j.arr_data[i] = NewJsnm(arr[i])
return j.arr_data[i]
}
func (j *Jsnm) ArrPath(path ...interface{}) *Jsnm {
if len(path) <= 0 {
return j
}
switch reflect.TypeOf(path[0]).Kind() {
case reflect.String:
return j.Get(path[0].(string)).ArrPath(path[1:]...)
case reflect.Int:
return j.ArrLoc(path[0].(int)).ArrPath(path[1:]...)
}
return nil
}
func (j *Jsnm) ArrGet(path ...string) *Jsnm {
if len(path) <= 0 {
return j
}
if strings.HasSuffix(path[0], "\"") {
path_0 := strings.Trim(path[0], "\"")
return j.Get(path_0).ArrGet(path[1:]...)
}
var i byte
if len(path[0]) > 0 {
i = path[0][0]
}
if i >= 48 && i <= 57 {
loc, err2 := strconv.ParseInt(path[0], 10, 64)
if err2 != nil {
fmt.Println(err2)
}
return j.ArrLoc(int(loc)).ArrGet(path[1:]...)
}
return j.Get(path[0]).ArrGet(path[1:]...)
}
func (j *Jsnm) String() string {
if nil == j {
return ""
}
return j.RawData().String()
}