Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ehanced parameter metadata to include concepts of required/optional, … #59

Merged
merged 5 commits into from
Jul 28, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Support enumerated parameter type explicitly, and specify valid value…
…s via another field. Also support list of strings as a parameter type
  • Loading branch information
djaglowski committed Jul 27, 2020
commit 243590676cd813a7023423685b47f2400e0eb9cb
160 changes: 95 additions & 65 deletions operator/plugin_parameter.go
Original file line number Diff line number Diff line change
@@ -11,8 +11,9 @@ type PluginParameter struct {
Label string
Description string
Required bool
Type interface{} // "string", "int", "bool" or array of strings
Default interface{} // Must be valid according to Type
Type string // "string", "int", "bool", "strings", or "enum"
ValidValues []string `yaml:"valid_values"` // only useable if Type == "enum"
Default interface{} // Must be valid according to Type & ValidValues
}

func (param PluginParameter) validate() error {
@@ -23,95 +24,124 @@ func (param PluginParameter) validate() error {
)
}

switch t := param.Type.(type) {
case string:
switch t {
case "string", "int", "bool": // ok
default:
if err := param.validateType(); err != nil {
return err
}

if err := param.validateValidValues(); err != nil {
return err
}

if err := param.validateDefault(); err != nil {
return err
}

return nil
}

func (param PluginParameter) validateType() error {
switch param.Type {
case "string", "int", "bool", "strings", "enum": // ok
default:
return errors.NewError(
"invalid type for parameter",
"ensure that the type is one of 'string', 'int', 'bool', 'strings', or 'enum'",
)
}
return nil
}

func (param PluginParameter) validateValidValues() error {
switch param.Type {
case "string", "int", "bool", "strings":
if len(param.ValidValues) > 0 {
return errors.NewError(
"invalid type for parameter",
"ensure that the type is one of 'string', 'int', 'bool', or an array containing only strings",
fmt.Sprintf("valid_values is undefined for parameter of type '%s'", param.Type),
"remove 'valid_values' field or change type to 'enum'",
)
}

if param.Default == nil {
return nil
case "enum":
if len(param.ValidValues) == 0 {
return errors.NewError(
"parameter of type 'enum' must have 'valid_values' specified",
"specify an array that includes one or more valid values",
)
}
}
return nil
}

// Validate default corresponds to type
func (param PluginParameter) validateDefault() error {
if param.Default == nil {
return nil
}

// Validate that Default corresponds to Type
switch param.Type {
case "string":
if _, ok := param.Default.(string); !ok {
return errors.NewError(
"default value for a parameter of type 'string' must be a string",
"ensure that the default value is a string",
)
}
case "int":
switch param.Default.(type) {
case string:
if param.Type != "string" {
return errors.NewError(
fmt.Sprintf("default value is a string but parameter type is %s", param.Type),
"ensure that the default value is a string",
)
}
case int, int32, int64:
if param.Type != "int" {
return errors.NewError(
fmt.Sprintf("default value is an int but parameter type is %s", param.Type),
"ensure that the default value is an int",
)
}
case bool:
if param.Type != "bool" {
return errors.NewError(
fmt.Sprintf("default value is a bool but parameter type is %s", param.Type),
"ensure that the default value is a bool",
)
}
case int, int32, int64: // ok
default:
return errors.NewError(
"invalid default value",
"ensure that the default value corresponds to parameter type",
"default value for a parameter of type 'int' must be an integer",
"ensure that the default value is an integer",
)
}

return nil
case []interface{}: // array represents enumerated values
for _, e := range t {
if _, ok := e.(string); !ok {
return errors.NewError(
"invalid value for enumerated parameter",
"ensure that all enumerated values are strings",
)
}
}

if param.Default == nil {
return nil
case "bool":
if _, ok := param.Default.(bool); !ok {
return errors.NewError(
"default value for a parameter of type 'bool' must be a boolean",
"ensure that the default value is a boolean",
)
}

// Validate that the default value is included in the enumeration
def, ok := param.Default.(string)
case "strings":
defaultList, ok := param.Default.([]interface{})
if !ok {
return errors.NewError(
"invalid default for enumerated parameter",
"default value for a parameter of type 'strings' must be an array of strings",
"ensure that the default value is a string",
)
}

validDef := false
for _, e := range t {
if str, ok := e.(string); ok && str == def {
validDef = true
for _, s := range defaultList {
if _, ok := s.(string); !ok {
return errors.NewError(
"default value for a parameter of type 'strings' must be an array of strings",
"ensure that the default value is an array of strings",
)
}
}

if !validDef {
case "enum":
if param.Default != nil {
// Validate that the default value is included in the enumeration
def, ok := param.Default.(string)
if !ok {
return errors.NewError(
"invalid default for enumerated parameter",
"ensure that the default value is a string",
)
}
for _, val := range param.ValidValues {
if val == def {
return nil
}
}
return errors.NewError(
"invalid default value for enumerated parameter",
"ensure default value is listed as a valid value",
)
}

return nil

default:
return errors.NewError(
"invalid type for parameter",
"supported types are 'string', 'int', 'bool', and array of strings",
"ensure that the type is one of 'string', 'int', 'bool', 'strings', or 'enum'",
)
}
return nil
}
Loading