Skip to content

Commit

Permalink
fallback to ther Int(), Float() calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Eun committed Aug 13, 2020
1 parent bef6c56 commit a28ba85
Show file tree
Hide file tree
Showing 24 changed files with 1,391 additions and 81 deletions.
24 changes: 18 additions & 6 deletions float32.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,17 @@ func (s stdRecipes) structToFloat32(c Converter, in StructValue, out *float32) e
return err
}

type toFloat32 interface {
Float32() float32
}
type toFloat32WithErr interface {
Float32() (float32, error)
}

func (s stdRecipes) baseStructToFloat32(_ Converter, in reflect.Value, out *float32) error {
if !in.CanInterface() {
return errors.New("unable to make interface")
}
type toFloat32 interface {
Float32() float32
}
type toFloat32WithErr interface {
Float32() (float32, error)
}

// check for struct.Float32()
if i, ok := in.Interface().(toFloat32); ok {
Expand All @@ -124,5 +125,16 @@ func (s stdRecipes) baseStructToFloat32(_ Converter, in reflect.Value, out *floa
return err
}

// check for struct.Float64()
if i, ok := in.Interface().(toFloat64); ok {
*out = float32(i.Float64())
return nil
}
if i, ok := in.Interface().(toFloat64WithErr); ok {
f, err := i.Float64()
*out = float32(f)
return err
}

return fmt.Errorf("%s has no Float32() function", in.Type().String())
}
12 changes: 12 additions & 0 deletions float32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ func TestFloat32(t *testing.T) {

{SomeStructWithFloat32WithErrFuncPtr{}, float32(0), float32(10), "", nil},
{&SomeStructWithFloat32WithErrFuncPtr{}, float32(0), float32(10), "", nil},

{SomeStructWithFloat64Func{}, float32(0), float32(10), "", nil},
{&SomeStructWithFloat64Func{}, float32(0), float32(10), "", nil},

{SomeStructWithFloat64FuncPtr{}, float32(0), float32(10), "", nil},
{&SomeStructWithFloat64FuncPtr{}, float32(0), float32(10), "", nil},

{SomeStructWithFloat64WithErrFunc{}, float32(0), float32(10), "", nil},
{&SomeStructWithFloat64WithErrFunc{}, float32(0), float32(10), "", nil},

{SomeStructWithFloat64WithErrFuncPtr{}, float32(0), float32(10), "", nil},
{&SomeStructWithFloat64WithErrFuncPtr{}, float32(0), float32(10), "", nil},
}

for i, test := range tests {
Expand Down
24 changes: 18 additions & 6 deletions float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,17 @@ func (s stdRecipes) structToFloat64(c Converter, in StructValue, out *float64) e
return err
}

type toFloat64 interface {
Float64() float64
}
type toFloat64WithErr interface {
Float64() (float64, error)
}

func (s stdRecipes) baseStructToFloat64(_ Converter, in reflect.Value, out *float64) error {
if !in.CanInterface() {
return errors.New("unable to make interface")
}
type toFloat64 interface {
Float64() float64
}
type toFloat64WithErr interface {
Float64() (float64, error)
}

// check for struct.Float64()
if i, ok := in.Interface().(toFloat64); ok {
Expand All @@ -124,5 +125,16 @@ func (s stdRecipes) baseStructToFloat64(_ Converter, in reflect.Value, out *floa
return err
}

// check for struct.Float32()
if i, ok := in.Interface().(toFloat32); ok {
*out = float64(i.Float32())
return nil
}
if i, ok := in.Interface().(toFloat32WithErr); ok {
f, err := i.Float32()
*out = float64(f)
return err
}

return fmt.Errorf("%s has no Float64() function", in.Type().String())
}
12 changes: 12 additions & 0 deletions float64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ func TestFloat64(t *testing.T) {
// time
{time.Unix(10, 10), float64(10.00000001), float64(10.00000001), "", nil},

{SomeStructWithFloat32Func{}, float64(0), float64(10), "", nil},
{&SomeStructWithFloat32Func{}, float64(0), float64(10), "", nil},

{SomeStructWithFloat32FuncPtr{}, float64(0), float64(10), "", nil},
{&SomeStructWithFloat32FuncPtr{}, float64(0), float64(10), "", nil},

{SomeStructWithFloat32WithErrFunc{}, float64(0), float64(10), "", nil},
{&SomeStructWithFloat32WithErrFunc{}, float64(0), float64(10), "", nil},

{SomeStructWithFloat32WithErrFuncPtr{}, float64(0), float64(10), "", nil},
{&SomeStructWithFloat32WithErrFuncPtr{}, float64(0), float64(10), "", nil},

{SomeStructWithFloat64Func{}, float64(0), float64(10), "", nil},
{&SomeStructWithFloat64Func{}, float64(0), float64(10), "", nil},

Expand Down
113 changes: 107 additions & 6 deletions int.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,17 @@ func (s stdRecipes) structToInt(c Converter, in StructValue, out *int) error {
return err
}

type toInt interface {
Int() int
}
type toIntWithErr interface {
Int() (int, error)
}

func (s stdRecipes) baseStructToInt(_ Converter, in reflect.Value, out *int) error {
if !in.CanInterface() {
return errors.New("unable to make interface")
}
type toInt interface {
Int() int
}
type toIntWithErr interface {
Int() (int, error)
}

// check for struct.Int()
if i, ok := in.Interface().(toInt); ok {
Expand All @@ -123,5 +124,105 @@ func (s stdRecipes) baseStructToInt(_ Converter, in reflect.Value, out *int) err
return err
}

if ok, i, err := genericIntConvert(in); ok {
if err != nil {
return err
}
*out = int(i)
return nil
}

return fmt.Errorf("%s has no Int() function", in.Type().String())
}

func genericIntConvert(in reflect.Value) (bool, int64, error) {
// check for struct.Int()
if i, ok := in.Interface().(toInt); ok {
return true, int64(i.Int()), nil
}
if i, ok := in.Interface().(toIntWithErr); ok {
v, err := i.Int()
return true, int64(v), err
}

// check for struct.Int8()
if i, ok := in.Interface().(toInt8); ok {
return true, int64(i.Int8()), nil
}
if i, ok := in.Interface().(toInt8WithErr); ok {
v, err := i.Int8()
return true, int64(v), err
}

// check for struct.Int16()
if i, ok := in.Interface().(toInt16); ok {
return true, int64(i.Int16()), nil
}
if i, ok := in.Interface().(toInt16WithErr); ok {
v, err := i.Int16()
return true, int64(v), err
}

// check for struct.Int32()
if i, ok := in.Interface().(toInt32); ok {
return true, int64(i.Int32()), nil
}
if i, ok := in.Interface().(toInt32WithErr); ok {
v, err := i.Int32()
return true, int64(v), err
}

// check for struct.Int64()
if i, ok := in.Interface().(toInt64); ok {
return true, int64(i.Int64()), nil
}
if i, ok := in.Interface().(toInt64WithErr); ok {
v, err := i.Int64()
return true, v, err
}

// check for struct.Uint()
if i, ok := in.Interface().(toUint); ok {
return true, int64(i.Uint()), nil
}
if i, ok := in.Interface().(toUintWithErr); ok {
v, err := i.Uint()
return true, int64(v), err
}

// check for struct.Uint8()
if i, ok := in.Interface().(toUint8); ok {
return true, int64(i.Uint8()), nil
}
if i, ok := in.Interface().(toUint8WithErr); ok {
v, err := i.Uint8()
return true, int64(v), err
}

// check for struct.Uint16()
if i, ok := in.Interface().(toUint16); ok {
return true, int64(i.Uint16()), nil
}
if i, ok := in.Interface().(toUint16WithErr); ok {
v, err := i.Uint16()
return true, int64(v), err
}

// check for struct.Uint32()
if i, ok := in.Interface().(toUint32); ok {
return true, int64(i.Uint32()), nil
}
if i, ok := in.Interface().(toUint32WithErr); ok {
v, err := i.Uint32()
return true, int64(v), err
}
// check for struct.Uint64()
if i, ok := in.Interface().(toUint64); ok {
return true, int64(i.Uint64()), nil
}
if i, ok := in.Interface().(toUint64WithErr); ok {
v, err := i.Uint64()
return true, int64(v), err
}
return false, 0, nil
}
21 changes: 15 additions & 6 deletions int16.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,17 @@ func (s stdRecipes) structToInt16(c Converter, in StructValue, out *int16) error
return err
}

type toInt16 interface {
Int16() int16
}
type toInt16WithErr interface {
Int16() (int16, error)
}

func (s stdRecipes) baseStructToInt16(_ Converter, in reflect.Value, out *int16) error {
if !in.CanInterface() {
return errors.New("unable to make interface")
}
type toInt16 interface {
Int16() int16
}
type toInt16WithErr interface {
Int16() (int16, error)
}

// check for struct.Int16()
if i, ok := in.Interface().(toInt16); ok {
Expand All @@ -123,5 +124,13 @@ func (s stdRecipes) baseStructToInt16(_ Converter, in reflect.Value, out *int16)
return err
}

if ok, i, err := genericIntConvert(in); ok {
if err != nil {
return err
}
*out = int16(i)
return nil
}

return fmt.Errorf("%s has no Int16() function", in.Type().String())
}
Loading

0 comments on commit a28ba85

Please sign in to comment.