2019-05-02 20:15:03 +00:00
|
|
|
package builtin
|
|
|
|
|
|
|
|
import (
|
2019-05-05 19:20:05 +00:00
|
|
|
"encoding/json"
|
|
|
|
"math"
|
|
|
|
|
2019-05-02 20:15:03 +00:00
|
|
|
"git.xdrm.io/go/aicra/typecheck"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Uint checks if a value is an uint
|
|
|
|
type Uint struct{}
|
|
|
|
|
|
|
|
// NewUint returns a bare number type checker
|
|
|
|
func NewUint() *Uint {
|
|
|
|
return &Uint{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checker returns the checker function
|
|
|
|
func (Uint) Checker(typeName string) typecheck.CheckerFunc {
|
|
|
|
// nothing if type not handled
|
|
|
|
if typeName != "uint" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return func(value interface{}) bool {
|
2019-05-05 19:20:05 +00:00
|
|
|
_, isInt := readUint(value)
|
|
|
|
|
|
|
|
return isInt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// readUint tries to read a serialized uint and returns whether it succeeded.
|
|
|
|
func readUint(value interface{}) (uint, bool) {
|
|
|
|
switch cast := value.(type) {
|
2019-05-02 20:15:03 +00:00
|
|
|
|
2019-05-05 19:20:05 +00:00
|
|
|
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
|
2019-05-02 20:15:03 +00:00
|
|
|
}
|
2019-05-05 19:20:05 +00:00
|
|
|
overflows := floatVal < 0 || floatVal > math.MaxUint64
|
|
|
|
return uint(floatVal), !overflows
|
|
|
|
|
2019-11-18 15:17:02 +00:00
|
|
|
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
|
|
|
|
|
2019-05-05 19:20:05 +00:00
|
|
|
// unknown type
|
|
|
|
default:
|
|
|
|
return 0, false
|
2019-05-02 20:15:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|