github.com/hamba/avro@v1.8.0/decoder_record_test.go (about) 1 package avro_test 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/hamba/avro" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestDecoder_RecordStruct(t *testing.T) { 12 defer ConfigTeardown() 13 14 data := []byte{0x36, 0x06, 0x66, 0x6f, 0x6f} 15 schema := `{ 16 "type": "record", 17 "name": "test", 18 "fields" : [ 19 {"name": "a", "type": "long"}, 20 {"name": "b", "type": "string"} 21 ] 22 }` 23 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 24 assert.NoError(t, err) 25 26 var got TestRecord 27 err = dec.Decode(&got) 28 29 assert.NoError(t, err) 30 assert.Equal(t, TestRecord{A: 27, B: "foo"}, got) 31 } 32 33 func TestDecoder_RecordStructPtr(t *testing.T) { 34 defer ConfigTeardown() 35 36 data := []byte{0x36, 0x06, 0x66, 0x6f, 0x6f} 37 schema := `{ 38 "type": "record", 39 "name": "test", 40 "fields" : [ 41 {"name": "a", "type": "long"}, 42 {"name": "b", "type": "string"} 43 ] 44 }` 45 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 46 assert.NoError(t, err) 47 48 got := &TestRecord{} 49 err = dec.Decode(&got) 50 51 assert.NoError(t, err) 52 assert.Equal(t, &TestRecord{A: 27, B: "foo"}, got) 53 } 54 55 func TestDecoder_RecordStructPtrNil(t *testing.T) { 56 defer ConfigTeardown() 57 58 data := []byte{0x36, 0x06, 0x66, 0x6f, 0x6f} 59 schema := `{ 60 "type": "record", 61 "name": "test", 62 "fields" : [ 63 {"name": "a", "type": "long"}, 64 {"name": "b", "type": "string"} 65 ] 66 }` 67 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 68 assert.NoError(t, err) 69 70 var got *TestRecord 71 err = dec.Decode(&got) 72 73 assert.NoError(t, err) 74 assert.Equal(t, &TestRecord{A: 27, B: "foo"}, got) 75 } 76 77 func TestDecoder_RecordPartialStruct(t *testing.T) { 78 defer ConfigTeardown() 79 80 data := []byte{0x36, 0x06, 0x66, 0x6f, 0x6f} 81 schema := `{ 82 "type": "record", 83 "name": "test", 84 "fields" : [ 85 {"name": "a", "type": "long"}, 86 {"name": "b", "type": "string"} 87 ] 88 }` 89 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 90 assert.NoError(t, err) 91 92 var got TestPartialRecord 93 err = dec.Decode(&got) 94 95 assert.NoError(t, err) 96 assert.Equal(t, TestPartialRecord{B: "foo"}, got) 97 } 98 99 func TestDecoder_RecordStructInvalidData(t *testing.T) { 100 defer ConfigTeardown() 101 102 data := []byte{0xE2, 0xA2, 0xF3, 0xAD, 0xAD, 0xAD, 0xE2, 0xA2, 0xF3, 0xAD, 0xAD, 0x06, 0x66, 0x6f, 0x6f} 103 schema := `{ 104 "type": "record", 105 "name": "test", 106 "fields" : [ 107 {"name": "a", "type": "long"}, 108 {"name": "b", "type": "string"} 109 ] 110 }` 111 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 112 assert.NoError(t, err) 113 114 var got TestRecord 115 err = dec.Decode(&got) 116 117 assert.Error(t, err) 118 } 119 120 func TestDecoder_RecordEmbeddedStruct(t *testing.T) { 121 defer ConfigTeardown() 122 123 data := []byte{0x36, 0x06, 0x66, 0x6f, 0x6f} 124 schema := `{ 125 "type": "record", 126 "name": "test", 127 "fields" : [ 128 {"name": "a", "type": "long"}, 129 {"name": "b", "type": "string"} 130 ] 131 }` 132 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 133 assert.NoError(t, err) 134 135 var got TestEmbeddedRecord 136 err = dec.Decode(&got) 137 138 assert.NoError(t, err) 139 assert.Equal(t, TestEmbeddedRecord{TestEmbed: TestEmbed{A: 27}, B: "foo"}, got) 140 } 141 142 func TestDecoder_RecordEmbeddedPtrStruct(t *testing.T) { 143 defer ConfigTeardown() 144 145 data := []byte{0x36, 0x06, 0x66, 0x6f, 0x6f} 146 schema := `{ 147 "type": "record", 148 "name": "test", 149 "fields" : [ 150 {"name": "a", "type": "long"}, 151 {"name": "b", "type": "string"} 152 ] 153 }` 154 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 155 assert.NoError(t, err) 156 157 var got TestEmbeddedPtrRecord 158 err = dec.Decode(&got) 159 160 assert.NoError(t, err) 161 assert.Equal(t, TestEmbeddedPtrRecord{TestEmbed: &TestEmbed{A: 27}, B: "foo"}, got) 162 } 163 164 func TestDecoder_RecordMap(t *testing.T) { 165 defer ConfigTeardown() 166 167 data := []byte{0x36, 0x06, 0x66, 0x6f, 0x6f} 168 schema := `{ 169 "type": "record", 170 "name": "test", 171 "fields" : [ 172 {"name": "a", "type": "long"}, 173 {"name": "b", "type": "string"} 174 ] 175 }` 176 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 177 assert.NoError(t, err) 178 179 var got map[string]interface{} 180 err = dec.Decode(&got) 181 182 assert.NoError(t, err) 183 assert.Equal(t, map[string]interface{}{"a": int64(27), "b": "foo"}, got) 184 } 185 186 func TestDecoder_RecordMapInvalidKey(t *testing.T) { 187 defer ConfigTeardown() 188 189 data := []byte{0x36, 0x06, 0x66, 0x6f, 0x6f} 190 schema := `{ 191 "type": "record", 192 "name": "test", 193 "fields" : [ 194 {"name": "a", "type": "long"}, 195 {"name": "b", "type": "string"} 196 ] 197 }` 198 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 199 assert.NoError(t, err) 200 201 var got map[int]interface{} 202 err = dec.Decode(&got) 203 204 assert.Error(t, err) 205 } 206 207 func TestDecoder_RecordMapInvalidElem(t *testing.T) { 208 defer ConfigTeardown() 209 210 data := []byte{0x36, 0x06, 0x66, 0x6f, 0x6f} 211 schema := `{ 212 "type": "record", 213 "name": "test", 214 "fields" : [ 215 {"name": "a", "type": "long"}, 216 {"name": "b", "type": "string"} 217 ] 218 }` 219 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 220 assert.NoError(t, err) 221 222 var got map[string]string 223 err = dec.Decode(&got) 224 225 assert.Error(t, err) 226 } 227 228 func TestDecoder_RecordMapInvalidData(t *testing.T) { 229 defer ConfigTeardown() 230 231 data := []byte{0xE2, 0xA2, 0xF3, 0xAD, 0xAD, 0xAD, 0xE2, 0xA2, 0xF3, 0xAD, 0xAD, 0x06, 0x66, 0x6f, 0x6f} 232 schema := `{ 233 "type": "record", 234 "name": "test", 235 "fields" : [ 236 {"name": "a", "type": "long"}, 237 {"name": "b", "type": "string"} 238 ] 239 }` 240 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 241 assert.NoError(t, err) 242 243 var got map[string]interface{} 244 err = dec.Decode(&got) 245 246 assert.Error(t, err) 247 } 248 249 func TestDecoder_RecordInterface(t *testing.T) { 250 defer ConfigTeardown() 251 252 data := []byte{0x36, 0x06, 0x66, 0x6f, 0x6f} 253 schema := `{ 254 "type": "record", 255 "name": "test", 256 "fields" : [ 257 {"name": "a", "type": "long"}, 258 {"name": "b", "type": "string"} 259 ] 260 }` 261 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 262 assert.NoError(t, err) 263 264 var got TestInterface = &TestRecord{} 265 err = dec.Decode(&got) 266 267 assert.NoError(t, err) 268 assert.Equal(t, &TestRecord{A: 27, B: "foo"}, got) 269 } 270 271 func TestDecoder_RecordEmptyInterface(t *testing.T) { 272 defer ConfigTeardown() 273 274 data := []byte{0x36, 0x06, 0x66, 0x6f, 0x6f} 275 schema := `{ 276 "type": "record", 277 "name": "test", 278 "fields" : [ 279 {"name": "a", "type": "long"}, 280 {"name": "b", "type": "string"} 281 ] 282 }` 283 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 284 assert.NoError(t, err) 285 286 var got TestInterface 287 err = dec.Decode(&got) 288 289 assert.Error(t, err) 290 } 291 292 func TestDecoder_RefStruct(t *testing.T) { 293 defer ConfigTeardown() 294 295 data := []byte{0x36, 0x06, 0x66, 0x6f, 0x6f, 0x36, 0x06, 0x66, 0x6f, 0x6f} 296 schema := `{ 297 "type": "record", 298 "name": "parent", 299 "fields" : [ 300 {"name": "a", "type": { 301 "type": "record", 302 "name": "test", 303 "fields" : [ 304 {"name": "a", "type": "long"}, 305 {"name": "b", "type": "string"} 306 ]} 307 }, 308 {"name": "b", "type": "test"} 309 ] 310 }` 311 dec, err := avro.NewDecoder(schema, bytes.NewReader(data)) 312 assert.NoError(t, err) 313 314 var got TestNestedRecord 315 err = dec.Decode(&got) 316 317 want := TestNestedRecord{ 318 A: TestRecord{A: 27, B: "foo"}, 319 B: TestRecord{A: 27, B: "foo"}, 320 } 321 assert.NoError(t, err) 322 assert.Equal(t, want, got) 323 }