-
Notifications
You must be signed in to change notification settings - Fork 5
/
union_test.go
215 lines (189 loc) · 9 KB
/
union_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
package goavro_test
import (
"bytes"
"fmt"
"math"
"strconv"
"testing"
"github.com/karrick/goavro"
)
func TestSchemaUnion(t *testing.T) {
testSchemaInvalid(t, `[{"type":"enum","name":"e1","symbols":["alpha","bravo"]},"e1"]`, "Union item 2 ought to be unique type")
testSchemaInvalid(t, `[{"type":"enum","name":"com.example.one","symbols":["red","green","blue"]},{"type":"enum","name":"one","namespace":"com.example","symbols":["dog","cat"]}]`, "Union item 2 ought to be unique type")
}
func TestUnion(t *testing.T) {
testBinaryCodecPass(t, `["null"]`, goavro.Union("null", nil), []byte("\x00"))
testBinaryCodecPass(t, `["null","int"]`, goavro.Union("null", nil), []byte("\x00"))
testBinaryCodecPass(t, `["int","null"]`, goavro.Union("null", nil), []byte("\x02"))
testBinaryCodecPass(t, `["null","int"]`, goavro.Union("int", 3), []byte("\x02\x06"))
testBinaryCodecPass(t, `["null","long"]`, goavro.Union("long", 3), []byte("\x02\x06"))
testBinaryCodecPass(t, `["int","null"]`, goavro.Union("int", 3), []byte("\x00\x06"))
testBinaryEncodePass(t, `["int","null"]`, goavro.Union("int", 3), []byte("\x00\x06")) // can encode a bare 3
testBinaryEncodeFail(t, `[{"type":"enum","name":"colors","symbols":["red","green","blue"]},{"type":"enum","name":"animals","symbols":["dog","cat"]}]`, goavro.Union("colors", "bravo"), "value ought to be member of symbols")
testBinaryEncodeFail(t, `[{"type":"enum","name":"colors","symbols":["red","green","blue"]},{"type":"enum","name":"animals","symbols":["dog","cat"]}]`, goavro.Union("animals", "bravo"), "value ought to be member of symbols")
testBinaryCodecPass(t, `[{"type":"enum","name":"colors","symbols":["red","green","blue"]},{"type":"enum","name":"animals","symbols":["dog","cat"]}]`, goavro.Union("colors", "green"), []byte{0, 2})
testBinaryCodecPass(t, `[{"type":"enum","name":"colors","symbols":["red","green","blue"]},{"type":"enum","name":"animals","symbols":["dog","cat"]}]`, goavro.Union("animals", "cat"), []byte{2, 2})
}
func TestUnionRejectInvalidType(t *testing.T) {
testBinaryEncodeFailBadDatumType(t, `["null","long"]`, 3)
testBinaryEncodeFailBadDatumType(t, `["null","int","long","float"]`, float64(3.5))
testBinaryEncodeFailBadDatumType(t, `["null","long"]`, goavro.Union("int", 3))
testBinaryEncodeFailBadDatumType(t, `["null","int","long","float"]`, goavro.Union("double", float64(3.5)))
}
func TestUnionWillCoerceTypeIfPossible(t *testing.T) {
testBinaryCodecPass(t, `["null","long","float","double"]`, goavro.Union("long", int32(3)), []byte("\x02\x06"))
testBinaryCodecPass(t, `["null","int","float","double"]`, goavro.Union("int", int64(3)), []byte("\x02\x06"))
testBinaryCodecPass(t, `["null","int","long","double"]`, goavro.Union("double", float32(3.5)), []byte("\x06\x00\x00\x00\x00\x00\x00\f@"))
testBinaryCodecPass(t, `["null","int","long","float"]`, goavro.Union("float", float64(3.5)), []byte("\x06\x00\x00\x60\x40"))
}
func TestUnionNumericCoercionGuardsPrecision(t *testing.T) {
testBinaryEncodeFail(t, `["null","int","long","double"]`, goavro.Union("int", float32(3.5)), "lose precision")
}
func TestUnionWithArray(t *testing.T) {
testBinaryCodecPass(t, `["null",{"type":"array","items":"int"}]`, goavro.Union("null", nil), []byte("\x00"))
testBinaryCodecPass(t, `["null",{"type":"array","items":"int"}]`, goavro.Union("array", []interface{}{}), []byte("\x02\x00"))
testBinaryCodecPass(t, `["null",{"type":"array","items":"int"}]`, goavro.Union("array", []interface{}{1}), []byte("\x02\x02\x02\x00"))
testBinaryCodecPass(t, `["null",{"type":"array","items":"int"}]`, goavro.Union("array", []interface{}{1, 2}), []byte("\x02\x04\x02\x04\x00"))
testBinaryCodecPass(t, `[{"type": "array", "items": "string"}, "null"]`, goavro.Union("null", nil), []byte{2})
testBinaryCodecPass(t, `[{"type": "array", "items": "string"}, "null"]`, goavro.Union("array", []string{"foo"}), []byte("\x00\x02\x06foo\x00"))
testBinaryCodecPass(t, `[{"type": "array", "items": "string"}, "null"]`, goavro.Union("array", []string{"foo", "bar"}), []byte("\x00\x04\x06foo\x06bar\x00"))
}
func TestUnionWithMap(t *testing.T) {
testBinaryCodecPass(t, `["null",{"type":"map","values":"string"}]`, goavro.Union("null", nil), []byte("\x00"))
testBinaryCodecPass(t, `["string",{"type":"map","values":"string"}]`, goavro.Union("map", map[string]interface{}{"He": "Helium"}), []byte("\x02\x02\x04He\x0cHelium\x00"))
testBinaryCodecPass(t, `["string",{"type":"array","items":"string"}]`, goavro.Union("string", "Helium"), []byte("\x00\x0cHelium"))
}
func TestUnionMapRecordFitsInRecord(t *testing.T) {
// union value may be either map or a record
codec, err := goavro.NewCodec(`["null",{"type":"map","values":"double"},{"type":"record","name":"com.example.record","fields":[{"name":"field1","type":"int"},{"name":"field2","type":"float"}]}]`)
if err != nil {
t.Fatal(err)
}
// the provided datum value could be encoded by either the map or the record
// schemas above
datum := map[string]interface{}{
"field1": 3,
"field2": 3.5,
}
datumIn := goavro.Union("com.example.record", datum)
buf, err := codec.BinaryFromNative(nil, datumIn)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(buf, []byte{
0x04, // prefer record (union item 2) over map (union item 1)
0x06, // field1 == 3
0x00, 0x00, 0x60, 0x40, // field2 == 3.5
}) {
t.Errorf("Actual: %#v; Expected: %#v", buf, []byte{byte(2)})
}
// round trip
datumOut, buf, err := codec.NativeFromBinary(buf)
if err != nil {
t.Fatal(err)
}
if actual, expected := len(buf), 0; actual != expected {
t.Errorf("Actual: %#v; Expected: %#v", actual, expected)
}
datumOutMap, ok := datumOut.(map[string]interface{})
if !ok {
t.Fatalf("Actual: %#v; Expected: %#v", ok, false)
}
if actual, expected := len(datumOutMap), 1; actual != expected {
t.Fatalf("Actual: %#v; Expected: %#v", actual, expected)
}
datumValue, ok := datumOutMap["com.example.record"]
if !ok {
t.Fatalf("Actual: %#v; Expected: %#v", datumOutMap, "have `com.example.record` key")
}
datumValueMap, ok := datumValue.(map[string]interface{})
if !ok {
t.Errorf("Actual: %#v; Expected: %#v", ok, true)
}
if actual, expected := len(datumValueMap), len(datum); actual != expected {
t.Errorf("Actual: %#v; Expected: %#v", actual, expected)
}
for k, v := range datum {
if actual, expected := fmt.Sprintf("%v", datumValueMap[k]), fmt.Sprintf("%v", v); actual != expected {
t.Errorf("Actual: %#v; Expected: %#v", actual, expected)
}
}
}
func TestUnionRecordFieldWhenNull(t *testing.T) {
schema := `{
"type": "record",
"name": "r1",
"fields": [
{"name": "f1", "type": [{"type": "array", "items": "string"}, "null"]}
]
}`
testBinaryCodecPass(t, schema, map[string]interface{}{"f1": goavro.Union("array", []interface{}{})}, []byte("\x00\x00"))
testBinaryCodecPass(t, schema, map[string]interface{}{"f1": goavro.Union("array", []string{"bar"})}, []byte("\x00\x02\x06bar\x00"))
testBinaryCodecPass(t, schema, map[string]interface{}{"f1": goavro.Union("array", []string{})}, []byte("\x00\x00"))
testBinaryCodecPass(t, schema, map[string]interface{}{"f1": goavro.Union("null", nil)}, []byte("\x02"))
testBinaryCodecPass(t, schema, map[string]interface{}{"f1": nil}, []byte("\x02"))
}
func TestUnionText(t *testing.T) {
testTextEncodeFail(t, `["null","int"]`, goavro.Union("null", 3), "expected")
testTextCodecPass(t, `["null","int"]`, goavro.Union("null", nil), []byte("null"))
testTextCodecPass(t, `["null","int"]`, goavro.Union("int", 3), []byte(`{"int":3}`))
testTextCodecPass(t, `["null","int","string"]`, goavro.Union("string", "😂 "), []byte(`{"string":"\u0001\uD83D\uDE02 "}`))
}
func ExampleUnion() {
codec, err := goavro.NewCodec(`["null","string","int"]`)
if err != nil {
fmt.Println(err)
}
buf, err := codec.TextualFromNative(nil, goavro.Union("string", "some string"))
if err != nil {
fmt.Println(err)
}
fmt.Println(string(buf))
// Output: {"string":"some string"}
}
func ExampleUnion3() {
// Imagine a record field with the following union type. I have seen this
// sort of type in many schemas. I have been told the reasoning behind it is
// when the writer desires to encode data to JSON that cannot be written as
// a JSON number, then to encode it as a string and allow the reader to
// parse the string accordingly.
codec, err := goavro.NewCodec(`["null","double","string"]`)
if err != nil {
fmt.Println(err)
}
native, _, err := codec.NativeFromTextual([]byte(`{"string":"NaN"}`))
if err != nil {
fmt.Println(err)
}
value := math.NaN()
if native == nil {
fmt.Print("decoded null: ")
} else {
for k, v := range native.(map[string]interface{}) {
switch k {
case "double":
fmt.Print("decoded double: ")
value = v.(float64)
case "string":
fmt.Print("decoded string: ")
s := v.(string)
switch s {
case "NaN":
value = math.NaN()
case "+Infinity":
value = math.Inf(1)
case "-Infinity":
value = math.Inf(-1)
default:
var err error
value, err = strconv.ParseFloat(s, 64)
if err != nil {
fmt.Println(err)
}
}
}
}
}
fmt.Println(value)
// Output: decoded string: NaN
}