Skip to content

Commit

Permalink
feat: support optional multi select variables (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
Katajisto authored Aug 26, 2024
1 parent 8be0fde commit 0599255
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
12 changes: 12 additions & 0 deletions examples/variable-types/recipe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ vars:
- option_1
- option_2
- option_3

- name: OPTIONAL_MULTI_SELECT_VAR
description: |
User chooses multiple values or no values at all from the predefined values in `options` property.
Defined by: non-empty `options` property, `optional: true` and `multi: true`.
multi: true
optional: true
options:
- option_1
- option_2
- option_3

- name: TABLE_VAR
description: |
Expand Down
15 changes: 13 additions & 2 deletions pkg/recipe/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (v *Variable) Validate() error {
if len(v.Options) > 0 {
if len(v.Columns) > 0 {
return errors.New("`options` and `columns` properties can not be defined at the same time")
} else if v.Optional {
} else if v.Optional && !v.Multi {
return errors.New("select variables can not be optional")
}
}
Expand Down Expand Up @@ -184,7 +184,7 @@ func (val VariableValues) Validate() error {
case string, bool, MultiSelectValue, TableValue:
break
default:
return fmt.Errorf("unsupported variable value type")
return fmt.Errorf("unsupported variable value type (%T)", v)
}
}

Expand Down Expand Up @@ -350,6 +350,17 @@ func (vv *VariableValues) UnmarshalYAML(unmarshal func(interface{}) error) error
Rows: rows,
}
}
// Multiselect values without a value are an empty []interface{} and we want to cast them into MultiSelectValues
case []interface{}:
multiV := make(MultiSelectValue, len(v))
for i, interfaceV := range v {
str, ok := interfaceV.(string)
if !ok {
return fmt.Errorf("failed to read value to multi select value: %v", v)
}
multiV[i] = str
}
(*vv)[name] = multiV
default:
(*vv)[name] = v
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/ui/survey/survey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ func TestPromptUserForValues(t *testing.T) {
},
input: "↓ ↓ \r",
},
{
name: "optional_multi_select_variable",
variables: []recipe.Variable{
{Name: "VAR_1", Options: []string{"a", "b", "c"}, Multi: true, Optional: true},
},
expectedValues: recipe.VariableValues{
"VAR_1": recipe.MultiSelectValue{},
},
input: "\r",
},
{
name: "table_variable_with_arrows",
variables: []recipe.Variable{
Expand Down

0 comments on commit 0599255

Please sign in to comment.