-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_impl.go
670 lines (605 loc) · 12.5 KB
/
object_impl.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*
AnyType Library for Go
Object (dictionary) implementation
*/
package anytype
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"strings"
"sync"
)
/*
Map object, a reference type. Contains a map.
Implements:
- field,
- Object.
*/
type object struct {
val map[string]field
ptr Object
}
/*
Object constructor.
Creates a new object.
Parameters:
- values... - any amount of key-value pairs to set after the object creation.
Returns:
- pointer to the created object.
*/
func NewObject(values ...any) Object {
ego := &object{val: make(map[string]field)}
ego.ptr = ego
ego.Set(values...)
return ego
}
/*
Object constructor.
Converts a map of supported types to an object.
Parameters:
- dict - original map.
Returns:
- created object.
*/
func NewObjectFrom(dict any) Object {
object := NewObject()
switch s := dict.(type) {
case map[string]any:
for key, value := range s {
object.Set(key, value)
}
case map[string]Object:
for key, value := range s {
object.Set(key, value)
}
case map[string]List:
for key, value := range s {
object.Set(key, value)
}
case map[string]string:
for key, value := range s {
object.Set(key, value)
}
case map[string]bool:
for key, value := range s {
object.Set(key, value)
}
case map[string]int:
for key, value := range s {
object.Set(key, value)
}
case map[string]float64:
for key, value := range s {
object.Set(key, value)
}
default:
panic("unsupported map type")
}
return object
}
/*
Asserts that the object is initialized.
*/
func (ego *object) assert() {
if ego == nil || ego.val == nil {
panic("object is not initialized")
}
}
/*
Defined in the field interface.
Acquires the value of the field, in this case a reference to the whole struct (Object is a reference type).
Returns:
- value of the field.
*/
func (ego *object) getVal() any {
return ego.ptr
}
/*
Defined in the field interface.
Creates a deep copy of the field, in this case a new object with identical fields.
Can be called recursively.
Returns:
- deep copy of the field.
*/
func (ego *object) copy() any {
obj := NewObject()
for key, value := range ego.val {
obj.Set(key, value.copy())
}
return obj
}
/*
Defined in the field interface.
Serializes the field into the JSON format, in this case prints all keys and their values.
Can be called recursively.
Returns:
- string representing serialized field.
*/
func (ego *object) serialize() string {
result := "{"
i := 0
for field, value := range ego.val {
result += fmt.Sprintf("%s:%s", strconv.Quote(field), value.serialize())
if i++; i < len(ego.val) {
result += ","
}
}
result += "}"
return result
}
/*
Defined in the field interface.
Checks if the content of the field is equal to the given field.
Can be called recursively.
Returns:
- true if the fields are equal, false otherwise.
*/
func (ego *object) isEqual(another any) bool {
obj, ok := another.(*object)
if !ok || ego.Count() != obj.Count() {
return false
}
for k := range ego.val {
if !ego.val[k].isEqual(obj.val[k]) {
return false
}
}
return true
}
func (ego *object) Init(ptr Object) {
ego.ptr = ptr
}
func (ego *object) Ego() Object {
return ego.ptr
}
func (ego *object) Set(values ...any) Object {
ego.assert()
length := len(values)
if length%2 != 0 {
panic("object fields have to be set as key-value pairs")
}
for i := 0; i < length; i += 2 {
name, ok := values[i].(string)
if !ok || name == "" {
panic("object key has to be a non-empty string")
}
ego.val[name] = parseVal(values[i+1])
}
return ego.ptr
}
func (ego *object) Unset(keys ...string) Object {
ego.assert()
for _, key := range keys {
if ego.val[key] == nil {
panic(fmt.Sprintf("object does not have a field '%s'", key))
}
delete(ego.val, key)
}
return ego.ptr
}
func (ego *object) Clear() Object {
ego.assert()
ego.val = make(map[string]field, 0)
return ego.ptr
}
func (ego *object) Get(key string) any {
ego.assert()
if ego.val[key] == nil {
panic(fmt.Sprintf("object does not have a field '%s'", key))
}
obj := ego.val[key]
switch obj.(type) {
case Object, List:
return obj
default:
return obj.getVal()
}
}
func (ego *object) GetObject(key string) Object {
o, ok := ego.Get(key).(*object)
if !ok {
panic("field is not an object")
}
return o
}
func (ego *object) GetList(key string) List {
o, ok := ego.Get(key).(*list)
if !ok {
panic("field is not a list")
}
return o
}
func (ego *object) GetString(key string) string {
o, ok := ego.Get(key).(string)
if !ok {
panic("field is not a string")
}
return o
}
func (ego *object) GetBool(key string) bool {
o, ok := ego.Get(key).(bool)
if !ok {
panic("field is not a bool")
}
return o
}
func (ego *object) GetInt(key string) int {
o, ok := ego.Get(key).(int)
if !ok {
panic("field is not an int")
}
return o
}
func (ego *object) GetFloat(key string) float64 {
o, ok := ego.Get(key).(float64)
if !ok {
panic("field is not a float")
}
return o
}
func (ego *object) TypeOf(key string) Type {
ego.assert()
if !ego.KeyExists(key) {
return TypeUndefined
}
switch ego.val[key].(type) {
case *atNil:
return TypeNil
case *atString:
return TypeString
case *atInt:
return TypeInt
case *atBool:
return TypeBool
case *atFloat:
return TypeFloat
case *object:
return TypeObject
case *list:
return TypeList
default:
panic("unknown field type")
}
}
func (ego *object) String() string {
ego.assert()
return ego.ptr.serialize()
}
func (ego *object) FormatString(indent int) string {
if indent < 0 || indent > 10 {
panic("invalid indentation")
}
buffer := new(bytes.Buffer)
json.Indent(buffer, []byte(ego.String()), "", strings.Repeat(" ", indent))
return buffer.String()
}
func (ego *object) Dict() map[string]any {
ego.assert()
dict := make(map[string]any, 0)
for key, value := range ego.val {
dict[key] = value.getVal()
}
return dict
}
func (ego *object) Keys() List {
keys := NewList()
for key := range ego.val {
keys.Add(key)
}
return keys
}
func (ego *object) Values() List {
values := NewList()
for _, value := range ego.val {
values.Add(value.getVal())
}
return values
}
func (ego *object) Clone() Object {
ego.assert()
return ego.ptr.copy().(*object)
}
func (ego *object) Count() int {
ego.assert()
return len(ego.val)
}
func (ego *object) Empty() bool {
return ego.ptr.Count() == 0
}
func (ego *object) Equals(another Object) bool {
ego.assert()
return ego.ptr.isEqual(another)
}
func (ego *object) Merge(another Object) Object {
ego.assert()
result := ego.Clone()
another.ForEach(func(key string, val any) {
result.Set(key, val)
})
return result
}
func (ego *object) Pluck(keys ...string) Object {
ego.assert()
result := NewObject()
for _, key := range keys {
result.Set(key, ego.Get(key))
}
return result
}
func (ego *object) Contains(value any) bool {
ego.assert()
for _, item := range ego.val {
switch item.(type) {
case Object, List:
if item == value {
return true
}
default:
if item.getVal() == value {
return true
}
}
}
return false
}
func (ego *object) KeyOf(value any) string {
ego.assert()
for key, item := range ego.val {
switch item.(type) {
case Object, List:
if item == value {
return key
}
default:
if item.getVal() == value {
return key
}
}
}
return ""
}
func (ego *object) KeyExists(key string) bool {
ego.assert()
_, ok := ego.val[key]
return ok
}
func (ego *object) ForEach(function func(string, any)) Object {
ego.assert()
for key, item := range ego.val {
function(key, item.getVal())
}
return ego.ptr
}
func (ego *object) ForEachValue(function func(any)) Object {
ego.assert()
for _, item := range ego.val {
function(item.getVal())
}
return ego.ptr
}
func (ego *object) ForEachObject(function func(Object)) Object {
ego.assert()
for _, item := range ego.val {
val, ok := item.getVal().(Object)
if ok {
function(val)
}
}
return ego.ptr
}
func (ego *object) ForEachList(function func(List)) Object {
ego.assert()
for _, item := range ego.val {
val, ok := item.getVal().(List)
if ok {
function(val)
}
}
return ego.ptr
}
func (ego *object) ForEachString(function func(string)) Object {
ego.assert()
for _, item := range ego.val {
val, ok := item.getVal().(string)
if ok {
function(val)
}
}
return ego.ptr
}
func (ego *object) ForEachBool(function func(bool)) Object {
ego.assert()
for _, item := range ego.val {
val, ok := item.getVal().(bool)
if ok {
function(val)
}
}
return ego.ptr
}
func (ego *object) ForEachInt(function func(int)) Object {
ego.assert()
for _, item := range ego.val {
val, ok := item.getVal().(int)
if ok {
function(val)
}
}
return ego.ptr
}
func (ego *object) ForEachFloat(function func(float64)) Object {
ego.assert()
for _, item := range ego.val {
val, ok := item.getVal().(float64)
if ok {
function(val)
}
}
return ego.ptr
}
func (ego *object) Map(function func(string, any) any) Object {
ego.assert()
result := NewObject()
for key, item := range ego.val {
result.Set(key, function(key, item.getVal()))
}
return result
}
func (ego *object) MapValues(function func(any) any) Object {
ego.assert()
result := NewObject()
for key, item := range ego.val {
result.Set(key, function(item.getVal()))
}
return result
}
func (ego *object) MapObjects(function func(Object) any) Object {
ego.assert()
result := NewObject()
for key, item := range ego.val {
val, ok := item.(Object)
if ok {
result.Set(key, function(val))
}
}
return result
}
func (ego *object) MapLists(function func(List) any) Object {
ego.assert()
result := NewObject()
for key, item := range ego.val {
val, ok := item.(List)
if ok {
result.Set(key, function(val))
}
}
return result
}
func (ego *object) MapStrings(function func(string) any) Object {
ego.assert()
result := NewObject()
for key, item := range ego.val {
val, ok := item.getVal().(string)
if ok {
result.Set(key, function(val))
}
}
return result
}
func (ego *object) MapInts(function func(int) any) Object {
ego.assert()
result := NewObject()
for key, item := range ego.val {
val, ok := item.getVal().(int)
if ok {
result.Set(key, function(val))
}
}
return result
}
func (ego *object) MapFloats(function func(float64) any) Object {
ego.assert()
result := NewObject()
for key, item := range ego.val {
val, ok := item.getVal().(float64)
if ok {
result.Set(key, function(val))
}
}
return result
}
func (ego *object) ForEachAsync(function func(string, any)) Object {
ego.assert()
var wg sync.WaitGroup
step := func(group *sync.WaitGroup, k string, x any) {
function(k, x)
group.Done()
}
wg.Add(ego.Count())
for key, item := range ego.val {
go step(&wg, key, item.getVal())
}
wg.Wait()
return ego.ptr
}
func (ego *object) MapAsync(function func(string, any) any) Object {
ego.assert()
var wg sync.WaitGroup
var mutex sync.Mutex
wg.Add(ego.Count())
result := NewObject()
step := func(group *sync.WaitGroup, k string, x any) {
mutex.Lock()
result.Set(k, function(k, x))
mutex.Unlock()
group.Done()
}
for key, item := range ego.val {
go step(&wg, key, item.getVal())
}
wg.Wait()
return result
}
func (ego *object) GetTF(tf string) any {
ego.assert()
if tf[0] != '.' || len(tf) < 2 {
panic(fmt.Sprintf("'%s' is not a valid tree form", tf))
}
tf = tf[1:]
dot := strings.Index(tf, ".")
hash := strings.Index(tf, "#")
if dot > 0 && (hash < 0 || dot < hash) {
key := tf[:dot]
if !ego.ptr.KeyExists(key) || ego.ptr.TypeOf(key) != TypeObject {
return nil
}
return ego.ptr.GetObject(key).GetTF(tf[dot:])
}
if hash > 0 && (dot < 0 || hash < dot) {
key := tf[:hash]
if !ego.ptr.KeyExists(key) || ego.ptr.TypeOf(key) != TypeList {
return nil
}
return ego.ptr.GetList(key).GetTF(tf[hash:])
}
if !ego.ptr.KeyExists(tf) {
return nil
}
return ego.ptr.Get(tf)
}
func (ego *object) SetTF(tf string, value any) Object {
ego.assert()
if tf[0] != '.' || len(tf) < 2 {
panic(fmt.Sprintf("'%s' is not a valid tree form", tf))
}
tf = tf[1:]
dot := strings.Index(tf, ".")
hash := strings.Index(tf, "#")
if dot > 0 && (hash < 0 || dot < hash) {
key := tf[:dot]
var object Object
if ego.KeyExists(key) {
object = ego.GetObject(key)
} else {
object = NewObject()
ego.Set(key, object)
}
object.SetTF(tf[dot:], value)
return ego.ptr
}
if hash > 0 && (dot < 0 || hash < dot) {
key := tf[:hash]
var list List
if ego.KeyExists(key) {
list = ego.GetList(key)
} else {
list = NewList()
ego.Set(key, list)
}
list.SetTF(tf[hash:], value)
return ego.ptr
}
return ego.ptr.Set(tf, value)
}