From c610362dd029c5a9c3fbe98cf9562f919664871a Mon Sep 17 00:00:00 2001 From: HAL 9000 <86941588+yanghua-ola@users.noreply.github.com> Date: Sat, 23 Apr 2022 12:46:52 +0800 Subject: [PATCH] fix: support go uint type (#28) --- serialize.go | 2 +- serialize_test.go | 1 + unserialize.go | 2 +- unserialize_test.go | 14 ++++++++++++++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/serialize.go b/serialize.go index 6facabe..7e003ec 100644 --- a/serialize.go +++ b/serialize.go @@ -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: diff --git a/serialize_test.go b/serialize_test.go index df2bca3..71c466b 100644 --- a/serialize_test.go +++ b/serialize_test.go @@ -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}, diff --git a/unserialize.go b/unserialize.go index 6d4b1a8..add62d9 100644 --- a/unserialize.go +++ b/unserialize.go @@ -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 diff --git a/unserialize_test.go b/unserialize_test.go index c5dd0ba..ca783d9 100644 --- a/unserialize_test.go +++ b/unserialize_test.go @@ -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)