-
Notifications
You must be signed in to change notification settings - Fork 8
/
value_encoders.go
680 lines (511 loc) · 13.9 KB
/
value_encoders.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
// Copyright 2016 Russ Olsen. All Rights Reserved.
//
// This code is a Go port of the Java version created and maintained by Cognitect, therefore:
//
// Copyright 2014 Cognitect. All Rights Reserved.
//
// Licensed 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 transit
import (
"container/list"
"fmt"
"github.com/pborman/uuid"
"github.com/shopspring/decimal"
"log"
"math"
"math/big"
"net/url"
"reflect"
"time"
)
// ValueEncoder is the interface for objects that know how to
// transit encode a single value.
type ValueEncoder interface {
IsStringable(reflect.Value) bool
Encode(e Encoder, value reflect.Value, asString bool) error
}
type NilEncoder struct{}
func NewNilEncoder() *NilEncoder {
return &NilEncoder{}
}
func (ie NilEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie NilEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
return e.emitter.EmitNil(asKey)
}
type RuneEncoder struct{}
func NewRuneEncoder() *RuneEncoder {
return &RuneEncoder{}
}
func (ie RuneEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie RuneEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
r := v.Interface().(rune)
return e.emitter.EmitString(fmt.Sprintf("~c%c", r), asKey)
}
type PointerEncoder struct{}
func NewPointerEncoder() *PointerEncoder {
return &PointerEncoder{}
}
func (ie PointerEncoder) IsStringable(v reflect.Value) bool {
return false
}
func (ie PointerEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
//log.Println("*** Defer pointer to:", v.Elem())
return e.EncodeInterface(v.Elem().Interface(), asKey)
}
type UuidEncoder struct{}
func NewUuidEncoder() *UuidEncoder {
return &UuidEncoder{}
}
func (ie UuidEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie UuidEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
u := v.Interface().(uuid.UUID)
return e.emitter.EmitString(fmt.Sprintf("~u%v", u.String()), asKey)
}
type TimeEncoder struct{}
func NewTimeEncoder() *TimeEncoder {
return &TimeEncoder{}
}
func (ie TimeEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie TimeEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
t := v.Interface().(time.Time)
nanos := t.UnixNano()
millis := nanos / int64(1000000)
//millis := t.Unix() * 1000
return e.emitter.EmitString(fmt.Sprintf("~m%d", millis), asKey)
}
type BoolEncoder struct{}
func NewBoolEncoder() *BoolEncoder {
return &BoolEncoder{}
}
func (ie BoolEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie BoolEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
b := v.Bool()
return e.emitter.EmitBool(b, asKey)
}
type FloatEncoder struct{}
func NewFloatEncoder() *FloatEncoder {
return &FloatEncoder{}
}
func (ie FloatEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie FloatEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
f := v.Float()
if math.IsNaN(f) {
return e.emitter.EmitString("~zNaN", asKey)
} else if math.IsInf(f, 1) {
return e.emitter.EmitString("~zINF", asKey)
} else if math.IsInf(f, -1) {
return e.emitter.EmitString("~z-INF", asKey)
} else {
return e.emitter.EmitFloat(f, asKey)
}
}
type DecimalEncoder struct{}
func NewDecimalEncoder() *DecimalEncoder {
return &DecimalEncoder{}
}
func (ie DecimalEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie DecimalEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
f := v.Interface().(decimal.Decimal)
return e.emitter.EmitString(fmt.Sprintf("~f%v", f.String()), asKey)
}
type BigFloatEncoder struct{}
func NewBigFloatEncoder() *BigFloatEncoder {
return &BigFloatEncoder{}
}
func (ie BigFloatEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie BigFloatEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
f := v.Interface().(big.Float)
return e.emitter.EmitString(fmt.Sprintf("~f%v", f.Text('f', 25)), asKey)
}
type BigIntEncoder struct{}
func NewBigIntEncoder() *BigIntEncoder {
return &BigIntEncoder{}
}
func (ie BigIntEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie BigIntEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
i := v.Interface().(big.Int)
return e.emitter.EmitString(fmt.Sprintf("~n%v", i.String()), asKey)
}
type BigRatEncoder struct{}
func NewBigRatEncoder() *BigRatEncoder {
return &BigRatEncoder{}
}
func (ie BigRatEncoder) IsStringable(v reflect.Value) bool {
return false
}
func (ie BigRatEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
r := v.Interface().(big.Rat)
e.emitter.EmitStartArray()
e.emitter.EmitTag("ratio")
e.emitter.EmitArraySeparator()
e.emitter.EmitStartArray()
e.Encode(r.Num())
e.emitter.EmitArraySeparator()
e.Encode(r.Denom())
e.emitter.EmitEndArray()
return e.emitter.EmitEndArray()
}
type IntEncoder struct{}
func NewIntEncoder() *IntEncoder {
return &IntEncoder{}
}
func (ie IntEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie IntEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
return e.emitter.EmitInt(v.Int(), asKey)
}
type UintEncoder struct{}
func NewUintEncoder() *UintEncoder {
return &UintEncoder{}
}
func (ie UintEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie UintEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
return e.emitter.EmitInt(int64(v.Uint()), asKey)
}
type KeywordEncoder struct{}
func NewKeywordEncoder() *KeywordEncoder {
return &KeywordEncoder{}
}
func (ie KeywordEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie KeywordEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
s := v.String()
//log.Println("Encoding keyword:", s)
return e.emitter.EmitString("~:"+s, true)
}
type SymbolEncoder struct{}
func NewSymbolEncoder() *SymbolEncoder {
return &SymbolEncoder{}
}
func (ie SymbolEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie SymbolEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
s := v.String()
//log.Println("Encoding symbol:", s)
return e.emitter.EmitString("~$"+s, true)
}
type StringEncoder struct{}
func NewStringEncoder() *StringEncoder {
return &StringEncoder{}
}
func (ie StringEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie StringEncoder) needsEscape(s string) bool {
if len(s) == 0 {
return false
}
firstCh := s[0:1]
return firstCh == start || firstCh == reserved || firstCh == sub
}
func (ie StringEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
s := v.String()
if ie.needsEscape(s) {
s = "~" + s
}
return e.emitter.EmitString(s, asKey)
}
type UrlEncoder struct{}
func NewUrlEncoder() *UrlEncoder {
return &UrlEncoder{}
}
func (ie UrlEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie UrlEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
u := v.Interface().(*url.URL)
us := u.String()
return e.emitter.EmitString(fmt.Sprintf("~r%s", us), asKey)
}
type TUriEncoder struct{}
func NewTUriEncoder() *TUriEncoder {
return &TUriEncoder{}
}
func (ie TUriEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie TUriEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
u := v.Interface().(*TUri)
return e.emitter.EmitString(fmt.Sprintf("~r%s", u.Value), asKey)
}
type ErrorEncoder struct{}
func NewErrorEncoder() *ErrorEncoder {
return &ErrorEncoder{}
}
func (ie ErrorEncoder) IsStringable(v reflect.Value) bool {
return true
}
func (ie ErrorEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
return NewTransitError("Dont know how to encode value", v)
}
type ArrayEncoder struct{}
func NewArrayEncoder() *ArrayEncoder {
return &ArrayEncoder{}
}
func (ie ArrayEncoder) IsStringable(v reflect.Value) bool {
return false
}
func (ie ArrayEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
e.emitter.EmitStartArray()
l := v.Len()
for i := 0; i < l; i++ {
if i > 0 {
e.emitter.EmitArraySeparator()
}
element := v.Index(i)
err := e.EncodeInterface(element.Interface(), asKey)
if err != nil {
return err
}
}
return e.emitter.EmitEndArray()
}
type MapEncoder struct {
verbose bool
}
func NewMapEncoder(verbose bool) *MapEncoder {
return &MapEncoder{verbose}
}
func (me MapEncoder) IsStringable(v reflect.Value) bool {
return false
}
func (me MapEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
keys := KeyValues(v)
if !me.allStringable(e, keys) {
return me.encodeCompositeMap(e, v)
} else if me.verbose {
return me.encodeVerboseMap(e, v)
} else {
return me.encodeNormalMap(e, v)
}
}
func (me MapEncoder) allStringable(e Encoder, keys []reflect.Value) bool {
for _, key := range keys {
valueEncoder := e.ValueEncoderFor(reflect.ValueOf(key.Interface()))
if !valueEncoder.IsStringable(key) {
return false
}
}
return true
}
func (me MapEncoder) encodeCompositeMap(e Encoder, v reflect.Value) error {
e.emitter.EmitStartArray()
e.emitter.EmitTag("cmap")
e.emitter.EmitArraySeparator()
e.emitter.EmitStartArray()
keys := KeyValues(v)
for i, key := range keys {
if i != 0 {
e.emitter.EmitArraySeparator()
}
err := e.EncodeValue(key, false)
if err != nil {
return err
}
e.emitter.EmitArraySeparator()
value := GetMapElement(v, key)
err = e.EncodeValue(value, false)
if err != nil {
return err
}
}
e.emitter.EmitEndArray()
return e.emitter.EmitEndArray()
}
func (me MapEncoder) encodeNormalMap(e Encoder, v reflect.Value) error {
//l := v.Len()
e.emitter.EmitStartArray()
e.emitter.EmitString("^ ", false)
keys := KeyValues(v)
for _, key := range keys {
e.emitter.EmitArraySeparator()
err := e.EncodeValue(key, true)
if err != nil {
return err
}
e.emitter.EmitArraySeparator()
value := GetMapElement(v, key)
err = e.EncodeValue(value, false)
if err != nil {
return err
}
}
return e.emitter.EmitEndArray()
}
func (me MapEncoder) encodeVerboseMap(e Encoder, v reflect.Value) error {
e.emitter.EmitStartMap()
keys := KeyValues(v)
for i, key := range keys {
if i != 0 {
e.emitter.EmitMapSeparator()
}
err := e.EncodeValue(key, true)
if err != nil {
return err
}
e.emitter.EmitKeySeparator()
value := GetMapElement(v, key)
err = e.EncodeValue(value, false)
if err != nil {
return err
}
}
return e.emitter.EmitEndMap()
}
type TaggedValueEncoder struct{}
func NewTaggedValueEncoder() *TaggedValueEncoder {
return &TaggedValueEncoder{}
}
func (ie TaggedValueEncoder) IsStringable(v reflect.Value) bool {
return false
}
func (ie TaggedValueEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
t := v.Interface().(TaggedValue)
e.emitter.EmitStartArray()
e.emitter.EmitTag(string(t.Tag))
e.emitter.EmitArraySeparator()
e.EncodeInterface(t.Value, asKey)
return e.emitter.EmitEndArray()
}
type SetEncoder struct{}
func NewSetEncoder() *SetEncoder {
return &SetEncoder{}
}
func (ie SetEncoder) IsStringable(v reflect.Value) bool {
return false
}
func (ie SetEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
s := v.Interface().(Set)
//log.Println("*** Encode set:", v)
//l := v.Len()
e.emitter.EmitStartArray()
e.emitter.EmitTag("set")
e.emitter.EmitArraySeparator()
e.emitter.EmitStartArray()
for i, element := range s.Contents {
if i != 0 {
e.emitter.EmitArraySeparator()
}
err := e.EncodeInterface(element, asKey)
if err != nil {
return err
}
}
e.emitter.EmitEndArray()
return e.emitter.EmitEndArray()
}
type ListEncoder struct{}
func NewListEncoder() *ListEncoder {
return &ListEncoder{}
}
func (ie ListEncoder) IsStringable(v reflect.Value) bool {
return false
}
func (ie ListEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
lst := v.Interface().(*list.List)
e.emitter.EmitStartArray()
e.emitter.EmitTag("list")
e.emitter.EmitArraySeparator()
e.emitter.EmitStartArray()
first := true
for element := lst.Front(); element != nil; element = element.Next() {
if first {
first = false
} else {
e.emitter.EmitArraySeparator()
}
err := e.EncodeInterface(element.Value, asKey)
if err != nil {
log.Println("ERROR", err)
return err
}
}
e.emitter.EmitEndArray()
return e.emitter.EmitEndArray()
}
type CMapEncoder struct{}
func NewCMapEncoder() *CMapEncoder {
return &CMapEncoder{}
}
func (ie CMapEncoder) IsStringable(v reflect.Value) bool {
return false
}
func (ie CMapEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
cmap := v.Interface().(*CMap)
//l := v.Len()
e.emitter.EmitStartArray()
e.emitter.EmitTag("cmap")
e.emitter.EmitArraySeparator()
e.emitter.EmitStartArray()
for i, entry := range cmap.Entries {
if i != 0 {
e.emitter.EmitArraySeparator()
}
err := e.EncodeInterface(entry.Key, false)
if err != nil {
return err
}
e.emitter.EmitArraySeparator()
err = e.EncodeInterface(entry.Value, false)
if err != nil {
return err
}
}
e.emitter.EmitEndArray()
return e.emitter.EmitEndArray()
}
type LinkEncoder struct{}
func NewLinkEncoder() *LinkEncoder {
return &LinkEncoder{}
}
func (ie LinkEncoder) IsStringable(v reflect.Value) bool {
return false
}
func (ie LinkEncoder) Encode(e Encoder, v reflect.Value, asKey bool) error {
link := v.Interface().(*Link)
e.emitter.EmitStartArray()
e.emitter.EmitTag("link")
e.emitter.EmitArraySeparator()
m := map[string]interface{}{
"href": link.Href,
"rel": link.Rel,
"name": link.Name,
"prompt": link.Prompt,
"render": link.Render,
}
e.Encode(m)
return e.emitter.EmitEndArray()
}