forked from linkedin/goavro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
record_test.go
719 lines (645 loc) · 22.2 KB
/
record_test.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
// Copyright [2019] LinkedIn Corp. 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.
package goavro
import (
"bytes"
"fmt"
"testing"
)
func TestRecordName(t *testing.T) {
testSchemaInvalid(t, `{"type":"record","fields":[{"name":"name","type":"string"},{"name":"age","type":"int"}]}`, "Record ought to have valid name: schema ought to have name key")
testSchemaInvalid(t, `{"type":"record","name":3}`, "Record ought to have valid name: schema name ought to be non-empty string")
testSchemaInvalid(t, `{"type":"record","name":""}`, "Record ought to have valid name: schema name ought to be non-empty string")
testSchemaInvalid(t, `{"type":"record","name":"&foo","fields":[{"name":"name","type":"string"},{"name":"age","type":"int"}]}`, "Record ought to have valid name: schema name ought to start with")
testSchemaInvalid(t, `{"type":"record","name":"foo&","fields":[{"name":"name","type":"string"},{"name":"age","type":"int"}]}`, "Record ought to have valid name: schema name ought to have second and remaining")
}
func TestRecordFields(t *testing.T) {
testSchemaInvalid(t, `{"type":"record","name":"r1"}`, `Record "r1" ought to have fields key`)
testSchemaInvalid(t, `{"type":"record","name":"r1","fields":3}`, `Record "r1" fields ought to be non-nil array`)
testSchemaInvalid(t, `{"type":"record","name":"r1","fields":null}`, `Record "r1" fields ought to be non-nil array`)
}
func TestRecordFieldInvalid(t *testing.T) {
testSchemaInvalid(t, `{"type":"record","name":"r1","fields":[3]}`, `Record "r1" field 1 ought to be valid Avro named type`)
testSchemaInvalid(t, `{"type":"record","name":"r1","fields":[""]}`, `Record "r1" field 1 ought to be valid Avro named type`)
testSchemaInvalid(t, `{"type":"record","name":"r1","fields":[{}]}`, `Record "r1" field 1 ought to be valid Avro named type`)
testSchemaInvalid(t, `{"type":"record","name":"r1","fields":[{"type":"int"}]}`, `Record "r1" field 1 ought to have valid name`)
testSchemaInvalid(t, `{"type":"record","name":"r1","fields":[{"name":"f1"}]}`, `Record "r1" field 1 ought to be valid Avro named type`)
testSchemaInvalid(t, `{"type":"record","name":"r1","fields":[{"name":"f1","type":"integer"}]}`, `Record "r1" field 1 ought to be valid Avro named type`)
testSchemaInvalid(t, `{"type":"record","name":"r1","fields":[{"name":"f1","type":"int"},{"name":"f1","type":"long"}]}`, `Record "r1" field 2 ought to have unique name`)
}
func TestSchemaRecord(t *testing.T) {
testSchemaValid(t, `{
"name": "person",
"type": "record",
"fields": [
{
"name": "height",
"type": "long"
},
{
"name": "weight",
"type": "long"
},
{
"name": "name",
"type": "string"
}
]
}`)
}
func TestSchemaRecordFieldWithDefaults(t *testing.T) {
testSchemaValid(t, `{
"name": "person",
"type": "record",
"fields": [
{
"name": "height",
"type": "long"
},
{
"name": "weight",
"type": "long"
},
{
"name": "name",
"type": "string"
},
{
"name": "hacker",
"type": "boolean",
"default": false
}
]
}`)
}
func TestRecordDecodedEmptyBuffer(t *testing.T) {
testBinaryDecodeFailShortBuffer(t, `{"type":"record","name":"foo","fields":[{"name":"field1","type":"int"}]}`, nil)
}
func TestRecordFieldTypeHasPrimitiveName(t *testing.T) {
codec, err := NewCodec(`{
"type": "record",
"name": "r1",
"namespace": "com.example",
"fields": [
{
"name": "f1",
"type": "string"
},
{
"name": "f2",
"type": {
"type": "int"
}
}
]
}`)
ensureError(t, err)
datumIn := map[string]interface{}{
"f1": "thirteen",
"f2": 13,
}
buf, err := codec.BinaryFromNative(nil, datumIn)
ensureError(t, err)
if expected := []byte{
0x10, // field1 size = 8
't', 'h', 'i', 'r', 't', 'e', 'e', 'n',
0x1a, // field2 == 13
}; !bytes.Equal(buf, expected) {
t.Errorf("GOT: %#v; WANT: %#v", buf, expected)
}
// round trip
datumOut, buf, err := codec.NativeFromBinary(buf)
ensureError(t, err)
if actual, expected := len(buf), 0; actual != expected {
t.Errorf("GOT: %#v; WANT: %#v", actual, expected)
}
datumOutMap, ok := datumOut.(map[string]interface{})
if !ok {
t.Errorf("GOT: %#v; WANT: %#v", ok, true)
}
if actual, expected := len(datumOutMap), len(datumIn); actual != expected {
t.Errorf("GOT: %#v; WANT: %#v", actual, expected)
}
for k, v := range datumIn {
if actual, expected := fmt.Sprintf("%v", datumOutMap[k]), fmt.Sprintf("%v", v); actual != expected {
t.Errorf("GOT: %#v; WANT: %#v", actual, expected)
}
}
}
func TestSchemaRecordRecursive(t *testing.T) {
testSchemaValid(t, `{
"type": "record",
"name": "recursive",
"fields": [
{
"name": "label",
"type": "string"
},
{
"name": "children",
"type": {
"type": "array",
"items": "recursive"
}
}
]
}`)
}
func TestSchemaNamespaceRecursive(t *testing.T) {
testSchemaValid(t, `{
"type": "record",
"name": "Container",
"namespace": "namespace1",
"fields": [
{
"name": "contained",
"type": {
"type": "record",
"name": "MutuallyRecursive",
"fields": [
{
"name": "label",
"type": "string"
},
{
"name": "children",
"type": {
"type": "array",
"items": {
"type": "record",
"name": "MutuallyRecursive",
"namespace": "namespace2",
"fields": [
{
"name": "value",
"type": "int"
},
{
"name": "children",
"type": {
"type": "array",
"items": "namespace1.MutuallyRecursive"
}
},
{
"name": "morechildren",
"type": {
"type": "array",
"items": "MutuallyRecursive"
}
}
]
}
}
},
{
"name": "anotherchild",
"type": "namespace2.MutuallyRecursive"
}
]
}
}
]
}`)
}
func TestSchemaRecordNamespaceComposite(t *testing.T) {
testSchemaValid(t, `{
"type": "record",
"namespace": "x",
"name": "Y",
"fields": [
{
"name": "e",
"type": {
"type": "record",
"name": "Z",
"fields": [
{
"name": "f",
"type": "x.Z"
}
]
}
}
]
}`)
}
func TestSchemaRecordNamespaceFullName(t *testing.T) {
testSchemaValid(t, `{
"type": "record",
"name": "x.Y",
"fields": [
{
"name": "e",
"type": {
"type": "record",
"name": "Z",
"fields": [
{
"name": "f",
"type": "x.Y"
},
{
"name": "g",
"type": "x.Z"
}
]
}
}
]
}`)
}
func TestSchemaRecordNamespaceEnum(t *testing.T) {
testSchemaValid(t, `{"type": "record", "name": "org.apache.avro.tests.Hello", "fields": [
{"name": "f1", "type": {"type": "enum", "name": "MyEnum", "symbols": ["Foo", "Bar", "Baz"]}},
{"name": "f2", "type": "org.apache.avro.tests.MyEnum"},
{"name": "f3", "type": "MyEnum"},
{"name": "f4", "type": {"type": "enum", "name": "other.namespace.OtherEnum", "symbols": ["one", "two", "three"]}},
{"name": "f5", "type": "other.namespace.OtherEnum"},
{"name": "f6", "type": {"type": "enum", "name": "ThirdEnum", "namespace": "some.other", "symbols": ["Alice", "Bob"]}},
{"name": "f7", "type": "some.other.ThirdEnum"}
]}`)
}
func TestSchemaRecordNamespaceFixed(t *testing.T) {
testSchemaValid(t, `{"type": "record", "name": "org.apache.avro.tests.Hello", "fields": [
{"name": "f1", "type": {"type": "fixed", "name": "MyFixed", "size": 16}},
{"name": "f2", "type": "org.apache.avro.tests.MyFixed"},
{"name": "f3", "type": "MyFixed"},
{"name": "f4", "type": {"type": "fixed", "name": "other.namespace.OtherFixed", "size": 18}},
{"name": "f5", "type": "other.namespace.OtherFixed"},
{"name": "f6", "type": {"type": "fixed", "name": "ThirdFixed", "namespace": "some.other", "size": 20}},
{"name": "f7", "type": "some.other.ThirdFixed"}
]}`)
}
func TestRecordNamespace(t *testing.T) {
c, err := NewCodec(`{
"type": "record",
"name": "org.foo.Y",
"fields": [
{
"name": "X",
"type": {
"type": "fixed",
"size": 4,
"name": "fixed_4"
}
},
{
"name": "Z",
"type": {
"type": "fixed_4"
}
}
]
}`)
ensureError(t, err)
datumIn := map[string]interface{}{
"X": []byte("abcd"),
"Z": []byte("efgh"),
}
buf, err := c.BinaryFromNative(nil, datumIn)
ensureError(t, err)
if expected := []byte("abcdefgh"); !bytes.Equal(buf, expected) {
t.Errorf("GOT: %#v; WANT: %#v", buf, expected)
}
// round trip
datumOut, buf, err := c.NativeFromBinary(buf)
ensureError(t, err)
if actual, expected := len(buf), 0; actual != expected {
t.Errorf("GOT: %#v; WANT: %#v", actual, expected)
}
datumOutMap, ok := datumOut.(map[string]interface{})
if !ok {
t.Errorf("GOT: %#v; WANT: %#v", ok, true)
}
if actual, expected := len(datumOutMap), len(datumIn); actual != expected {
t.Errorf("GOT: %#v; WANT: %#v", actual, expected)
}
for k, v := range datumIn {
if actual, expected := fmt.Sprintf("%s", datumOutMap[k]), fmt.Sprintf("%s", v); actual != expected {
t.Errorf("GOT: %#v; WANT: %#v", actual, expected)
}
}
}
func TestRecordEncodeFail(t *testing.T) {
schema := `{
"type": "record",
"name": "r1",
"fields": [
{"name": "f1", "type": "string"},
{"name": "f2", "type": "string"}
]
}`
testBinaryEncodeFail(t, schema, map[string]interface{}{"f1": "foo"}, `field "f2": schema does not specify default value and no value provided`)
testBinaryEncodeFail(t, schema, map[string]interface{}{"f1": "foo", "f2": 13}, `field "f2": value does not match its schema`)
}
func TestRecordTextDecodeFail(t *testing.T) {
schema := `{"name":"r1","type":"record","fields":[{"name":"string","type":"string"},{"name":"bytes","type":"bytes"}]}`
testTextDecodeFail(t, schema, []byte(` "string" : "silly" , "bytes" : "silly" } `), "expected: '{'")
testTextDecodeFail(t, schema, []byte(` { 16 : "silly" , "bytes" : "silly" } `), "expected initial \"")
testTextDecodeFail(t, schema, []byte(` { "badName" : "silly" , "bytes" : "silly" } `), "cannot determine codec")
testTextDecodeFail(t, schema, []byte(` { "string" , "silly" , "bytes" : "silly" } `), "expected: ':'")
testTextDecodeFail(t, schema, []byte(` { "string" : 13 , "bytes" : "silly" } `), "expected initial \"")
testTextDecodeFail(t, schema, []byte(` { "string" : "silly" : "bytes" : "silly" } `), "expected ',' or '}'")
testTextDecodeFail(t, schema, []byte(` { "string" : "silly" , "bytes" : "silly" `), "short buffer")
testTextDecodeFail(t, schema, []byte(` { "string" : "silly" `), "short buffer")
testTextDecodeFail(t, schema, []byte(` { "string" : "silly" } `), "only found 1 of 2 fields")
}
func TestRecordTextCodecPass(t *testing.T) {
silly := "⌘ "
testTextEncodePass(t, `{"name":"r1","type":"record","fields":[{"name":"string","type":"string"}]}`, map[string]interface{}{"string": silly}, []byte(`{"string":"\u0001\u2318 "}`))
testTextEncodePass(t, `{"name":"r1","type":"record","fields":[{"name":"bytes","type":"bytes"}]}`, map[string]interface{}{"bytes": []byte(silly)}, []byte(`{"bytes":"\u0001\u00E2\u008C\u0098 "}`))
testTextDecodePass(t, `{"name":"r1","type":"record","fields":[{"name":"string","type":"string"},{"name":"bytes","type":"bytes"}]}`, map[string]interface{}{"string": silly, "bytes": []byte(silly)}, []byte(` { "string" : "\u0001\u2318 " , "bytes" : "\u0001\u00E2\u008C\u0098 " }`))
}
func TestRecordFieldDefaultValue(t *testing.T) {
testSchemaValid(t, `{"type":"record","name":"r1","fields":[{"name":"f1","type":"int","default":13}]}`)
testSchemaValid(t, `{"type":"record","name":"r1","fields":[{"name":"f1","type":"string","default":"foo"}]}`)
testSchemaInvalid(t,
`{"type":"record","name":"r1","fields":[{"name":"f1","type":"int","default":"foo"}]}`,
"default value ought to encode using field schema")
}
func TestRecordFieldUnionDefaultValue(t *testing.T) {
testSchemaValid(t, `{"type":"record","name":"r1","fields":[{"name":"f1","type":["int","null"],"default":13}]}`)
testSchemaValid(t, `{"type":"record","name":"r1","fields":[{"name":"f1","type":["null","int"],"default":null}]}`)
}
func TestRecordFieldUnionInvalidDefaultValue(t *testing.T) {
testSchemaInvalid(t,
`{"type":"record","name":"r1","fields":[{"name":"f1","type":["null","int"],"default":13}]}`,
"default value ought to encode using field schema")
testSchemaInvalid(t,
`{"type":"record","name":"r1","fields":[{"name":"f1","type":["int","null"],"default":null}]}`,
"default value ought to encode using field schema")
}
func TestRecordRecursiveRoundTrip(t *testing.T) {
codec, err := NewCodec(`
{
"type": "record",
"name": "LongList",
"fields" : [
{"name": "next", "type": ["null", "LongList"], "default": null}
]
}
`)
ensureError(t, err)
// NOTE: May omit fields when using default value
initial := `{"next":{"LongList":{}}}`
// NOTE: Textual encoding will show all fields, even those with values that
// match their default values
final := `{"next":{"LongList":{"next":null}}}`
// Convert textual Avro data (in Avro JSON format) to native Go form
datum, _, err := codec.NativeFromTextual([]byte(initial))
ensureError(t, err)
// Convert native Go form to binary Avro data
buf, err := codec.BinaryFromNative(nil, datum)
ensureError(t, err)
// Convert binary Avro data back to native Go form
datum, _, err = codec.NativeFromBinary(buf)
ensureError(t, err)
// Convert native Go form to textual Avro data
buf, err = codec.TextualFromNative(nil, datum)
ensureError(t, err)
if actual, expected := string(buf), final; actual != expected {
t.Fatalf("GOT: %v; WANT: %v", actual, expected)
}
}
func ExampleCodec_NativeFromTextual_roundTrip() {
codec, err := NewCodec(`
{
"type": "record",
"name": "LongList",
"fields" : [
{"name": "next", "type": ["null", "LongList"], "default": null}
]
}
`)
if err != nil {
fmt.Println(err)
}
// NOTE: May omit fields when using default value
textual := []byte(`{"next":{"LongList":{"next":{"LongList":{}}}}}`)
// Convert textual Avro data (in Avro JSON format) to native Go form
native, _, err := codec.NativeFromTextual(textual)
if err != nil {
fmt.Println(err)
}
// Convert native Go form to binary Avro data
binary, err := codec.BinaryFromNative(nil, native)
if err != nil {
fmt.Println(err)
}
// Convert binary Avro data back to native Go form
native, _, err = codec.NativeFromBinary(binary)
if err != nil {
fmt.Println(err)
}
// Convert native Go form to textual Avro data
textual, err = codec.TextualFromNative(nil, native)
if err != nil {
fmt.Println(err)
}
// NOTE: Textual encoding will show all fields, even those with values that
// match their default values
fmt.Println(string(textual))
// Output: {"next":{"LongList":{"next":{"LongList":{"next":null}}}}}
}
func ExampleCodec_BinaryFromNative_avro() {
codec, err := NewCodec(`
{
"type": "record",
"name": "LongList",
"fields" : [
{"name": "next", "type": ["null", "LongList"], "default": null}
]
}
`)
if err != nil {
fmt.Println(err)
}
// Convert native Go form to binary Avro data
binary, err := codec.BinaryFromNative(nil, map[string]interface{}{
"next": map[string]interface{}{
"LongList": map[string]interface{}{
"next": map[string]interface{}{
"LongList": map[string]interface{}{
// NOTE: May omit fields when using default value
},
},
},
},
})
if err != nil {
fmt.Println(err)
}
fmt.Printf("%#v", binary)
// Output: []byte{0x2, 0x2, 0x0}
}
func ExampleCodec_NativeFromBinary_avro() {
codec, err := NewCodec(`
{
"type": "record",
"name": "LongList",
"fields" : [
{"name": "next", "type": ["null", "LongList"], "default": null}
]
}
`)
if err != nil {
fmt.Println(err)
}
// Convert native Go form to binary Avro data
binary := []byte{0x2, 0x2, 0x0}
native, _, err := codec.NativeFromBinary(binary)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%v", native)
// Output: map[next:map[LongList:map[next:map[LongList:map[next:<nil>]]]]]
}
func ExampleCodec_NativeFromTextual_avro() {
codec, err := NewCodec(`
{
"type": "record",
"name": "LongList",
"fields" : [
{"name": "next", "type": ["null", "LongList"], "default": null}
]
}
`)
if err != nil {
fmt.Println(err)
}
// Convert native Go form to text Avro data
text := []byte(`{"next":{"LongList":{"next":{"LongList":{"next":null}}}}}`)
native, _, err := codec.NativeFromTextual(text)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%v", native)
// Output: map[next:map[LongList:map[next:map[LongList:map[next:<nil>]]]]]
}
func ExampleCodec_TextualFromNative_avro() {
codec, err := NewCodec(`
{
"type": "record",
"name": "LongList",
"fields" : [
{"name": "next", "type": ["null", "LongList"], "default": null}
]
}
`)
if err != nil {
fmt.Println(err)
}
// Convert native Go form to text Avro data
text, err := codec.TextualFromNative(nil, map[string]interface{}{
"next": map[string]interface{}{
"LongList": map[string]interface{}{
"next": map[string]interface{}{
"LongList": map[string]interface{}{
// NOTE: May omit fields when using default value
},
},
},
},
})
if err != nil {
fmt.Println(err)
}
fmt.Printf("%s", text)
// Output: {"next":{"LongList":{"next":{"LongList":{"next":null}}}}}
}
func TestRecordFieldFixedDefaultValue(t *testing.T) {
testSchemaValid(t, `{"type": "record", "name": "r1", "fields":[{"name": "f1", "type": {"type": "fixed", "name": "someFixed", "size": 1}, "default": "\u0000"}]}`)
}
func TestRecordFieldDefaultValueTypes(t *testing.T) {
t.Run("success", func(t *testing.T) {
codec, err := NewCodec(`{"type": "record", "name": "r1", "fields":[{"name": "someBoolean", "type": "boolean", "default": true},{"name": "someBytes", "type": "bytes", "default": "0"},{"name": "someDouble", "type": "double", "default": 0},{"name": "someFloat", "type": "float", "default": 0},{"name": "someInt", "type": "int", "default": 0},{"name": "someLong", "type": "long", "default": 0},{"name": "someString", "type": "string", "default": "0"}]}`)
ensureError(t, err)
r1, _, err := codec.NativeFromTextual([]byte("{}"))
ensureError(t, err)
r1m := r1.(map[string]interface{})
someBoolean := r1m["someBoolean"]
if _, ok := someBoolean.(bool); !ok {
t.Errorf("GOT: %T; WANT: []byte", someBoolean)
}
someBytes := r1m["someBytes"]
if _, ok := someBytes.([]byte); !ok {
t.Errorf("GOT: %T; WANT: []byte", someBytes)
}
someDouble := r1m["someDouble"]
if _, ok := someDouble.(float64); !ok {
t.Errorf("GOT: %T; WANT: float64", someDouble)
}
someFloat := r1m["someFloat"]
if _, ok := someFloat.(float32); !ok {
t.Errorf("GOT: %T; WANT: float32", someFloat)
}
someInt := r1m["someInt"]
if _, ok := someInt.(int32); !ok {
t.Errorf("GOT: %T; WANT: int32", someInt)
}
someLong := r1m["someLong"]
if _, ok := someLong.(int64); !ok {
t.Errorf("GOT: %T; WANT: int64", someLong)
}
someString := r1m["someString"]
if _, ok := someString.(string); !ok {
t.Errorf("GOT: %T; WANT: string", someString)
}
})
t.Run("provided default is wrong type", func(t *testing.T) {
t.Run("long", func(t *testing.T) {
_, err := NewCodec(`{"type": "record", "name": "r1", "fields":[{"name": "someLong", "type": "long", "default": "0"},{"name": "someInt", "type": "int", "default": 0},{"name": "someFloat", "type": "float", "default": 0},{"name": "someDouble", "type": "double", "default": 0}]}`)
ensureError(t, err, "field schema")
})
t.Run("int", func(t *testing.T) {
_, err := NewCodec(`{"type": "record", "name": "r1", "fields":[{"name": "someLong", "type": "long", "default": 0},{"name": "someInt", "type": "int", "default": "0"},{"name": "someFloat", "type": "float", "default": 0},{"name": "someDouble", "type": "double", "default": 0}]}`)
ensureError(t, err, "field schema")
})
t.Run("float", func(t *testing.T) {
_, err := NewCodec(`{"type": "record", "name": "r1", "fields":[{"name": "someLong", "type": "long", "default": 0},{"name": "someInt", "type": "int", "default": 0},{"name": "someFloat", "type": "float", "default": "0"},{"name": "someDouble", "type": "double", "default": 0}]}`)
ensureError(t, err, "field schema")
})
t.Run("double", func(t *testing.T) {
_, err := NewCodec(`{"type": "record", "name": "r1", "fields":[{"name": "someLong", "type": "long", "default": 0},{"name": "someInt", "type": "int", "default": 0},{"name": "someFloat", "type": "float", "default": 0},{"name": "someDouble", "type": "double", "default": "0"}]}`)
ensureError(t, err, "field schema")
})
})
t.Run("union of int and long", func(t *testing.T) {
t.Skip("FIXME: should encode default value as int64 rather than float64")
codec, err := NewCodec(`{"type":"record","name":"r1","fields":[{"name":"f1","type":["int","long"],"default":13}]}`)
ensureError(t, err)
r1, _, err := codec.NativeFromTextual([]byte("{}"))
ensureError(t, err)
r1m := r1.(map[string]interface{})
someUnion := r1m["f1"]
someMap, ok := someUnion.(map[string]interface{})
if !ok {
t.Fatalf("GOT: %T; WANT: map[string]interface{}", someUnion)
}
if got, want := len(someMap), 1; got != want {
t.Errorf("GOT: %v; WANT: %v", got, want)
}
t.Logf("someMap: %#v", someMap)
for k, v := range someMap {
// The "int" type is the first type option of the union.
if got, want := k, "int"; got != want {
t.Errorf("GOT: %v; WANT: %v", got, want)
}
switch tv := v.(type) {
case int64:
if got, want := tv, int64(13); got != want {
t.Errorf("GOT: %v; WANT: %v", got, want)
}
default:
t.Errorf("GOT: %T; WANT: int64", v)
}
}
})
}