This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
forked from matttproud/thrift4go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmap.go
770 lines (742 loc) · 15.9 KB
/
tmap.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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package thrift
import (
"bytes"
"container/list"
"reflect"
)
/**
* Helper class that encapsulates map metadata.
*
*/
type TMap interface {
KeyType() TType
ValueType() TType
Len() int
Set(key, value interface{})
Get(key interface{}) (interface{}, bool)
Contains(key interface{}) bool
Iter() <-chan TMapElem
KeyIter() <-chan interface{}
ValueIter() <-chan interface{}
Keys() []interface{}
Values() []interface{}
Less(other interface{}) bool
Equals(other interface{}) bool
CompareTo(other interface{}) (int, bool)
}
type TMapElem interface {
Key() interface{}
Value() interface{}
}
type tMap struct {
keyType TType
valueType TType
size int
l *list.List
b map[bool]interface{}
i08 map[byte]interface{}
i16 map[int16]interface{}
i32 map[int32]interface{}
i64 map[int64]interface{}
f64 map[float64]interface{}
s map[string]interface{}
}
type tMapElem struct {
key interface{}
value interface{}
}
func (p *tMapElem) Key() interface{} {
return p.key
}
func (p *tMapElem) Value() interface{} {
return p.value
}
func NewTMapElem(k, v interface{}) TMapElem {
return &tMapElem{key: k, value: v}
}
func NewTMap(k, v TType, s int) TMap {
return &tMap{keyType: k, valueType: v, size: s, l: list.New()}
}
func NewTMapDefault() TMap {
return NewTMap(STOP, STOP, 0)
}
func (p *tMap) KeyType() TType {
return p.keyType
}
func (p *tMap) ValueType() TType {
return p.valueType
}
func (p *tMap) Len() int {
if p.l.Len() != 0 {
return p.l.Len()
}
switch p.KeyType() {
case STOP, VOID:
return 0
case BOOL:
return len(p.b)
case BYTE:
return len(p.i08)
case I16:
return len(p.i16)
case I32:
return len(p.i32)
case I64:
return len(p.i64)
case DOUBLE:
return len(p.f64)
case STRING, UTF8, UTF16:
return len(p.s)
default:
return p.size
}
return p.size
}
func (p *tMap) Get(key interface{}) (interface{}, bool) {
if p.KeyType().IsEmptyType() {
return nil, false
}
if key == nil {
for elem := p.l.Front(); elem != nil; elem = elem.Next() {
e := elem.Value.(TMapElem)
k := e.Key()
if k == nil {
return e.Value(), true
}
}
return nil, false
}
useKey, ok := p.KeyType().CoerceData(key)
if !ok {
return nil, false
}
switch p.KeyType() {
case STOP, VOID:
// if here, then we don't have a key type yet and key is not nil
// so this is pretty much an empty map
return nil, false
case BOOL:
m := p.b
if m == nil {
return nil, false
}
if v, ok := m[useKey.(bool)]; ok {
return v, true
}
return nil, true
case BYTE:
m := p.i08
if v, ok := m[useKey.(byte)]; ok {
return v, true
}
return nil, false
case DOUBLE:
m := p.f64
if m == nil {
return nil, false
}
if v, ok := m[useKey.(float64)]; ok {
return v, true
}
return nil, false
case I16:
m := p.i16
if m == nil {
return nil, false
}
if v, ok := m[useKey.(int16)]; ok {
return v, true
}
return nil, false
case I32:
m := p.i32
if m == nil {
return nil, false
}
if v, ok := m[useKey.(int32)]; ok {
return v, true
}
return nil, false
case I64:
m := p.i64
if m == nil {
return nil, false
}
if v, ok := m[useKey.(int64)]; ok {
return v, true
}
return nil, false
case STRING, UTF8, UTF16:
// TODO(pomack) properly handle ENUM
m := p.s
if m == nil {
return nil, false
}
if v, ok := m[useKey.(string)]; ok {
return v, true
}
return nil, false
case BINARY:
for elem := p.l.Front(); elem != nil; elem = elem.Next() {
e := elem.Value.(TMapElem)
k := e.Key()
if k == nil {
continue
}
binkey, ok := k.([]byte)
if ok {
if bytes.Compare(binkey, useKey.([]byte)) == 0 {
return e.Value(), true
}
continue
}
if reflect.DeepEqual(useKey, k) {
return e.Value(), true
}
}
return nil, false
case STRUCT:
for elem := p.l.Front(); elem != nil; elem = elem.Next() {
e := elem.Value.(TMapElem)
k := e.Key()
if k == nil {
continue
}
structkey, ok := k.(TStruct)
if ok {
if structkey.Equals(useKey.(TStruct)) {
return e.Value(), true
}
continue
}
if reflect.DeepEqual(useKey, k) {
return e.Value(), true
}
}
return nil, false
case MAP:
for elem := p.l.Front(); elem != nil; elem = elem.Next() {
e := elem.Value.(TMapElem)
k := e.Key()
if k == nil {
continue
}
mapkey, ok := k.(TMap)
if ok {
if mapkey.Equals(useKey.(TMap)) {
return e.Value(), true
}
continue
}
if reflect.DeepEqual(useKey, k) {
return e.Value(), true
}
}
return nil, false
case SET:
for elem := p.l.Front(); elem != nil; elem = elem.Next() {
e := elem.Value.(TMapElem)
k := e.Key()
if k == nil {
continue
}
setkey, ok := k.(TSet)
if ok {
if setkey.Equals(useKey.(TSet)) {
return e.Value(), true
}
continue
}
if reflect.DeepEqual(useKey, k) {
return e.Value(), true
}
}
return nil, false
case LIST:
for elem := p.l.Front(); elem != nil; elem = elem.Next() {
e := elem.Value.(TMapElem)
k := e.Key()
if k == nil {
continue
}
listkey, ok := k.(TList)
if ok {
if listkey.Equals(useKey.(TList)) {
return e.Value(), true
}
continue
}
if reflect.DeepEqual(useKey, k) {
return e.Value(), true
}
}
return nil, false
default:
panic("Invalid Thrift element type")
}
return nil, false
}
func (p *tMap) Set(key, value interface{}) {
if p.KeyType() == STOP || p.KeyType() == VOID {
p.keyType = TypeFromValue(key)
}
coercedKey, ok := p.KeyType().CoerceData(key)
if !ok {
return
}
if p.ValueType() == STOP || p.ValueType() == VOID {
p.valueType = TypeFromValue(value)
}
coercedValue, ok := p.ValueType().CoerceData(value)
if !ok {
return
}
newElem := NewTMapElem(coercedKey, coercedValue)
if !p.KeyType().IsBaseType() || p.KeyType() == BINARY {
for elem := p.l.Front(); elem != nil; elem = elem.Next() {
k := elem.Value.(TMapElem).Key()
if cmp, ok := p.KeyType().Compare(coercedKey, k); ok && cmp >= 0 {
if cmp == 0 {
p.l.InsertAfter(newElem, elem)
p.l.Remove(elem)
return
}
p.l.InsertBefore(newElem, elem)
return
}
}
p.l.PushBack(newElem)
return
}
if key == nil {
return
}
switch p.KeyType() {
case STOP, VOID:
// if here, then we don't have a key type yet and key is not nil
// so this is pretty much an empty map
return
case BOOL:
if p.b == nil {
p.b = make(map[bool]interface{})
}
b := coercedKey.(bool)
p.b[b] = value
case BYTE:
if p.i08 == nil {
p.i08 = make(map[byte]interface{})
}
b := coercedKey.(byte)
p.i08[b] = value
case DOUBLE:
if p.f64 == nil {
p.f64 = make(map[float64]interface{})
}
b := coercedKey.(float64)
p.f64[b] = value
case I16:
if p.i16 == nil {
p.i16 = make(map[int16]interface{})
}
b := coercedKey.(int16)
p.i16[b] = value
case I32:
if p.i32 == nil {
p.i32 = make(map[int32]interface{})
}
b := coercedKey.(int32)
p.i32[b] = value
case I64:
if p.i64 == nil {
p.i64 = make(map[int64]interface{})
}
b := coercedKey.(int64)
p.i64[b] = value
case STRING, UTF8, UTF16:
if p.s == nil {
p.s = make(map[string]interface{})
}
b := coercedKey.(string)
p.s[b] = value
case BINARY, STRUCT, MAP, SET, LIST:
panic("Should never be here because type is " + p.KeyType().String())
default:
panic("Should never be here because type is " + p.KeyType().String())
}
}
func (p *tMap) Contains(key interface{}) bool {
coercedKey, ok := p.KeyType().CoerceData(key)
if !ok {
return false
}
if coercedKey == nil {
for elem := p.l.Front(); elem != nil; elem = elem.Next() {
k := elem.Value.(TMapElem).Key()
if k == nil {
return true
}
}
return false
}
if !ok {
return false
}
switch p.KeyType() {
case STOP:
// if here, then we don't have a key type yet and key is not nil
// so this is pretty much an empty map
return false
case VOID:
// if here, then we don't have a key type yet and key is not nil
// so this is pretty much an empty map
return false
case BOOL:
m := p.b
if m == nil {
return false
}
_, ok := m[coercedKey.(bool)]
return ok
case BYTE:
m := p.i08
_, ok := m[coercedKey.(byte)]
return ok
case DOUBLE:
m := p.f64
if m == nil {
return false
}
_, ok := m[coercedKey.(float64)]
return ok
case I16:
m := p.i16
if m == nil {
return false
}
_, ok := m[coercedKey.(int16)]
return ok
case I32:
m := p.i32
if m == nil {
return false
}
_, ok := m[coercedKey.(int32)]
return ok
case I64:
m := p.i64
if m == nil {
return false
}
_, ok := m[coercedKey.(int64)]
return ok
case STRING, UTF8, UTF16:
// TODO(pomack) properly handle ENUM
m := p.s
if m == nil {
return false
}
_, ok := m[coercedKey.(string)]
return ok
case BINARY:
for elem := p.l.Front(); elem != nil; elem = elem.Next() {
e := elem.Value.(TMapElem)
k := e.Key()
if k == nil {
continue
}
binkey, ok := k.([]byte)
if ok {
if bytes.Compare(binkey, coercedKey.([]byte)) == 0 {
return true
}
continue
}
if reflect.DeepEqual(coercedKey, k) {
return true
}
}
return false
case STRUCT:
for elem := p.l.Front(); elem != nil; elem = elem.Next() {
e := elem.Value.(TMapElem)
k := e.Key()
if k == nil {
continue
}
structkey, ok := k.(TStruct)
if ok {
if structkey.Equals(coercedKey.(TStruct)) {
return true
}
continue
}
if reflect.DeepEqual(coercedKey, k) {
return true
}
}
return false
case MAP:
for elem := p.l.Front(); elem != nil; elem = elem.Next() {
e := elem.Value.(TMapElem)
k := e.Key()
if k == nil {
continue
}
mapkey, ok := k.(TMap)
if ok {
if mapkey.Equals(coercedKey.(TMap)) {
return true
}
continue
}
}
return false
case SET:
for elem := p.l.Front(); elem != nil; elem = elem.Next() {
e := elem.Value.(TMapElem)
k := e.Key()
if k == nil {
continue
}
setkey, ok := k.(TSet)
if ok {
if setkey.Equals(coercedKey.(TSet)) {
return true
}
continue
}
}
return false
case LIST:
for elem := p.l.Front(); elem != nil; elem = elem.Next() {
e := elem.Value.(TMapElem)
k := e.Key()
if k == nil {
continue
}
listkey, ok := k.(TList)
if ok {
if listkey.Equals(coercedKey.(TList)) {
return true
}
continue
}
}
return false
default:
panic("Invalid Thrift element type")
}
return false
}
// Iterate over all elements; driver for range
func (p *tMap) iterate(c chan<- TMapElem) {
switch p.KeyType() {
case STOP, VOID:
close(c)
case BOOL:
for k, v := range p.b {
c <- NewTMapElem(k, v)
}
close(c)
case BYTE:
for k, v := range p.i08 {
c <- NewTMapElem(k, v)
}
close(c)
case I16:
for k, v := range p.i16 {
c <- NewTMapElem(k, v)
}
close(c)
case I32:
for k, v := range p.i32 {
c <- NewTMapElem(k, v)
}
close(c)
case I64:
for k, v := range p.i64 {
c <- NewTMapElem(k, v)
}
close(c)
case DOUBLE:
for k, v := range p.f64 {
c <- NewTMapElem(k, v)
}
close(c)
case STRING, UTF8, UTF16:
for k, v := range p.s {
c <- NewTMapElem(k, v)
}
close(c)
case BINARY, STRUCT, LIST, SET:
for v := p.l.Front(); v != nil; v = v.Next() {
c <- v.Value.(TMapElem)
}
close(c)
default:
panic("Invalid Thrift type")
}
}
// Channel iterator for range.
func (p *tMap) Iter() <-chan TMapElem {
c := make(chan TMapElem)
go p.iterate(c)
return c
}
// Iterate over all keys; driver for range
func (p *tMap) iterateKeys(c chan<- interface{}) {
switch p.KeyType() {
case STOP, VOID:
close(c)
case BOOL:
for k, _ := range p.b {
c <- k
}
close(c)
case BYTE:
for k, _ := range p.i08 {
c <- k
}
close(c)
case I16:
for k, _ := range p.i16 {
c <- k
}
close(c)
case I32:
for k, _ := range p.i32 {
c <- k
}
close(c)
case I64:
for k, _ := range p.i64 {
c <- k
}
close(c)
case DOUBLE:
for k, _ := range p.f64 {
c <- k
}
close(c)
case STRING, UTF8, UTF16:
for k, _ := range p.s {
c <- k
}
close(c)
case BINARY, STRUCT, LIST, SET:
for v := p.l.Front(); v != nil; v = v.Next() {
c <- v.Value.(TMapElem).Key()
}
close(c)
default:
panic("Invalid Thrift type")
}
}
func (p *tMap) KeyIter() <-chan interface{} {
c := make(chan interface{})
go p.iterateKeys(c)
return c
}
// Iterate over all values; driver for range
func (p *tMap) iterateValues(c chan<- interface{}) {
switch p.KeyType() {
case STOP, VOID:
close(c)
case BOOL:
for _, v := range p.b {
c <- v
}
close(c)
case BYTE:
for _, v := range p.i08 {
c <- v
}
close(c)
case I16:
for _, v := range p.i16 {
c <- v
}
close(c)
case I32:
for _, v := range p.i32 {
c <- v
}
close(c)
case I64:
for _, v := range p.i64 {
c <- v
}
close(c)
case DOUBLE:
for _, v := range p.f64 {
c <- v
}
close(c)
case STRING, UTF8, UTF16:
for _, v := range p.s {
c <- v
}
close(c)
case BINARY, STRUCT, LIST, SET:
for v := p.l.Front(); v != nil; v = v.Next() {
c <- v.Value.(TMapElem).Value()
}
close(c)
default:
panic("Invalid Thrift type")
}
}
func (p *tMap) ValueIter() <-chan interface{} {
c := make(chan interface{})
go p.iterateValues(c)
return c
}
func (p *tMap) Less(other interface{}) bool {
cmp, ok := p.CompareTo(other)
return ok && cmp > 0
}
func (p *tMap) Equals(other interface{}) bool {
c, cok := p.CompareTo(other)
return cok && c == 0
}
func (p *tMap) CompareTo(other interface{}) (int, bool) {
return TType(MAP).Compare(p, other)
}
func (p *tMap) Keys() []interface{} {
size := p.Len()
values := make([]interface{}, size, size)
i := 0
for k := range p.KeyIter() {
values[i] = k
i++
}
return values
}
func (p *tMap) Values() []interface{} {
size := p.Len()
values := make([]interface{}, size, size)
i := 0
for v := range p.ValueIter() {
values[i] = v
i++
}
return values
}