Add validator interface to unify and for readability #11
|
@ -6,7 +6,7 @@ import "git.xdrm.io/go/aicra/datatype"
|
|||
type AnyDataType struct{}
|
||||
|
||||
// Build returns the validator
|
||||
func (AnyDataType) Build(typeName string) datatype.Validator {
|
||||
func (AnyDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
||||
// nothing if type not handled
|
||||
if typeName != "any" {
|
||||
return nil
|
||||
|
|
|
@ -6,7 +6,7 @@ import "git.xdrm.io/go/aicra/datatype"
|
|||
type BoolDataType struct{}
|
||||
|
||||
// Build returns the validator
|
||||
func (BoolDataType) Build(typeName string) datatype.Validator {
|
||||
func (BoolDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
||||
// nothing if type not handled
|
||||
if typeName != "bool" {
|
||||
return nil
|
||||
|
|
|
@ -10,7 +10,7 @@ import (
|
|||
type FloatDataType struct{}
|
||||
|
||||
// Build returns the validator
|
||||
func (FloatDataType) Build(typeName string) datatype.Validator {
|
||||
func (FloatDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
||||
// nothing if type not handled
|
||||
if typeName != "float64" && typeName != "float" {
|
||||
return nil
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
type IntDataType struct{}
|
||||
|
||||
// Build returns the validator
|
||||
func (IntDataType) Build(typeName string) datatype.Validator {
|
||||
func (IntDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
||||
// nothing if type not handled
|
||||
if typeName != "int" {
|
||||
return nil
|
||||
|
|
|
@ -15,7 +15,7 @@ type StringDataType struct{}
|
|||
|
||||
// Build returns the validator.
|
||||
// availables type names are : `string`, `string(length)` and `string(minLength, maxLength)`.
|
||||
func (s StringDataType) Build(typeName string) datatype.Validator {
|
||||
func (s StringDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
||||
simple := typeName == "string"
|
||||
fixedLengthMatches := fixedLengthRegex.FindStringSubmatch(typeName)
|
||||
variableLengthMatches := variableLengthRegex.FindStringSubmatch(typeName)
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
type UintDataType struct{}
|
||||
|
||||
// Build returns the validator
|
||||
func (UintDataType) Build(typeName string) datatype.Validator {
|
||||
func (UintDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
|
||||
// nothing if type not handled
|
||||
if typeName != "uint" {
|
||||
return nil
|
||||
|
|
|
@ -4,9 +4,9 @@ package 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
|
||||
// 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 {
|
||||
Build(typeDefinition string) Validator
|
||||
Build(typeDefinition string, registry ...T) Validator
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ func (param *Parameter) checkAndFormat() error {
|
|||
// assigns the first matching data type from the type definition
|
||||
func (param *Parameter) assignDataType(types []datatype.T) bool {
|
||||
for _, dtype := range types {
|
||||
param.Validator = dtype.Build(param.Type)
|
||||
param.Validator = dtype.Build(param.Type, types...)
|
||||
if param.Validator != nil {
|
||||
return true
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue