diff --git a/decoder_union_test.go b/decoder_union_test.go index 4af546b..bf08093 100644 --- a/decoder_union_test.go +++ b/decoder_union_test.go @@ -225,6 +225,42 @@ func TestDecoder_UnionPtrReversedNull(t *testing.T) { assert.Nil(t, got) } +func TestDecoder_UnionNullableMap(t *testing.T) { + tt := []struct { + name string + data []byte + want map[string]string + }{{ + name: "WithData", + data: []byte{0x02, 0x01, 0x10, 0x06, 0x66, 0x6F, 0x6F, 0x06, 0x66, 0x6F, 0x6F, 0x00}, + want: map[string]string{"foo": "foo"}, + }, { + name: "Empty", + data: []byte{0x02, 0x00}, + want: map[string]string{}, + }, { + name: "Null", + data: []byte{0x00}, + want: nil, + }} + + schema := `["null", {"type":"map", "values": "string"}]` + + for _, test := range tt { + t.Run(test.name, func(t *testing.T) { + defer ConfigTeardown() + + dec, _ := avro.NewDecoder(schema, bytes.NewReader(test.data)) + + var got map[string]string + err := dec.Decode(&got) + + require.NoError(t, err) + assert.Equal(t, test.want, got) + }) + } +} + func TestDecoder_UnionNullableSlice(t *testing.T) { defer ConfigTeardown() diff --git a/encoder_union_test.go b/encoder_union_test.go index 72c9d6e..9d3a4cf 100644 --- a/encoder_union_test.go +++ b/encoder_union_test.go @@ -256,6 +256,43 @@ func TestEncoder_UnionPtrNotNullable(t *testing.T) { assert.Error(t, err) } +func TestEncoder_UnionNullableMap(t *testing.T) { + tt := []struct { + name string + data map[string]string + want []byte + }{{ + name: "WithData", + data: map[string]string{"foo": "foo"}, + want: []byte{0x02, 0x01, 0x10, 0x06, 0x66, 0x6F, 0x6F, 0x06, 0x66, 0x6F, 0x6F, 0x00}, + }, { + name: "Empty", + data: map[string]string{}, + want: []byte{0x02, 0x00}, + }, { + name: "Null", + data: nil, + want: []byte{0x00}, + }} + + schema := `["null", {"type":"map", "values": "string"}]` + + for _, test := range tt { + t.Run(test.name, func(t *testing.T) { + defer ConfigTeardown() + + buf := bytes.NewBuffer([]byte{}) + enc, err := avro.NewEncoder(schema, buf) + require.NoError(t, err) + + err = enc.Encode(test.data) + + require.NoError(t, err) + assert.Equal(t, test.want, buf.Bytes()) + }) + } +} + func TestEncoder_UnionNullableSlice(t *testing.T) { defer ConfigTeardown()