github.com/hamba/avro/v2@v2.22.1-0.20240518180522-aff3955acf7d/decoder_dynamic_test.go (about) 1 package avro_test 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/hamba/avro/v2" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestDecoder_Interface(t *testing.T) { 13 tests := []struct { 14 name string 15 data []byte 16 schema string 17 got any 18 want any 19 }{ 20 { 21 name: "Empty Interface", 22 data: []byte{0x36, 0x06, 0x66, 0x6f, 0x6f}, 23 schema: `{"type": "record", "name": "test", "fields" : [{"name": "a", "type": "long"}, {"name": "b", "type": "string"}]}`, 24 got: nil, 25 want: map[string]any{"a": int64(27), "b": "foo"}, 26 }, 27 { 28 name: "Interface Non-Ptr", 29 data: []byte{0x36, 0x06, 0x66, 0x6f, 0x6f}, 30 schema: `{"type": "record", "name": "test", "fields": [{"name": "a", "type": "long"}, {"name": "b", "type": "string"}]}`, 31 got: TestRecord{}, 32 want: map[string]any{"a": int64(27), "b": "foo"}, 33 }, 34 { 35 name: "Interface Nil Ptr", 36 data: []byte{0x36, 0x06, 0x66, 0x6f, 0x6f}, 37 schema: `{"type": "record", "name": "test", "fields" : [{"name": "a", "type": "long"}, {"name": "b", "type": "string"}]}`, 38 got: (*TestRecord)(nil), 39 want: &TestRecord{A: 27, B: "foo"}, 40 }, 41 { 42 name: "Interface Ptr", 43 data: []byte{0x36, 0x06, 0x66, 0x6f, 0x6f}, 44 schema: `{"type": "record", "name": "test", "fields": [{"name": "a", "type": "long"}, {"name": "b", "type": "string"}]}`, 45 got: &TestRecord{}, 46 want: &TestRecord{A: 27, B: "foo"}, 47 }, 48 } 49 50 for _, test := range tests { 51 test := test 52 t.Run(test.name, func(t *testing.T) { 53 defer ConfigTeardown() 54 55 dec, _ := avro.NewDecoder(test.schema, bytes.NewReader(test.data)) 56 57 err := dec.Decode(&test.got) 58 59 require.NoError(t, err) 60 assert.Equal(t, test.want, test.got) 61 }) 62 } 63 }