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"
|
|
|
|
|
2021-06-20 19:29:46 +00:00
|
|
|
"github.com/xdrm-io/aicra/datatype"
|
2020-03-29 14:59:32 +00:00
|
|
|
)
|
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"`
|
2020-04-04 13:39:00 +00:00
|
|
|
Optional bool
|
|
|
|
// ExtractType is the type the Validator will cast into
|
2020-04-04 09:45:49 +00:00
|
|
|
ExtractType reflect.Type
|
2020-04-04 13:39:00 +00:00
|
|
|
// Validator is inferred from the "type" property
|
2020-04-04 09:45:49 +00:00
|
|
|
Validator datatype.Validator
|
|
|
|
}
|
|
|
|
|
|
|
|
func (param *Parameter) validate(datatypes ...datatype.T) error {
|
2020-03-14 14:24:17 +00:00
|
|
|
if len(param.Description) < 1 {
|
2020-04-04 12:34:20 +00:00
|
|
|
return errMissingParamDesc
|
2020-03-14 14:24:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(param.Type) < 1 || param.Type == "?" {
|
2020-04-04 12:34:20 +00:00
|
|
|
return errMissingParamType
|
2020-03-14 14:24:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-04 13:39:00 +00:00
|
|
|
// optional type
|
2020-03-14 14:24:17 +00:00
|
|
|
if param.Type[0] == '?' {
|
|
|
|
param.Optional = true
|
|
|
|
param.Type = param.Type[1:]
|
|
|
|
}
|
|
|
|
|
2020-04-04 13:39:00 +00:00
|
|
|
// find validator
|
2020-03-29 12:12:47 +00:00
|
|
|
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-04-04 12:34:20 +00:00
|
|
|
return errUnknownDataType
|
2020-03-29 12:12:47 +00:00
|
|
|
}
|
2020-03-14 14:24:17 +00:00
|
|
|
return nil
|
|
|
|
}
|