2020-03-14 15:13:38 +00:00
|
|
|
package builtin
|
|
|
|
|
2020-03-28 17:48:27 +00:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
2021-06-20 19:29:46 +00:00
|
|
|
"github.com/xdrm-io/aicra/datatype"
|
2020-03-28 17:48:27 +00:00
|
|
|
)
|
2020-03-14 15:13:38 +00:00
|
|
|
|
|
|
|
// BoolDataType is what its name tells
|
|
|
|
type BoolDataType struct{}
|
|
|
|
|
2020-03-28 18:11:23 +00:00
|
|
|
// Type returns the type of data
|
|
|
|
func (BoolDataType) Type() reflect.Type {
|
|
|
|
return reflect.TypeOf(true)
|
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 (BoolDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
2020-03-14 15:13:38 +00:00
|
|
|
// nothing if type not handled
|
|
|
|
if typeName != "bool" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return func(value interface{}) (interface{}, 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|