Skip to content

Commit

Permalink
fix: conversion type from float to int
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarfh committed Sep 26, 2023
1 parent 21e94f4 commit fe3c4c8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 20 deletions.
69 changes: 69 additions & 0 deletions convert_numbers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package god

import "reflect"

type Numeric interface {
ToFloat64() float64
ToFloat32() float32
ToInt64() int64
ToInt32() int
}

func ConvertToNumeric(value interface{}) (Numeric, bool) {
v := reflect.ValueOf(value)
if v.Kind() == reflect.Ptr {
elemType := v.Elem().Type()

switch elemType.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return MyInt(v.Elem().Int()), true
case reflect.Float32, reflect.Float64:
return MyFloat64(v.Elem().Float()), true
}
} else {
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return MyInt(v.Int()), true
case reflect.Float32, reflect.Float64:
return MyFloat64(v.Float()), true
}
}

return nil, false
}

type MyInt int

func (mi MyInt) ToFloat64() float64 {
return float64(mi)
}

func (mf MyInt) ToFloat32() float32 {
return float32(mf)
}

func (mi MyInt) ToInt32() int {
return int(mi)
}

func (mi MyInt) ToInt64() int64 {
return int64(mi)
}

type MyFloat64 float64

func (mf MyFloat64) ToFloat64() float64 {
return float64(mf)
}

func (mf MyFloat64) ToFloat32() float32 {
return float32(mf)
}

func (mf MyFloat64) ToInt32() int {
return int(mf)
}

func (mi MyFloat64) ToInt64() int64 {
return int64(mi)
}
32 changes: 12 additions & 20 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ func String(v ...Validation) SchemaFunc {
func Float32(v ...Validation) SchemaFunc {
return func(value interface{}) Schema {
return CommonTypeValidation(v, value, "float", func(val interface{}) (interface{}, bool) {
if out, ok := val.(float32); ok {
return out, ok
}
if out, ok := val.(*float32); ok {
return out, ok
if out, ok := ConvertToNumeric(value); ok {
return out.ToFloat32(), ok
}

return nil, false
})
}
Expand All @@ -36,12 +34,10 @@ func Float32(v ...Validation) SchemaFunc {
func Float64(v ...Validation) SchemaFunc {
return func(value interface{}) Schema {
return CommonTypeValidation(v, value, "float64", func(val interface{}) (interface{}, bool) {
if out, ok := val.(float64); ok {
return out, ok
}
if out, ok := val.(*float64); ok {
return out, ok
if out, ok := ConvertToNumeric(value); ok {
return out.ToFloat64(), ok
}

return nil, false
})
}
Expand All @@ -51,12 +47,10 @@ func Float64(v ...Validation) SchemaFunc {
func Int(v ...Validation) SchemaFunc {
return func(value interface{}) Schema {
return CommonTypeValidation(v, value, "int", func(val interface{}) (interface{}, bool) {
if out, ok := val.(int); ok {
return out, ok
}
if out, ok := val.(*int); ok {
return out, ok
if out, ok := ConvertToNumeric(value); ok {
return out.ToInt32(), ok
}

return nil, false
})
}
Expand All @@ -66,12 +60,10 @@ func Int(v ...Validation) SchemaFunc {
func Int64(v ...Validation) SchemaFunc {
return func(value interface{}) Schema {
return CommonTypeValidation(v, value, "int64", func(val interface{}) (interface{}, bool) {
if out, ok := val.(int64); ok {
return out, ok
}
if out, ok := val.(*int64); ok {
return out, ok
if out, ok := ConvertToNumeric(value); ok {
return out.ToInt64(), ok
}

return nil, false
})
}
Expand Down

0 comments on commit fe3c4c8

Please sign in to comment.