2020-03-14 15:13:38 +00:00
|
|
|
package builtin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"math"
|
2020-03-28 17:48:27 +00:00
|
|
|
"reflect"
|
2020-03-14 15:13:38 +00:00
|
|
|
|
2020-03-16 08:20:00 +00:00
|
|
|
"git.xdrm.io/go/aicra/datatype"
|
2020-03-14 15:13:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// UintDataType is what its name tells
|
|
|
|
type UintDataType struct{}
|
|
|
|
|
2020-03-28 18:11:23 +00:00
|
|
|
// Type returns the type of data
|
|
|
|
func (UintDataType) Type() reflect.Type {
|
|
|
|
return reflect.TypeOf(uint(0))
|
2020-03-28 17:48:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 15:13:38 +00:00
|
|
|
// Build returns the validator
|
2020-03-22 15:50:10 +00:00
|
|
|
func (UintDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
2020-03-14 15:13:38 +00:00
|
|
|
// nothing if type not handled
|
|
|
|
if typeName != "uint" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(value interface{}) (interface{}, bool) {
|
|
|
|
switch cast := value.(type) {
|
|
|
|
|
|
|
|
case int:
|
|
|
|
return uint(cast), cast >= 0
|
|
|
|
|
|
|
|
case uint:
|
|
|
|
return cast, true
|
|
|
|
|
|
|
|
case float64:
|
|
|
|
uintVal := uint(cast)
|
|
|
|
overflows := cast < 0 || cast > math.MaxUint64
|
|
|
|
return uintVal, cast == float64(uintVal) && !overflows
|
|
|
|
|
|
|
|
// serialized string -> try to convert to float
|
|
|
|
case string:
|
|
|
|
num := json.Number(cast)
|
|
|
|
floatVal, err := num.Float64()
|
|
|
|
if err != nil {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
overflows := floatVal < 0 || floatVal > math.MaxUint64
|
|
|
|
return uint(floatVal), !overflows
|
|
|
|
|
|
|
|
case []byte:
|
|
|
|
num := json.Number(cast)
|
|
|
|
floatVal, err := num.Float64()
|
|
|
|
if err != nil {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
overflows := floatVal < 0 || floatVal > math.MaxUint64
|
|
|
|
return uint(floatVal), !overflows
|
|
|
|
|
|
|
|
// unknown type
|
|
|
|
default:
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|