From 8cfa2235d6377bd58be2fc9dc08aad34ab898bd9 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 28 Mar 2020 18:48:27 +0100 Subject: [PATCH] add Kind() method to datatype.T interface and to config parameter --- datatype/builtin/any.go | 11 ++++++++++- datatype/builtin/bool.go | 11 ++++++++++- datatype/builtin/float.go | 6 ++++++ datatype/builtin/int.go | 6 ++++++ datatype/builtin/string.go | 6 ++++++ datatype/builtin/uint.go | 6 ++++++ datatype/types.go | 3 +++ internal/config/service.go | 1 + internal/config/types.go | 6 ++++-- 9 files changed, 52 insertions(+), 4 deletions(-) diff --git a/datatype/builtin/any.go b/datatype/builtin/any.go index e12dd10..d96b26d 100644 --- a/datatype/builtin/any.go +++ b/datatype/builtin/any.go @@ -1,10 +1,19 @@ package builtin -import "git.xdrm.io/go/aicra/datatype" +import ( + "reflect" + + "git.xdrm.io/go/aicra/datatype" +) // AnyDataType is what its name tells type AnyDataType struct{} +// Kind returns the kind of data +func (AnyDataType) Kind() reflect.Kind { + return reflect.Interface +} + // Build returns the validator func (AnyDataType) Build(typeName string, registry ...datatype.T) datatype.Validator { // nothing if type not handled diff --git a/datatype/builtin/bool.go b/datatype/builtin/bool.go index 1d5e225..112158c 100644 --- a/datatype/builtin/bool.go +++ b/datatype/builtin/bool.go @@ -1,10 +1,19 @@ package builtin -import "git.xdrm.io/go/aicra/datatype" +import ( + "reflect" + + "git.xdrm.io/go/aicra/datatype" +) // BoolDataType is what its name tells type BoolDataType struct{} +// Kind returns the kind of data +func (BoolDataType) Kind() reflect.Kind { + return reflect.Bool +} + // Build returns the validator func (BoolDataType) Build(typeName string, registry ...datatype.T) datatype.Validator { // nothing if type not handled diff --git a/datatype/builtin/float.go b/datatype/builtin/float.go index d2f3204..3eb4c23 100644 --- a/datatype/builtin/float.go +++ b/datatype/builtin/float.go @@ -2,6 +2,7 @@ package builtin import ( "encoding/json" + "reflect" "git.xdrm.io/go/aicra/datatype" ) @@ -9,6 +10,11 @@ import ( // FloatDataType is what its name tells type FloatDataType struct{} +// Kind returns the kind of data +func (FloatDataType) Kind() reflect.Kind { + return reflect.Float64 +} + // Build returns the validator func (FloatDataType) Build(typeName string, registry ...datatype.T) datatype.Validator { // nothing if type not handled diff --git a/datatype/builtin/int.go b/datatype/builtin/int.go index 7dfdbf9..6559127 100644 --- a/datatype/builtin/int.go +++ b/datatype/builtin/int.go @@ -3,6 +3,7 @@ package builtin import ( "encoding/json" "math" + "reflect" "git.xdrm.io/go/aicra/datatype" ) @@ -10,6 +11,11 @@ import ( // IntDataType is what its name tells type IntDataType struct{} +// Kind returns the kind of data +func (IntDataType) Kind() reflect.Kind { + return reflect.Int +} + // Build returns the validator func (IntDataType) Build(typeName string, registry ...datatype.T) datatype.Validator { // nothing if type not handled diff --git a/datatype/builtin/string.go b/datatype/builtin/string.go index 1a94b82..b09b184 100644 --- a/datatype/builtin/string.go +++ b/datatype/builtin/string.go @@ -1,6 +1,7 @@ package builtin import ( + "reflect" "regexp" "strconv" @@ -13,6 +14,11 @@ var variableLengthRegex = regexp.MustCompile(`^string\((\d+), ?(\d+)\)$`) // StringDataType is what its name tells type StringDataType struct{} +// Kind returns the kind of data +func (StringDataType) Kind() reflect.Kind { + return reflect.String +} + // Build returns the validator. // availables type names are : `string`, `string(length)` and `string(minLength, maxLength)`. func (s StringDataType) Build(typeName string, registry ...datatype.T) datatype.Validator { diff --git a/datatype/builtin/uint.go b/datatype/builtin/uint.go index 53f71de..d88fb5e 100644 --- a/datatype/builtin/uint.go +++ b/datatype/builtin/uint.go @@ -3,6 +3,7 @@ package builtin import ( "encoding/json" "math" + "reflect" "git.xdrm.io/go/aicra/datatype" ) @@ -10,6 +11,11 @@ import ( // UintDataType is what its name tells type UintDataType struct{} +// Kind returns the kind of data +func (UintDataType) Kind() reflect.Kind { + return reflect.Uint +} + // Build returns the validator func (UintDataType) Build(typeName string, registry ...datatype.T) datatype.Validator { // nothing if type not handled diff --git a/datatype/types.go b/datatype/types.go index fe529f1..a4761bb 100644 --- a/datatype/types.go +++ b/datatype/types.go @@ -1,5 +1,7 @@ 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) @@ -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) // to be able to access other datatypes type T interface { + Kind() reflect.Kind Build(typeDefinition string, registry ...T) Validator } diff --git a/internal/config/service.go b/internal/config/service.go index 9ea781a..d65251d 100644 --- a/internal/config/service.go +++ b/internal/config/service.go @@ -238,6 +238,7 @@ func (svc *Service) validateInput(types []datatype.T) error { param.Validator = dtype.Build(param.Type, types...) if param.Validator != nil { datatypeFound = true + param.Kind = dtype.Kind() break } } diff --git a/internal/config/types.go b/internal/config/types.go index 4b356a5..c3cc6f8 100644 --- a/internal/config/types.go +++ b/internal/config/types.go @@ -2,6 +2,7 @@ package config import ( "net/http" + "reflect" "git.xdrm.io/go/aicra/datatype" ) @@ -26,8 +27,7 @@ type Service struct { Scope [][]string `json:"scope"` Description string `json:"info"` 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 // format: '/uri/{param}' @@ -46,6 +46,8 @@ type Parameter struct { Description string `json:"info"` Type string `json:"type"` 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 bool