Skip to content

Commit

Permalink
marshal: handle alias type of int collectly.
Browse files Browse the repository at this point in the history
marshalInt call of reflect.Value.IsNil on alias type of int Value and panic.
  • Loading branch information
matope committed Nov 9, 2015
1 parent cddf562 commit 371530d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 6 additions & 6 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,7 @@ func marshalInt(info TypeInfo, value interface{}) ([]byte, error) {
return nil, nil
}

rv := reflect.ValueOf(value)
if rv.IsNil() {
return nil, nil
}

switch rv.Type().Kind() {
switch rv := reflect.ValueOf(value); rv.Type().Kind() {
case reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8:
v := rv.Int()
if v > math.MaxInt32 || v < math.MinInt32 {
Expand All @@ -315,7 +310,12 @@ func marshalInt(info TypeInfo, value interface{}) ([]byte, error) {
return nil, marshalErrorf("marshal int: value %d out of range", v)
}
return encInt(int32(v)), nil
default:
if rv.IsNil() {
return nil, nil
}
}

return nil, marshalErrorf("can not marshal %T into %s", value, info)
}

Expand Down
7 changes: 7 additions & 0 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"gopkg.in/inf.v0"
)

type AliasInt int

var marshalTests = []struct {
Info TypeInfo
Data []byte
Expand Down Expand Up @@ -73,6 +75,11 @@ var marshalTests = []struct {
[]byte("\x01\x02\x03\x04"),
int(16909060),
},
{
NativeType{proto: 2, typ: TypeInt},
[]byte("\x01\x02\x03\x04"),
AliasInt(16909060),
},
{
NativeType{proto: 2, typ: TypeInt},
[]byte("\x80\x00\x00\x00"),
Expand Down

0 comments on commit 371530d

Please sign in to comment.