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