add Kind() method to datatype.T interface and to config parameter

This commit is contained in:
Adrien Marquès 2020-03-28 18:48:27 +01:00
parent cb7f22e03d
commit 8cfa2235d6
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
9 changed files with 52 additions and 4 deletions

View File

@ -1,10 +1,19 @@
package builtin package builtin
import "git.xdrm.io/go/aicra/datatype" import (
"reflect"
"git.xdrm.io/go/aicra/datatype"
)
// AnyDataType is what its name tells // AnyDataType is what its name tells
type AnyDataType struct{} type AnyDataType struct{}
// Kind returns the kind of data
func (AnyDataType) Kind() reflect.Kind {
return reflect.Interface
}
// Build returns the validator // Build returns the validator
func (AnyDataType) Build(typeName string, registry ...datatype.T) datatype.Validator { func (AnyDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
// nothing if type not handled // nothing if type not handled

View File

@ -1,10 +1,19 @@
package builtin package builtin
import "git.xdrm.io/go/aicra/datatype" import (
"reflect"
"git.xdrm.io/go/aicra/datatype"
)
// BoolDataType is what its name tells // BoolDataType is what its name tells
type BoolDataType struct{} type BoolDataType struct{}
// Kind returns the kind of data
func (BoolDataType) Kind() reflect.Kind {
return reflect.Bool
}
// Build returns the validator // Build returns the validator
func (BoolDataType) Build(typeName string, registry ...datatype.T) datatype.Validator { func (BoolDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
// nothing if type not handled // nothing if type not handled

View File

@ -2,6 +2,7 @@ package builtin
import ( import (
"encoding/json" "encoding/json"
"reflect"
"git.xdrm.io/go/aicra/datatype" "git.xdrm.io/go/aicra/datatype"
) )
@ -9,6 +10,11 @@ import (
// FloatDataType is what its name tells // FloatDataType is what its name tells
type FloatDataType struct{} type FloatDataType struct{}
// Kind returns the kind of data
func (FloatDataType) Kind() reflect.Kind {
return reflect.Float64
}
// Build returns the validator // Build returns the validator
func (FloatDataType) Build(typeName string, registry ...datatype.T) datatype.Validator { func (FloatDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
// nothing if type not handled // nothing if type not handled

View File

@ -3,6 +3,7 @@ package builtin
import ( import (
"encoding/json" "encoding/json"
"math" "math"
"reflect"
"git.xdrm.io/go/aicra/datatype" "git.xdrm.io/go/aicra/datatype"
) )
@ -10,6 +11,11 @@ import (
// IntDataType is what its name tells // IntDataType is what its name tells
type IntDataType struct{} type IntDataType struct{}
// Kind returns the kind of data
func (IntDataType) Kind() reflect.Kind {
return reflect.Int
}
// Build returns the validator // Build returns the validator
func (IntDataType) Build(typeName string, registry ...datatype.T) datatype.Validator { func (IntDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
// nothing if type not handled // nothing if type not handled

View File

@ -1,6 +1,7 @@
package builtin package builtin
import ( import (
"reflect"
"regexp" "regexp"
"strconv" "strconv"
@ -13,6 +14,11 @@ var variableLengthRegex = regexp.MustCompile(`^string\((\d+), ?(\d+)\)$`)
// StringDataType is what its name tells // StringDataType is what its name tells
type StringDataType struct{} type StringDataType struct{}
// Kind returns the kind of data
func (StringDataType) Kind() reflect.Kind {
return reflect.String
}
// 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, registry ...datatype.T) datatype.Validator { func (s StringDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {

View File

@ -3,6 +3,7 @@ package builtin
import ( import (
"encoding/json" "encoding/json"
"math" "math"
"reflect"
"git.xdrm.io/go/aicra/datatype" "git.xdrm.io/go/aicra/datatype"
) )
@ -10,6 +11,11 @@ import (
// UintDataType is what its name tells // UintDataType is what its name tells
type UintDataType struct{} type UintDataType struct{}
// Kind returns the kind of data
func (UintDataType) Kind() reflect.Kind {
return reflect.Uint
}
// Build returns the validator // Build returns the validator
func (UintDataType) Build(typeName string, registry ...datatype.T) datatype.Validator { func (UintDataType) Build(typeName string, registry ...datatype.T) datatype.Validator {
// nothing if type not handled // nothing if type not handled

View File

@ -1,5 +1,7 @@
package datatype package datatype
import "reflect"
// Validator returns whether a given value fulfills a datatype // Validator returns whether a given value fulfills a 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)
@ -8,5 +10,6 @@ type Validator func(value interface{}) (cast interface{}, valid bool)
// definition does not match this T ; the registry is passed for recursive datatypes (e.g. slices, structs, etc) // 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 // to be able to access other datatypes
type T interface { type T interface {
Kind() reflect.Kind
Build(typeDefinition string, registry ...T) Validator Build(typeDefinition string, registry ...T) Validator
} }

View File

@ -238,6 +238,7 @@ func (svc *Service) validateInput(types []datatype.T) error {
param.Validator = dtype.Build(param.Type, types...) param.Validator = dtype.Build(param.Type, types...)
if param.Validator != nil { if param.Validator != nil {
datatypeFound = true datatypeFound = true
param.Kind = dtype.Kind()
break break
} }
} }

View File

@ -2,6 +2,7 @@ package config
import ( import (
"net/http" "net/http"
"reflect"
"git.xdrm.io/go/aicra/datatype" "git.xdrm.io/go/aicra/datatype"
) )
@ -26,8 +27,7 @@ type Service struct {
Scope [][]string `json:"scope"` Scope [][]string `json:"scope"`
Description string `json:"info"` Description string `json:"info"`
Input map[string]*Parameter `json:"in"` Input map[string]*Parameter `json:"in"`
// Download *bool `json:"download"` // Output map[string]*Parameter `json:"out"`
// Output map[string]*Parameter `json:"out"`
// references to url parameters // references to url parameters
// format: '/uri/{param}' // format: '/uri/{param}'
@ -46,6 +46,8 @@ type Parameter struct {
Description string `json:"info"` Description string `json:"info"`
Type string `json:"type"` Type string `json:"type"`
Rename string `json:"name,omitempty"` Rename string `json:"name,omitempty"`
// Kind of data the datatype returns
Kind reflect.Kind
// Optional is set to true when the type is prefixed with '?' // Optional is set to true when the type is prefixed with '?'
Optional bool Optional bool