Skip to content

Commit

Permalink
Merge pull request #41 from Stebalien/fix/40
Browse files Browse the repository at this point in the history
fix unmarshaling null into []byte
  • Loading branch information
warpfork authored Nov 1, 2018
2 parents 10eebc3 + 6b34486 commit 2700834
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion obj/objFixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ var objFixtures = []struct {
{title: "empty",
sequence: fixtures.SequenceMap["empty"],
marshalResults: []marshalResults{
// not much marshals to empty!
// not much marshals to empty!
},
unmarshalResults: []unmarshalResults{
{title: "into string",
Expand Down Expand Up @@ -1044,6 +1044,8 @@ var objFixtures = []struct {
// valueFn: func() interface{} { return [0]int(nil) }},
{title: "from int slice",
valueFn: func() interface{} { return []int(nil) }},
{title: "from byte slice",
valueFn: func() interface{} { return []byte(nil) }},
//{title: "from iface array", // Not Possible! Compiler says: "cannot convert nil to type [0]interface {}"
// valueFn: func() interface{} { return [0]interface{}(nil) }},
{title: "from iface slice",
Expand Down Expand Up @@ -1099,6 +1101,9 @@ var objFixtures = []struct {
{title: "into *[]str",
slotFn: func() interface{} { var v []string; return &v },
valueFn: func() interface{} { return []string(nil) }},
{title: "into *[]byte",
slotFn: func() interface{} { var v []byte; return &v },
valueFn: func() interface{} { return []byte(nil) }},
{title: "into [0]str",
slotFn: func() interface{} { var v []string; return v },
expectErr: ErrInvalidUnmarshalTarget{reflect.TypeOf([]string{})}},
Expand Down
9 changes: 9 additions & 0 deletions obj/unmarshalBuiltins.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ func (mach *unmarshalMachinePrimitive) Step(_ *Unmarshaller, _ *unmarshalSlab, t
case TBytes:
mach.rv.SetBytes(tok.Bytes)
return true, nil
case TNull:
mach.rv.SetBytes(nil)
return true, nil
default:
return true, ErrUnmarshalTypeCantFit{*tok, mach.rv, 0}
}
Expand All @@ -138,6 +141,12 @@ func (mach *unmarshalMachinePrimitive) Step(_ *Unmarshaller, _ *unmarshalSlab, t
mach.rv.Index(i).SetUint(uint64(tok.Bytes[i]))
}
return true, nil
case TNull:
if mach.rv.Len() != 0 {
return true, ErrUnmarshalTypeCantFit{*tok, mach.rv, 0}
}
mach.rv.SetBytes(nil)
return true, nil
default:
return true, ErrUnmarshalTypeCantFit{*tok, mach.rv, 0}
}
Expand Down

0 comments on commit 2700834

Please sign in to comment.