utils.go: added functions anyInteger() and anyFloat()

This commit is contained in:
Celestino Amoroso 2024-03-28 06:26:20 +01:00
parent 9fa3d9fcb2
commit fdc2fd8dfd

View File

@ -4,6 +4,8 @@
// utils.go
package expr
import "reflect"
func isString(v any) (ok bool) {
_, ok = v.(string)
return ok
@ -52,3 +54,45 @@ func toBool(v any) (b bool, ok bool) {
}
return
}
func isFunc(v any) bool {
return reflect.TypeOf(v).Kind() == reflect.Func
}
func anyInteger(v any) (i int64, ok bool) {
ok = true
switch intval := v.(type) {
case int:
i = int64(intval)
case uint8:
i = int64(intval)
case uint16:
i = int64(intval)
case uint32:
i = int64(intval)
case int8:
i = int64(intval)
case int16:
i = int64(intval)
case int32:
i = int64(intval)
case int64:
i = intval
default:
ok = false
}
return
}
func anyFloat(v any) (float float64, ok bool) {
ok = true
switch floatval := v.(type) {
case float32:
float = float64(floatval)
case float64:
float = floatval
default:
ok = false
}
return
}