Skip to content

Commit

Permalink
Make struct decoding also handle empty Primitives
Browse files Browse the repository at this point in the history
Updates BurntSushi#125.
Updates BurntSushi#126.
  • Loading branch information
cespare authored and IMAZU Mitsumasa committed Jul 19, 2016
1 parent ff6d53e commit a8c2f2a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ func (md *MetaData) unify(data interface{}, rv reflect.Value) error {
func (md *MetaData) unifyStruct(mapping interface{}, rv reflect.Value) error {
tmap, ok := mapping.(map[string]interface{})
if !ok {
if mapping == nil {
return nil
}
return mismatch(rv, "map", mapping)
}

Expand Down
11 changes: 11 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,9 @@ func TestDecodePrimitive(t *testing.T) {
type S struct {
P Primitive
}
type T struct {
S []int
}
slicep := func(s []int) *[]int { return &s }
arrayp := func(a [2]int) *[2]int { return &a }
mapp := func(m map[string]int) *map[string]int { return &m }
Expand Down Expand Up @@ -582,6 +585,14 @@ func TestDecodePrimitive(t *testing.T) {
{mapp(nil), "[P]\na = 2", mapp(map[string]int{"a": 2})},
{mapp(map[string]int{}), "[P]\na = 2", mapp(map[string]int{"a": 2})},
{mapp(map[string]int{"a": 1, "b": 3}), "[P]\na = 2", mapp(map[string]int{"a": 2, "b": 3})},

// structs
{&T{nil}, "[P]", &T{nil}},
{&T{[]int{}}, "[P]", &T{[]int{}}},
{&T{[]int{1, 2, 3}}, "[P]", &T{[]int{1, 2, 3}}},
{&T{nil}, "[P]\nS = [1,2]", &T{[]int{1, 2}}},
{&T{[]int{}}, "[P]\nS = [1,2]", &T{[]int{1, 2}}},
{&T{[]int{1, 2, 3}}, "[P]\nS = [1,2]", &T{[]int{1, 2}}},
} {
var s S
md, err := Decode(tt.input, &s)
Expand Down

0 comments on commit a8c2f2a

Please sign in to comment.