2019-05-01 09:23:05 +00:00
|
|
|
package builtin
|
|
|
|
|
2019-05-01 09:28:55 +00:00
|
|
|
import "git.xdrm.io/go/aicra/typecheck"
|
2019-05-01 09:23:05 +00:00
|
|
|
|
|
|
|
// Bool checks if a value is a boolean
|
|
|
|
type Bool struct{}
|
|
|
|
|
|
|
|
// NewBool returns a bare boolean type checker
|
|
|
|
func NewBool() *Bool {
|
|
|
|
return &Bool{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checker returns the checker function
|
2019-05-02 20:15:03 +00:00
|
|
|
func (Bool) Checker(typeName string) typecheck.CheckerFunc {
|
2019-05-01 09:23:05 +00:00
|
|
|
// nothing if type not handled
|
|
|
|
if typeName != "bool" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return func(value interface{}) bool {
|
2019-11-18 15:17:02 +00:00
|
|
|
_, isBool := readBool(value)
|
2019-05-01 09:23:05 +00:00
|
|
|
return isBool
|
|
|
|
}
|
|
|
|
}
|
2019-11-18 15:17:02 +00:00
|
|
|
|
|
|
|
// readBool tries to read a serialized boolean and returns whether it succeeded.
|
|
|
|
func readBool(value interface{}) (bool, bool) {
|
|
|
|
switch cast := value.(type) {
|
|
|
|
case bool:
|
|
|
|
return cast, true
|
|
|
|
|
|
|
|
case string:
|
|
|
|
strVal := string(cast)
|
|
|
|
return strVal == "true", strVal == "true" || strVal == "false"
|
|
|
|
|
|
|
|
case []byte:
|
|
|
|
strVal := string(cast)
|
|
|
|
return strVal == "true", strVal == "true" || strVal == "false"
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, false
|
|
|
|
}
|