aicra/internal/config/parameter.go

49 lines
1.0 KiB
Go
Raw Normal View History

package config
import (
"reflect"
"github.com/xdrm-io/aicra/datatype"
)
// Parameter represents a parameter definition (from api.json)
type Parameter struct {
Description string `json:"info"`
Type string `json:"type"`
Rename string `json:"name,omitempty"`
Optional bool
// ExtractType is the type the Validator will cast into
ExtractType reflect.Type
// Validator is inferred from the "type" property
Validator datatype.Validator
}
func (param *Parameter) validate(datatypes ...datatype.T) error {
if len(param.Description) < 1 {
2020-04-04 12:34:20 +00:00
return errMissingParamDesc
}
if len(param.Type) < 1 || param.Type == "?" {
2020-04-04 12:34:20 +00:00
return errMissingParamType
}
// optional type
if param.Type[0] == '?' {
param.Optional = true
param.Type = param.Type[1:]
}
// find validator
for _, dtype := range datatypes {
param.Validator = dtype.Build(param.Type, datatypes...)
param.ExtractType = dtype.Type()
if param.Validator != nil {
break
}
}
if param.Validator == nil {
2020-04-04 12:34:20 +00:00
return errUnknownDataType
}
return nil
}