2020-03-14 14:24:17 +00:00
|
|
|
package config
|
|
|
|
|
2020-03-29 14:59:32 +00:00
|
|
|
import (
|
2020-04-04 09:45:49 +00:00
|
|
|
"reflect"
|
|
|
|
|
2020-03-29 14:59:32 +00:00
|
|
|
"git.xdrm.io/go/aicra/datatype"
|
|
|
|
)
|
2020-03-14 23:27:54 +00:00
|
|
|
|
2020-04-04 09:45:49 +00:00
|
|
|
// Parameter represents a parameter definition (from api.json)
|
|
|
|
type Parameter struct {
|
|
|
|
Description string `json:"info"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Rename string `json:"name,omitempty"`
|
|
|
|
// ExtractType is the type of data the datatype returns
|
|
|
|
ExtractType reflect.Type
|
|
|
|
// Optional is set to true when the type is prefixed with '?'
|
|
|
|
Optional bool
|
|
|
|
|
|
|
|
// Validator is inferred from @Type
|
|
|
|
Validator datatype.Validator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (param *Parameter) validate(datatypes ...datatype.T) error {
|
2020-03-14 14:24:17 +00:00
|
|
|
// missing description
|
|
|
|
if len(param.Description) < 1 {
|
|
|
|
return ErrMissingParamDesc
|
|
|
|
}
|
|
|
|
|
|
|
|
// invalid type
|
|
|
|
if len(param.Type) < 1 || param.Type == "?" {
|
|
|
|
return ErrMissingParamType
|
|
|
|
}
|
|
|
|
|
2020-03-28 11:28:58 +00:00
|
|
|
// optional type transform
|
2020-03-14 14:24:17 +00:00
|
|
|
if param.Type[0] == '?' {
|
|
|
|
param.Optional = true
|
|
|
|
param.Type = param.Type[1:]
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:12:47 +00:00
|
|
|
// assign the datatype
|
|
|
|
for _, dtype := range datatypes {
|
|
|
|
param.Validator = dtype.Build(param.Type, datatypes...)
|
2020-03-29 14:59:32 +00:00
|
|
|
param.ExtractType = dtype.Type()
|
2020-03-29 12:12:47 +00:00
|
|
|
if param.Validator != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-03-29 14:59:32 +00:00
|
|
|
if param.Validator == nil {
|
2020-03-29 12:12:47 +00:00
|
|
|
return ErrUnknownDataType
|
|
|
|
}
|
|
|
|
|
2020-03-14 14:24:17 +00:00
|
|
|
return nil
|
|
|
|
}
|