Skip to content

Commit

Permalink
Refactor input options and fix ordering in selection
Browse files Browse the repository at this point in the history
  • Loading branch information
evilmarty committed May 25, 2022
1 parent 99951dc commit e986f97
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
29 changes: 20 additions & 9 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,39 @@ var (

type InputValues map[string]any

type InputOptions map[string]string
type InputOption struct {
Label string
Value string
}

type InputOptions []InputOption

func (x *InputOptions) UnmarshalYAML(node *yaml.Node) error {
var mapValue map[string]string
var options InputOptions

switch node.Kind {
case yaml.SequenceNode:
var seqValue []string
if err := node.Decode(&seqValue); err != nil {
return err
}
mapValue = make(map[string]string, len(seqValue))
for _, item := range seqValue {
mapValue[item] = item
options = make(InputOptions, len(seqValue))
for i, item := range seqValue {
options[i].Label = item
options[i].Value = item
}
case yaml.MappingNode:
var mapValue map[string]string
if err := node.Decode(&mapValue); err != nil {
return err
}
options = make(InputOptions, 0, len(mapValue))
for label, value := range mapValue {
options = append(options, InputOption{Label: label, Value: value})
}
}

*x = mapValue
*x = options

return nil
}
Expand All @@ -69,7 +80,7 @@ func (input Input) Valid(value any) bool {

func (input Input) contains(value any) bool {
for _, option := range input.Options {
if option == value {
if option.Value == value {
return true
}
}
Expand All @@ -88,8 +99,8 @@ func (input Input) matches(value any) bool {
func (input Input) choose() (any, error) {
var choices = make([]*selection.Choice, 0, len(input.Options))
prompt := fmt.Sprintf("%s %s", promptStyle.Render("Choose a"), inputNameStyle.Render(input.Name))
for label, value := range input.Options {
choices = append(choices, &selection.Choice{String: label, Value: value})
for _, option := range input.Options {
choices = append(choices, &selection.Choice{String: option.Label, Value: option.Value})
}
sp := selection.New(prompt, choices)

Expand Down
20 changes: 10 additions & 10 deletions input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func TestInputOptionsUnmarshalYAMLSequence(t *testing.T) {
- Starscream
`
expected := InputOptions{
"Megatron": "Megatron",
"Soundwave": "Soundwave",
"Starscream": "Starscream",
InputOption{Label: "Megatron", Value: "Megatron"},
InputOption{Label: "Soundwave", Value: "Soundwave"},
InputOption{Label: "Starscream", Value: "Starscream"},
}
err := yaml.Unmarshal([]byte(content), &actual)

Expand All @@ -38,9 +38,9 @@ Optimus Prime: Autobot
Optimus Primal: Maximal
`
expected := InputOptions{
"Megatron": "Decepticon",
"Optimus Prime": "Autobot",
"Optimus Primal": "Maximal",
InputOption{Label: "Megatron", Value: "Decepticon"},
InputOption{Label: "Optimus Prime", Value: "Autobot"},
InputOption{Label: "Optimus Primal", Value: "Maximal"},
}
err := yaml.Unmarshal([]byte(content), &actual)

Expand Down Expand Up @@ -83,8 +83,8 @@ city:
func TestInputSelectable_WithOptions(t *testing.T) {
input := Input{
Options: InputOptions{
"a": "1",
"b": "2",
InputOption{Label: "a", Value: "1"},
InputOption{Label: "b", Value: "2"},
},
}

Expand All @@ -104,8 +104,8 @@ func TestInputSelectable_WithoutOptions(t *testing.T) {
func TestInputValid_WithOptions(t *testing.T) {
input := Input{
Options: InputOptions{
"a": "1",
"b": "2",
InputOption{Label: "a", Value: "1"},
InputOption{Label: "b", Value: "2"},
},
}

Expand Down

0 comments on commit e986f97

Please sign in to comment.