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-21 19:08:22 +00:00
|
|
|
"github.com/xdrm-io/aicra/validator"
|
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
|
2021-06-21 19:08:22 +00:00
|
|
|
// GoType is the type the Validator will cast into
|
|
|
|
GoType reflect.Type
|
2020-04-04 13:39:00 +00:00
|
|
|
// Validator is inferred from the "type" property
|
2021-06-21 19:08:22 +00:00
|
|
|
Validator validator.ValidateFunc
|
2020-04-04 09:45:49 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 19:08:22 +00:00
|
|
|
func (param *Parameter) validate(datatypes ...validator.Type) error {
|
2020-03-14 14:24:17 +00:00
|
|
|
if len(param.Description) < 1 {
|
2021-06-21 19:50:57 +00:00
|
|
|
return ErrMissingParamDesc
|
2020-03-14 14:24:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(param.Type) < 1 || param.Type == "?" {
|
2021-06-21 19:50:57 +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 {
|
2021-06-21 19:08:22 +00:00
|
|
|
param.Validator = dtype.Validator(param.Type, datatypes...)
|
|
|
|
param.GoType = dtype.GoType()
|
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 {
|
2021-06-21 19:50:57 +00:00
|
|
|
return ErrUnknownParamType
|
2020-03-29 12:12:47 +00:00
|
|
|
}
|
2020-03-14 14:24:17 +00:00
|
|
|
return nil
|
|
|
|
}
|