Skip to content

Commit

Permalink
Merge pull request #10 from jmank88/discard_unknown
Browse files Browse the repository at this point in the history
decode: discard unknown fields
  • Loading branch information
jmank88 authored Jan 26, 2018
2 parents a4a42a6 + 854f6fa commit 79177f2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
9 changes: 6 additions & 3 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,9 +656,12 @@ func objectIntoStruct(structPtr reflect.Value) func(*ObjectDecoder) error {
structValue := structPtr.Elem()
f := fieldByName(structValue, k)
if f == zeroValue {
return errors.Errorf("unable to decode entry: no field found named/tagged %q", k)
}
if err := o.Decode(f.Addr().Interface()); err != nil {
// Discard value with no matching field.
// TODO could be more efficient with custom discardValue() method
if _, err := o.decodeInterface(); err != nil {
return errors.Wrapf(err, "failed to discard value for %q with call #%d", k, o.count)
}
} else if err := o.Decode(f.Addr().Interface()); err != nil {
return errors.Wrapf(err, "failed to decode value for %q with call #%d", k, o.count)
}
}
Expand Down
23 changes: 23 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,26 @@ func (tc *testCase) unmarshalBlock(t *testing.T) {
expected, expected, actual.Elem().Interface(), actual.Elem().Interface())
}
}

func TestUnmarshalDiscardUnknownFields(t *testing.T) {
type val struct{ A int8 }

exp := val{8}
var got val

bin := []byte{'{', 'U', 0x01, 'A', 'i', 0x08, 'U', 0x01, 'b', 'i', 0x05, '}'}

if err := Unmarshal(bin, &got); err != nil {
t.Fatal(err)
} else if got != exp {
t.Errorf("\nexpected: %T %v \nbut got: %T %v", exp, exp, got, got)
}

block := "[{]\n\t[U][1][A][i][8]\n\t[U][1][B][i][5]\n[}]"

if err := UnmarshalBlock([]byte(block), &got); err != nil {
t.Fatal(err)
} else if got != exp {
t.Errorf("\nexpected: %T %v \nbut got: %T %v", exp, exp, got, got)
}
}

0 comments on commit 79177f2

Please sign in to comment.