diff --git a/datatype/datatype.go b/datatype/datatype.go new file mode 100644 index 0000000..2e0a264 --- /dev/null +++ b/datatype/datatype.go @@ -0,0 +1,23 @@ +package datatype + +import ( + "reflect" +) + +// Validator returns whether a given value fulfills the datatype +// and casts the value into a common go type. +// +// for example, if a validator checks for upper case strings, +// whether the value is a []byte, a string or a []rune, if the +// value matches the validator's checks, it will be cast it into +// a common go type, say, string. +type Validator func(value interface{}) (cast interface{}, valid bool) + +// T represents a datatype. The Build function returns a Validator if +// it manages types with the name `typeDefinition` (from the configuration field "type"); else it or returns NIL if the type +// definition does not match this datatype; the registry is passed to allow recursive datatypes (e.g. slices, structs, etc) +// The datatype's validator (when input is valid) must return a cast's go type matching the `Type() reflect.Type` +type T interface { + Type() reflect.Type + Build(typeDefinition string, registry ...T) Validator +} diff --git a/datatype/types.go b/datatype/types.go deleted file mode 100644 index c258b2f..0000000 --- a/datatype/types.go +++ /dev/null @@ -1,15 +0,0 @@ -package datatype - -import "reflect" - -// Validator returns whether a given value fulfills a datatype -// and casts the value into a compatible type -type Validator func(value interface{}) (cast interface{}, valid bool) - -// T builds a T from the type definition (from the configuration field "type") and returns NIL if the type -// definition does not match this T ; the registry is passed for recursive datatypes (e.g. slices, structs, etc) -// to be able to access other datatypes -type T interface { - Type() reflect.Type - Build(typeDefinition string, registry ...T) Validator -}