Skip to content

Commit

Permalink
feat: support interface alias type for nil
Browse files Browse the repository at this point in the history
  • Loading branch information
shockerli committed Oct 11, 2021
1 parent 28b2565 commit 8a90f0b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
9 changes: 7 additions & 2 deletions cvte.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ func indirect(a interface{}) (val interface{}, rt reflect.Type, rv reflect.Value
val = rv.Interface()

switch rt.Kind() {
case reflect.Ptr: // indirect the base type, if is be referenced many times
for rv.Kind() == reflect.Ptr && !rv.IsNil() {
case reflect.Ptr: // indirect the base type, if is been referenced many times
for rv.Kind() == reflect.Ptr {
// stop indirect until nil, avoid stack overflow
if rv.IsNil() {
val = nil
return
}
rv = rv.Elem()
}
return indirect(rv.Interface())
Expand Down
37 changes: 20 additions & 17 deletions cvte_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,28 @@ import (

// indirect type
type (
AliasTypeBool bool
AliasTypeInt int
AliasTypeInt8 int8
AliasTypeInt16 int16
AliasTypeInt32 int32
AliasTypeInt64 int64
AliasTypeUint uint
AliasTypeUint8 uint8
AliasTypeUint16 uint16
AliasTypeUint32 uint32
AliasTypeUint64 uint64
AliasTypeFloat32 float32
AliasTypeFloat64 float64
AliasTypeString string
AliasTypeBytes []byte
AliasTypeBool bool
AliasTypeInt int
AliasTypeInt8 int8
AliasTypeInt16 int16
AliasTypeInt32 int32
AliasTypeInt64 int64
AliasTypeUint uint
AliasTypeUint8 uint8
AliasTypeUint16 uint16
AliasTypeUint32 uint32
AliasTypeUint64 uint64
AliasTypeFloat32 float32
AliasTypeFloat64 float64
AliasTypeString string
AliasTypeBytes []byte
AliasTypeInterface interface{}
)

var (
aliasTypeBool_true AliasTypeBool = true
aliasTypeBool_false AliasTypeBool = false
)

var (
aliasTypeInt_0 AliasTypeInt = 0
aliasTypeInt_1 AliasTypeInt = 1

Expand All @@ -49,6 +48,10 @@ var (
aliasTypeString_8d15_minus AliasTypeString = "-8.15"
aliasTypeString_on AliasTypeString = "on"
aliasTypeString_off AliasTypeString = "off"

pointerRunes = []rune("中国")
pointerInterNil *AliasTypeInterface
AliasTypeBytes_nil AliasTypeBytes
)

// custom type
Expand Down
4 changes: 4 additions & 0 deletions int.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ func convUint64(val interface{}) (uint64, error) {
// indirect type
v, _, rv := indirect(val)
switch vv := v.(type) {
case nil:
return 0, nil
case bool:
if vv {
return 1, nil
Expand Down Expand Up @@ -423,6 +425,8 @@ func convInt64(val interface{}) (int64, error) {
// indirect type
v, _, rv := indirect(val)
switch vv := v.(type) {
case nil:
return 0, nil
case bool:
if vv {
return 1, nil
Expand Down
10 changes: 10 additions & 0 deletions int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,7 @@ func TestUint64E(t *testing.T) {
{AliasTypeBytes("0"), 0, false},
{AliasTypeBytes("10.98"), 10, false},
{json.Number("1"), 1, false},
{pointerInterNil, 0, false},

// errors
{int(-8), 0, true},
Expand Down Expand Up @@ -1129,6 +1130,7 @@ func TestUint32E(t *testing.T) {
{AliasTypeBytes("0"), 0, false},
{AliasTypeBytes("10.98"), 10, false},
{json.Number("1"), 1, false},
{pointerInterNil, 0, false},

// errors
{int(-8), 0, true},
Expand Down Expand Up @@ -1242,6 +1244,7 @@ func TestUint16E(t *testing.T) {
{AliasTypeBytes("0"), 0, false},
{AliasTypeBytes("10.98"), 10, false},
{json.Number("1"), 1, false},
{pointerInterNil, 0, false},

// errors
{int(-8), 0, true},
Expand Down Expand Up @@ -1357,6 +1360,7 @@ func TestUint8E(t *testing.T) {
{AliasTypeBytes("0"), 0, false},
{AliasTypeBytes("10.98"), 10, false},
{json.Number("1"), 1, false},
{pointerInterNil, 0, false},

// errors
{int(-8), 0, true},
Expand Down Expand Up @@ -1474,6 +1478,7 @@ func TestUintE(t *testing.T) {
{AliasTypeBytes("0"), 0, false},
{AliasTypeBytes("10.98"), 10, false},
{json.Number("1"), 1, false},
{pointerInterNil, 0, false},

// errors
{int(-8), 0, true},
Expand Down Expand Up @@ -1607,6 +1612,7 @@ func TestInt64E(t *testing.T) {
{aliasTypeBool_false, 0, false},
{&aliasTypeBool_false, 0, false},
{json.Number("1"), 1, false},
{pointerInterNil, 0, false},

// errors
{"10a", 0, true},
Expand Down Expand Up @@ -1724,6 +1730,7 @@ func TestInt32E(t *testing.T) {
{aliasTypeBool_false, 0, false},
{&aliasTypeBool_false, 0, false},
{json.Number("1"), 1, false},
{pointerInterNil, 0, false},

// errors
{"10a", 0, true},
Expand Down Expand Up @@ -1842,6 +1849,7 @@ func TestInt16E(t *testing.T) {
{aliasTypeBool_false, 0, false},
{&aliasTypeBool_false, 0, false},
{json.Number("1"), 1, false},
{pointerInterNil, 0, false},

// errors
{"10a", 0, true},
Expand Down Expand Up @@ -1962,6 +1970,7 @@ func TestInt8E(t *testing.T) {
{aliasTypeBool_false, 0, false},
{&aliasTypeBool_false, 0, false},
{json.Number("1"), 1, false},
{pointerInterNil, 0, false},

// errors
{"10a", 0, true},
Expand Down Expand Up @@ -2083,6 +2092,7 @@ func TestIntE(t *testing.T) {
{aliasTypeBool_false, 0, false},
{&aliasTypeBool_false, 0, false},
{json.Number("1"), 1, false},
{pointerInterNil, 0, false},

// errors
{"10a", 0, true},
Expand Down

0 comments on commit 8a90f0b

Please sign in to comment.