Skip to content

Commit

Permalink
avro: add boolean to resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
akihiro17 authored Jul 20, 2020
1 parent 23ae1fd commit d5f3e03
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions decoder_union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,20 @@ func TestDecoder_UnionInterfaceInMap(t *testing.T) {
assert.Equal(t, map[string]interface{}{"foo": 27}, got)
}

func TestDecoder_UnionInterfaceInMapWithBool(t *testing.T) {
defer ConfigTeardown()

data := []byte{0x01, 0x0c, 0x06, 0x66, 0x6F, 0x6F, 0x02, 0x01, 0x00}
schema := `{"type":"map", "values": ["null", "boolean"]}`
dec, _ := avro.NewDecoder(schema, bytes.NewReader(data))

var got map[string]interface{}
err := dec.Decode(&got)

assert.NoError(t, err)
assert.Equal(t, map[string]interface{}{"foo": true}, got)
}

func TestDecoder_UnionInterfaceMap(t *testing.T) {
defer ConfigTeardown()

Expand Down
14 changes: 14 additions & 0 deletions encoder_union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,20 @@ func TestEncoder_UnionInterfaceMap(t *testing.T) {
assert.Equal(t, []byte{0x02, 0x01, 0x0a, 0x06, 0x66, 0x6f, 0x6f, 0x36, 0x00}, buf.Bytes())
}

func TestEncoder_UnionInterfaceInMapWithBool(t *testing.T) {
defer ConfigTeardown()

schema := `{"type":"map", "values": ["null", "boolean"]}`
buf := bytes.NewBuffer([]byte{})
enc, err := avro.NewEncoder(schema, buf)
assert.NoError(t, err)

err = enc.Encode(map[string]interface{}{"foo": true})

assert.NoError(t, err)
assert.Equal(t, []byte{0x01, 0x0c, 0x06, 0x66, 0x6F, 0x6F, 0x02, 0x01, 0x00}, buf.Bytes())
}

func TestEncoder_UnionInterfaceArray(t *testing.T) {
defer ConfigTeardown()

Expand Down
1 change: 1 addition & 0 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func NewTypeResolver() *TypeResolver {
r.Register(string(Double), float64(0))
r.Register(string(String), "")
r.Register(string(Bytes), []byte{})
r.Register(string(Boolean), bool(true))

return r
}
Expand Down

0 comments on commit d5f3e03

Please sign in to comment.