-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharr.go
255 lines (239 loc) · 5.94 KB
/
arr.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package m2obj
import (
"regexp"
"strconv"
)
// arrCheckIndexFormat
//
// Likes *Object.arrCheckIndexKey but only match the format, no verifying on integer transform, no index overflow checking.
func arrCheckIndexFormat(key string) bool {
reg := regexp.MustCompile(`\[(\d+)]`)
return reg.MatchString(key)
}
// arrCheckIndexKey
//
// To get an element by index of an Array Object, the keyStr Must be formatted as this:
// xxx.ArrayName.[index].xxx
// It means that there must be an index statement quoted with '[' and ']' after an Array Object.
//
// This func checks off the rule above.
func (o *Object) arrCheckIndexKey(key, keyStr string) (index int, err error) {
reg := regexp.MustCompile(`\[(\d+)]`)
if !reg.MatchString(key) { // the key doesn't be matched as [number]
err = invalidTypeErr(keyStr)
return
} else { // matched
index, err = strconv.Atoi(reg.FindStringSubmatch(key)[1])
if err != nil { // the key can not trans to an Integer
err = invalidKeyStrErr(keyStr)
return
} else { // gotten an integer as the index
arr := *o.val.(*arrayData)
if index < 0 || len(arr) <= index { // the index overflows from the arr
err = indexOverflowErr{
Index: index,
}
return
} else { // no error, check passed
return index, nil
}
}
}
}
// ArrPush **!!! ONLY FOR ARR OBJECT**
//
// Push a value (or an Object) back into the Array Object.
func (o *Object) ArrPush(value interface{}) {
switch o.val.(type) {
case *arrayData:
*o.val.(*arrayData) = append(*o.val.(*arrayData), New(value))
o.ArrGet(o.ArrLen() - 1).buildParentLink(o)
o.callOnChange()
default:
panic(invalidTypeErr(""))
}
}
// ArrPop **!!! ONLY FOR ARR OBJECT**
//
// Pop back from the Array Object.
func (o *Object) ArrPop() (value *Object) {
switch o.val.(type) {
case *arrayData:
value = o.ArrGet(o.ArrLen() - 1)
*o.val.(*arrayData) = (*o.val.(*arrayData))[:len(*o.val.(*arrayData))-1]
o.callOnChange()
default:
panic(invalidTypeErr(""))
}
return
}
// ArrSet **!!! ONLY FOR ARR OBJECT**
//
// Set the value of the element which indexed at the Array Object.
func (o *Object) ArrSet(index int, value interface{}) {
switch o.val.(type) {
case *arrayData:
(*o.val.(*arrayData))[index] = New(value)
o.ArrGet(index).buildParentLink(o)
o.callOnChange()
default:
panic(invalidTypeErr(""))
}
}
// ArrGet **!!! ONLY FOR ARR OBJECT**
//
// An alias of `MustGet("[index]")`
func (o *Object) ArrGet(index int) (obj *Object) {
switch o.val.(type) {
case *arrayData:
return (*o.val.(*arrayData))[index]
default:
panic(invalidTypeErr(""))
}
}
// ArrInsert **!!! ONLY FOR ARR OBJECT**
//
// Insert Into the index before the element which indexed at the Array Object.
//
// Specially, if `index == o.ArrLen()` , this is same as `o.Push(value)` but has a lower performance.
func (o *Object) ArrInsert(index int, value interface{}) {
switch o.val.(type) {
case *arrayData:
var (
arr, arrBefore, arrAfter, arrRes arrayData
)
arr = *o.val.(*arrayData)
// overflow
if index < 0 || index > len(arr) {
panic(indexOverflowErr{index})
}
// before
arrBefore = arrayData{}
if index > 0 {
arrBefore = append(arrBefore, arr[:index]...)
}
// after
arrAfter = arrayData{}
if index < len(arr) {
arrAfter = append(arrAfter, arr[index:]...)
}
// generate
arrRes = append(arrBefore, New(value))
arrRes = append(arrRes, arrAfter...)
*o.val.(*arrayData) = arrRes
o.ArrGet(index).buildParentLink(o)
o.callOnChange()
default:
panic(invalidTypeErr(""))
}
}
// ArrRemove **!!! ONLY FOR ARR OBJECT**
//
// Remove the element which indexed at the Array Object.
func (o *Object) ArrRemove(index int) {
switch o.val.(type) {
case *arrayData:
var (
arr, arrBefore, arrAfter, arrRes arrayData
)
arr = *o.val.(*arrayData)
// overflow
if index < 0 || index >= len(arr) {
panic(indexOverflowErr{index})
}
// before
arrBefore = arrayData{}
if index > 0 {
arrBefore = append(arrBefore, arr[:index]...)
}
// after
arrAfter = arrayData{}
if index < len(arr)-1 {
arrAfter = append(arrAfter, arr[index+1:]...)
}
// generate
arrRes = append(arrBefore, arrAfter...)
*o.val.(*arrayData) = arrRes
o.callOnChange()
default:
panic(invalidTypeErr(""))
}
}
// ArrShift **!!! ONLY FOR ARR OBJECT**
//
// An alias of `ArrRemove(index)`
func (o *Object) ArrShift() {
o.ArrRemove(0)
}
// ArrUnshift **!!! ONLY FOR ARR OBJECT**
//
// An alias of `ArrInsert(0, value)`
func (o *Object) ArrUnshift(value interface{}) {
o.ArrInsert(0, value)
}
// ArrForeach **!!! ONLY FOR ARR OBJECT**
//
// Loop for range `[0...o.ArrLen()-1]`, foreach calls `do`.
//
// Stops when do returns a non-nil err and return it.
func (o *Object) ArrForeach(do func(index int, obj *Object) error) (err error) {
switch o.val.(type) {
case *arrayData:
for i, obj := range *o.val.(*arrayData) {
if err = do(i, obj); err != nil {
break
}
}
o.buildParentLink(o.parent)
default:
panic(invalidTypeErr(""))
}
return
}
// ArrMerge **!!! ONLY FOR ARR OBJECT**
//
// Push all of elements from an Array Object o2 into the Array Object
func (o *Object) ArrMerge(o2 *Object) {
switch o.val.(type) {
case *arrayData:
switch o2.val.(type) {
case *arrayData: // Group
newArr := o.Clone()
err := o2.ArrForeach(func(index int, obj *Object) error {
newArr.ArrPush(obj)
return nil
})
if err == nil {
o.SetVal(newArr)
o.buildParentLink(o.parent)
}
default:
panic(invalidTypeErr(""))
}
default:
panic(invalidTypeErr(""))
}
}
// ArrPushAll **!!! ONLY FOR ARR OBJECT**
//
// Push all the elements in the parameter to the array object
func (o *Object) ArrPushAll(values ...interface{}) {
switch o.val.(type) {
case *arrayData:
o2 := New(Array(values))
o.ArrMerge(o2)
default:
panic(invalidTypeErr(""))
}
}
// ArrLen **!!! ONLY FOR ARR OBJECT**
//
// Get the length of an array object
func (o *Object) ArrLen() int {
switch o.val.(type) {
case *arrayData:
return len(*o.val.(*arrayData))
default:
panic(invalidTypeErr(""))
}
}