-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.go
366 lines (334 loc) · 7.99 KB
/
object.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package djson
import (
"bytes"
"errors"
"fmt"
)
type PairEachable interface {
Each(func(k []byte, val Value) bool)
}
type Object interface {
PairEachable
Set(k []byte, val Value)
Get(k []byte) Value
Has(k []byte) bool
Del(k []byte)
Copy() Object
Total() int
}
type object struct {
pairs []*pair
*CallableRegister
}
type pair struct {
key []byte
val Value
}
func (p *pair) copy() *pair {
r := &pair{}
r.key = make([]byte, len(p.key))
copy(r.key, p.key)
r.val = p.val.Copy()
return r
}
var _ Object = &object{}
func NewObject(pairs ...*pair) *object {
obj := &object{pairs: pairs, CallableRegister: NewCallableRegister("object")}
obj.RegisterCall("map", setObject)
obj.RegisterCall("trans", transObject)
obj.RegisterCall("replace", replaceObject)
obj.RegisterCall("del", delObject)
obj.RegisterCall("filter", getObject)
return obj
}
func setObject(val Value, nexter TokenScanner, vars Context) (ret Value, err error) {
r := val.Value.(Object).Copy()
err = eachObjectItemForSet(val, nexter, vars, func(k []byte, val Value) error {
r.Set(k, val)
return nil
})
ret = Value{Type: ValueObject, Value: r}
return
}
func replaceObject(caller Value, nexter TokenScanner, vars Context) (ret Value, err error) {
r := caller.Value.(Object).Copy()
err = eachObjectItemForSet(caller, nexter, vars, func(k []byte, val Value) error {
if val.Type != ValueObject {
return errors.New("replace only support a object as Value")
}
r.Del(k)
obj := val.Value.(Object)
obj.Each(func(k []byte, val Value) bool {
r.Set(k, val)
return true
})
return nil
})
ret = Value{Type: ValueObject, Value: r}
return
}
func transObject(caller Value, scanner TokenScanner, ctx Context) (ret Value, err error) {
scanner.PushEnds(TokenParenthesesClose)
defer scanner.PopEnds(TokenParenthesesClose)
o := caller.Copy()
ctx.pushMe(o)
defer ctx.popMe()
stmt := NewStmtExecutor(scanner, ctx)
if err = stmt.Execute(); err != nil {
return
}
ret = o
return
}
func getObject(caller Value, nexter TokenScanner, vars Context) (ret Value, err error) {
no := NewObject()
err = eachObjectItem(caller, nexter, vars, func(k []byte, val Value) error {
no.Set(k, val)
return nil
})
ret = Value{Value: no, Type: ValueObject}
return
}
func delObject(caller Value, nexter TokenScanner, vars Context) (ret Value, err error) {
r := caller.Value.(Object).Copy()
err = eachObjectItem(caller, nexter, vars, func(k []byte, val Value) error {
r.Del(k)
return nil
})
ret = ObjectValue(r)
return
}
func eachObjectItemForSet(caller Value, nexter TokenScanner, ctx Context, handle func(k []byte, val Value) error) (err error) {
scanner := NewCachedTokenScanner(nexter)
scanner.PushEnds(TokenParenthesesClose)
defer scanner.PopEnds(TokenParenthesesClose)
stmt := NewStmtExecutor(scanner, ctx)
ctx.pushMe(caller)
defer ctx.popMe()
caller.Value.(PairEachable).Each(func(k []byte, val Value) bool {
scanner.ResetRead()
ctx.Assign([]byte{'k'}, StringValue(k...))
ctx.Assign([]byte{'v'}, val)
if err = stmt.Execute(); err != nil {
return false
}
if stmt.Exited() {
Exit()
}
p := stmt.Value()
if p.Type == ValueNull {
return true
}
return handle(k, p) == nil
})
return
}
func eachObjectItem(caller Value, nexter TokenScanner, ctx Context, handle func(k []byte, val Value) error) (err error) {
scanner := NewCachedTokenScanner(nexter)
nexter.PushEnds(TokenParenthesesClose)
defer nexter.PopEnds(TokenParenthesesClose)
stmt := NewStmtExecutor(scanner, ctx)
ctx.pushMe(caller)
defer ctx.popMe()
caller.Value.(PairEachable).Each(func(k []byte, val Value) bool {
scanner.ResetRead()
ctx.Assign([]byte{'k'}, StringValue(k...))
ctx.Assign([]byte{'v'}, val)
if err = stmt.Execute(); err != nil {
return false
}
if stmt.Exited() {
Exit()
}
if !stmt.value.Bool() {
return true
}
return handle(k, val) == nil
})
return
}
func (obj *object) Add(val Value) (ret Value, err error) {
if val.Type != ValueObject {
err = fmt.Errorf("object can't + a [%s]", val.TypeName())
return
}
r := obj.Copy()
val.Value.(Object).Each(func(k []byte, v Value) bool {
r.Set(k, v)
return true
})
ret = Value{Type: ValueObject, Value: r}
return
}
func (obj *object) Minus(val Value) (ret Value, err error) {
r := obj.Copy()
switch val.Type {
case ValueObject:
val.Value.(Object).Each(func(k []byte, val Value) bool {
it := r.Get(k)
if val.Equal(it) {
r.Del(k)
}
return true
})
case ValueArray:
val.Value.(Array).Each(func(i int, val Value) bool {
switch val.Type {
case ValueString:
it := r.Get(val.Value.(String).Bytes())
if it.Type != ValueNull {
r.Del(val.Value.(String).Bytes())
}
}
return true
})
default:
err = fmt.Errorf("object can't - a [%s]", val.TypeName())
return
}
ret = ObjectValue(r)
return
}
func (obj *object) Multiply(val Value) (ret Value, err error) {
err = fmt.Errorf("object can't * a [%s]", val.TypeName())
return
}
func (obj *object) Devide(val Value) (ret Value, err error) {
err = fmt.Errorf("object can't / a [%s]", val.TypeName())
return
}
func (obj *object) Compare(val Value) (ret int, err error) {
if val.Type != ValueObject {
err = fmt.Errorf("object can't compare with [%s]", val.TypeName())
return
}
rr := val.Value.(Object)
if obj.Total() > rr.Total() {
return 1, nil
} else if obj.Total() < rr.Total() {
return -1, nil
}
var c int
obj.Each(func(k []byte, val Value) bool {
c, err = val.Compare(rr.Get(k))
return err == nil && c != 0
})
return c, err
}
func (obj *object) Copy() Object {
r := NewObject()
r.pairs = make([]*pair, len(obj.pairs))
for i, p := range obj.pairs {
r.pairs[i] = p.copy()
}
return r
}
func (obj *object) Get(k []byte) Value {
for _, p := range obj.pairs {
if bytes.Equal(p.key, k) {
return p.val
}
}
return Value{Type: ValueNull}
}
func (obj *object) Total() int {
return len(obj.pairs)
}
func (obj *object) Has(k []byte) bool {
for _, p := range obj.pairs {
if bytes.Equal(p.key, k) {
return true
}
}
return false
}
func (obj *object) Set(k []byte, val Value) {
for i, p := range obj.pairs {
if bytes.Equal(p.key, k) {
obj.pairs[i] = &pair{key: k, val: val}
return
}
}
obj.pairs = append(obj.pairs, &pair{key: k, val: val})
}
func (obj *object) Del(k []byte) {
for i, p := range obj.pairs {
if bytes.Equal(p.key, k) {
obj.pairs = append(obj.pairs[:i], obj.pairs[i+1:]...)
return
}
}
}
func (obj *object) Each(handle func(k []byte, val Value) bool) {
for _, p := range obj.pairs {
if !handle(p.key, p.val) {
break
}
}
}
type objectExecutor struct {
scanner TokenScanner
ctx Context
value Value
}
func newObjectExecutor(scanner TokenScanner, vars Context) *objectExecutor {
return &objectExecutor{scanner: scanner, ctx: vars}
}
func (e *objectExecutor) execute() (err error) {
e.value, err = e.pairs()
return
}
func (e *objectExecutor) pairs() (ret Value, err error) {
val := NewObject()
ret = ObjectValue(val)
stmt := NewStmtExecutor(e.scanner, e.ctx)
e.ctx.pushMe(ObjectValue(val))
defer e.ctx.popMe()
for {
err = stmt.Execute(For(NullValue()), EndWhen(TokenColon))
if stmt.Exited() {
Exit()
}
if err != nil || stmt.value.Type == ValueNull {
return
}
if stmt.value.Type != ValueString {
err = fmt.Errorf("object key [%v] must be string", stmt.value.Value)
return
}
key := stmt.value.Value.(String).Bytes()
stmt.Execute(For(NullValue()), EndWhen(TokenComma, TokenBraceClose))
if stmt.Exited() {
Exit()
}
if err != nil {
return
}
val.Set(key, stmt.value)
if e.scanner.EndAt() == TokenBraceClose {
return
}
}
}
func (obj *object) lookup(k []byte) Value {
i, r := splitKeyAndRest(k)
if !bytes.Equal(i, []byte{'*'}) {
val := obj.Get(i)
if val.Type == ValueNull || len(r) == 0 {
return val
}
return val.lookup(r)
}
arr := NewArray()
for _, p := range obj.pairs {
if len(r) == 0 {
arr.items = append(arr.items, p.val)
continue
}
item := p.val.lookup(r)
if item.Type != ValueNull {
arr.items = append(arr.items, item)
}
}
return Value{Type: ValueArray, Value: arr}
}