expr/utils.go

55 lines
820 B
Go
Raw Normal View History

2024-03-26 08:45:18 +01:00
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
2024-03-26 07:00:53 +01:00
// utils.go
package expr
func isString(v any) (ok bool) {
_, ok = v.(string)
return ok
}
func isInteger(v any) (ok bool) {
_, ok = v.(int64)
return ok
}
func isFloat(v any) (ok bool) {
_, ok = v.(float64)
return ok
}
func isNumber(v any) (ok bool) {
return isFloat(v) || isInteger(v)
}
func isNumberString(v any) (ok bool) {
return isString(v) || isNumber(v)
}
func numAsFloat(v any) (f float64) {
var ok bool
if f, ok = v.(float64); !ok {
i, _ := v.(int64)
f = float64(i)
}
return
}
func toBool(v any) (b bool, ok bool) {
ok = true
switch x := v.(type) {
case string:
b = len(x) > 0
case float64:
b = x != 0.0
case int64:
b = x != 0
case bool:
b = x
default:
ok = false
}
return
}