Skip to content

Commit

Permalink
Add Int and Uint types to serix
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Oct 4, 2023
1 parent 67f34bf commit c0a9c77
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions serializer/serix/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func (api *API) decodeBasedOnType(ctx context.Context, b []byte, value reflect.V

return deseri.Done()

case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
deseri := serializer.NewDeserializer(b)
addrValue := value.Addr()
Expand Down
4 changes: 2 additions & 2 deletions serializer/serix/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func (api *API) encodeBasedOnType(
return ierrors.Wrap(err, "failed to write bool value to serializer")
}).Serialize()

case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
_, typeToConvert, _ := getNumberTypeToConvert(valueType.Kind())
value = value.Convert(typeToConvert)
Expand Down
4 changes: 2 additions & 2 deletions serializer/serix/map_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ func (api *API) mapDecodeBasedOnType(ctx context.Context, mapVal any, value refl
return nil
case reflect.Int8, reflect.Int16, reflect.Int32:
return api.mapDecodeNum(value, valueType, float64NumParser(mapVal.(float64), value.Kind(), true))
case reflect.Int64:
case reflect.Int64, reflect.Int:
return api.mapDecodeNum(value, valueType, strNumParser(mapVal.(string), 64, true))
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
return api.mapDecodeNum(value, valueType, float64NumParser(mapVal.(float64), value.Kind(), false))
case reflect.Uint64:
case reflect.Uint64, reflect.Uint:
return api.mapDecodeNum(value, valueType, strNumParser(mapVal.(string), 64, false))
case reflect.Float32, reflect.Float64:
return api.mapDecodeFloat(value, valueType, mapVal)
Expand Down
4 changes: 2 additions & 2 deletions serializer/serix/map_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ func (api *API) mapEncodeBasedOnType(
return value.Bool(), nil
case reflect.Int8, reflect.Int16, reflect.Int32:
return value.Int(), nil
case reflect.Int64:
case reflect.Int64, reflect.Int:
return strconv.FormatInt(value.Int(), 10), nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
return value.Uint(), nil
case reflect.Uint64:
case reflect.Uint64, reflect.Uint:
return strconv.FormatUint(value.Uint(), 10), nil
case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(value.Float(), 'E', -1, 64), nil
Expand Down
6 changes: 6 additions & 0 deletions serializer/serix/map_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ func TestMapEncodeDecode(t *testing.T) {
name: "basic types",
paras: func() paras {
type example struct {
Uint uint `serix:"0,mapKey=uint"`
Uint64 uint64 `serix:"0,mapKey=uint64"`
Uint32 uint32 `serix:"1,mapKey=uint32"`
Uint16 uint16 `serix:"2,mapKey=uint16"`
Uint8 uint8 `serix:"3,mapKey=uint8"`
Int int `serix:"4,mapKey=int"`
Int64 int64 `serix:"4,mapKey=int64"`
Int32 int32 `serix:"5,mapKey=int32"`
Int16 int16 `serix:"6,mapKey=int16"`
Expand All @@ -97,10 +99,12 @@ func TestMapEncodeDecode(t *testing.T) {
return paras{
api: api,
in: &example{
Uint: 1,
Uint64: 64,
Uint32: 32,
Uint16: 16,
Uint8: 8,
Int: 1,
Int64: -64,
Int32: -32,
Int16: -16,
Expand All @@ -115,10 +119,12 @@ func TestMapEncodeDecode(t *testing.T) {
}(),
expected: `{
"type": 42,
"uint": "1",
"uint64": "64",
"uint32": 32,
"uint16": 16,
"uint8": 8,
"int": "1",
"int64": "-64",
"int32": -32,
"int16": -16,
Expand Down
4 changes: 2 additions & 2 deletions serializer/serix/serix.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ func getNumberTypeToConvert(kind reflect.Kind) (int, reflect.Type, reflect.Type)
case reflect.Int32:
numberType = reflect.TypeOf(int32(0))
bitSize = 32
case reflect.Int64:
case reflect.Int64, reflect.Int:
numberType = reflect.TypeOf(int64(0))
bitSize = 64
case reflect.Uint8:
Expand All @@ -1000,7 +1000,7 @@ func getNumberTypeToConvert(kind reflect.Kind) (int, reflect.Type, reflect.Type)
case reflect.Uint32:
numberType = reflect.TypeOf(uint32(0))
bitSize = 32
case reflect.Uint64:
case reflect.Uint64, reflect.Uint:
numberType = reflect.TypeOf(uint64(0))
bitSize = 64
case reflect.Float32:
Expand Down

0 comments on commit c0a9c77

Please sign in to comment.