provide datatype registry to every type to allow for recursive datatypes : slices, maps, structs

This commit is contained in:
Adrien Marquès 2020-03-22 16:50:10 +01:00
parent eef94ff998
commit 5f3aa5967d
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
8 changed files with 11 additions and 11 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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
}

View File

@ -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
}