Skip to content

Commit

Permalink
fix: support go uint type (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghua-ola authored Apr 23, 2022
1 parent 539a6f1 commit c610362
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func Marshal(input interface{}, options *MarshalOptions) ([]byte, error) {
reflect.Int64:
return MarshalInt(value.Int()), nil

case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return MarshalUint(value.Uint()), nil

case reflect.Float32:
Expand Down
1 change: 1 addition & 0 deletions serialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var marshalTests = map[string]marshalTest{
"int32: 27": {int32(27), []byte("i:27;"), nil},
"int64: 28": {int64(28), []byte("i:28;"), nil},

"uint: 3": {uint(3), []byte("i:3;"), nil},
"uint8: 4": {uint8(4), []byte("i:4;"), nil},
"uint16: 7": {uint16(7), []byte("i:7;"), nil},
"uint32: 9": {uint32(9), []byte("i:9;"), nil},
Expand Down
2 changes: 1 addition & 1 deletion unserialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func Unmarshal(data []byte, v interface{}) error {

value.SetInt(v)

case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v, err := UnmarshalUint(data)
if err != nil {
return err
Expand Down
14 changes: 14 additions & 0 deletions unserialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ func TestUnmarshalInt(t *testing.T) {
}
})

t.Run("uint", func(t *testing.T) {
var result uint
err := phpserialize.Unmarshal(test.input, &result)

if test.expectedError == nil {
expectErrorToNotHaveOccurred(t, err)
if result != uint(test.output) {
t.Errorf("Expected '%v', got '%v'", result, test.output)
}
} else {
expectErrorToEqual(t, err, test.expectedError)
}
})

t.Run("uint8", func(t *testing.T) {
var result uint8
err := phpserialize.Unmarshal(test.input, &result)
Expand Down

0 comments on commit c610362

Please sign in to comment.