Skip to content

Commit 0599255

Browse files
authored
feat: support optional multi select variables (#136)
1 parent 8be0fde commit 0599255

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

examples/variable-types/recipe.yml

+12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ vars:
3636
- option_1
3737
- option_2
3838
- option_3
39+
40+
- name: OPTIONAL_MULTI_SELECT_VAR
41+
description: |
42+
User chooses multiple values or no values at all from the predefined values in `options` property.
43+
44+
Defined by: non-empty `options` property, `optional: true` and `multi: true`.
45+
multi: true
46+
optional: true
47+
options:
48+
- option_1
49+
- option_2
50+
- option_3
3951

4052
- name: TABLE_VAR
4153
description: |

pkg/recipe/variable.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (v *Variable) Validate() error {
104104
if len(v.Options) > 0 {
105105
if len(v.Columns) > 0 {
106106
return errors.New("`options` and `columns` properties can not be defined at the same time")
107-
} else if v.Optional {
107+
} else if v.Optional && !v.Multi {
108108
return errors.New("select variables can not be optional")
109109
}
110110
}
@@ -184,7 +184,7 @@ func (val VariableValues) Validate() error {
184184
case string, bool, MultiSelectValue, TableValue:
185185
break
186186
default:
187-
return fmt.Errorf("unsupported variable value type")
187+
return fmt.Errorf("unsupported variable value type (%T)", v)
188188
}
189189
}
190190

@@ -350,6 +350,17 @@ func (vv *VariableValues) UnmarshalYAML(unmarshal func(interface{}) error) error
350350
Rows: rows,
351351
}
352352
}
353+
// Multiselect values without a value are an empty []interface{} and we want to cast them into MultiSelectValues
354+
case []interface{}:
355+
multiV := make(MultiSelectValue, len(v))
356+
for i, interfaceV := range v {
357+
str, ok := interfaceV.(string)
358+
if !ok {
359+
return fmt.Errorf("failed to read value to multi select value: %v", v)
360+
}
361+
multiV[i] = str
362+
}
363+
(*vv)[name] = multiV
353364
default:
354365
(*vv)[name] = v
355366
}

pkg/ui/survey/survey_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ func TestPromptUserForValues(t *testing.T) {
7373
},
7474
input: "↓ ↓ \r",
7575
},
76+
{
77+
name: "optional_multi_select_variable",
78+
variables: []recipe.Variable{
79+
{Name: "VAR_1", Options: []string{"a", "b", "c"}, Multi: true, Optional: true},
80+
},
81+
expectedValues: recipe.VariableValues{
82+
"VAR_1": recipe.MultiSelectValue{},
83+
},
84+
input: "\r",
85+
},
7686
{
7787
name: "table_variable_with_arrows",
7888
variables: []recipe.Variable{

0 commit comments

Comments
 (0)