2021-06-21 19:30:33 +00:00
|
|
|
package validator
|
2020-03-14 15:13:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-03-28 17:48:27 +00:00
|
|
|
"reflect"
|
2020-03-14 15:13:38 +00:00
|
|
|
)
|
|
|
|
|
2021-06-21 19:30:33 +00:00
|
|
|
// FloatType makes the "float" (or "float64") type available in the aicra configuration
|
|
|
|
// It considers valid:
|
|
|
|
// - float64
|
|
|
|
// - int (since it does not overflow)
|
|
|
|
// - uint (since it does not overflow)
|
|
|
|
// - strings containing json-compatible floats
|
|
|
|
// - []byte containing json-compatible floats
|
|
|
|
type FloatType struct{}
|
|
|
|
|
|
|
|
// GoType returns the `float64` type
|
|
|
|
func (FloatType) GoType() reflect.Type {
|
2020-03-28 18:11:23 +00:00
|
|
|
return reflect.TypeOf(float64(0))
|
2020-03-28 17:48:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 19:30:33 +00:00
|
|
|
// Validator for float64 values
|
|
|
|
func (FloatType) Validator(typename string, avail ...Type) ValidateFunc {
|
|
|
|
if typename != "float64" && typename != "float" {
|
2020-03-14 15:13:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return func(value interface{}) (interface{}, bool) {
|
|
|
|
switch cast := value.(type) {
|
|
|
|
|
|
|
|
case int:
|
|
|
|
return float64(cast), true
|
|
|
|
|
|
|
|
case uint:
|
|
|
|
return float64(cast), true
|
|
|
|
|
|
|
|
case float64:
|
|
|
|
return cast, true
|
|
|
|
|
|
|
|
// serialized string -> try to convert to float
|
|
|
|
case []byte:
|
|
|
|
num := json.Number(cast)
|
|
|
|
floatVal, err := num.Float64()
|
|
|
|
return floatVal, err == nil
|
|
|
|
|
|
|
|
case string:
|
|
|
|
num := json.Number(cast)
|
|
|
|
floatVal, err := num.Float64()
|
|
|
|
return floatVal, err == nil
|
|
|
|
|
|
|
|
// unknown type
|
|
|
|
default:
|
|
|
|
return 0, false
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|