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