diff --git a/parse/builder/builder.go b/parse/builder/builder.go index ec4e25904..253c82b6d 100644 --- a/parse/builder/builder.go +++ b/parse/builder/builder.go @@ -327,6 +327,8 @@ func ConvertSimple(fieldType string, value interface{}, op Operation) (interface return convert.ToString(value), nil case "int": return convert.ToNumber(value) + case "float": + return convert.ToFloat(value) case "password": return convert.ToString(value), nil case "string": diff --git a/types/convert/convert.go b/types/convert/convert.go index 9529ae4c5..ccab26855 100644 --- a/types/convert/convert.go +++ b/types/convert/convert.go @@ -105,6 +105,30 @@ func ToNumber(value interface{}) (int64, error) { return strconv.ParseInt(ToString(value), 10, 64) } +func ToFloat(value interface{}) (float64, error) { + value = Singular(value) + + f64, ok := value.(float64) + if ok { + return f64, nil + } + + f32, ok := value.(float32) + if ok { + return float64(f32), nil + } + + if n, ok := value.(json.Number); ok { + i, err := n.Int64() + if err == nil { + return float64(i), nil + } + f, err := n.Float64() + return float64(f), err + } + return strconv.ParseFloat(ToString(value), 64) +} + func Capitalize(s string) string { if len(s) <= 1 { return strings.ToUpper(s)