Skip to content

Commit

Permalink
fix: Increase range of types accepted by promptChoice template function
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Mar 27, 2024
1 parent f490429 commit ad77158
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
4 changes: 2 additions & 2 deletions internal/cmd/executetemplatecmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func (c *Config) runExecuteTemplateCmd(cmd *cobra.Command, args []string) error
return promptBoolInitTemplateFunc(field, args...)
}

promptChoiceInitTemplateFunc := func(prompt string, choices []any, args ...string) string {
choiceStrs, err := anySliceToStringSlice(choices)
promptChoiceInitTemplateFunc := func(prompt string, choices any, args ...string) string {
choiceStrs, err := anyToStringSlice(choices)
if err != nil {
panic(err)
}
Expand Down
43 changes: 29 additions & 14 deletions internal/cmd/interactivetemplatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (c *Config) promptBoolOnceInteractiveTemplateFunc(m map[string]any, path an
return c.promptBoolInteractiveTemplateFunc(prompt, args...)
}

func (c *Config) promptChoiceInteractiveTemplateFunc(prompt string, choices []any, args ...string) string {
func (c *Config) promptChoiceInteractiveTemplateFunc(prompt string, choices any, args ...string) string {
if len(args) > 1 {
err := fmt.Errorf("want 2 or 3 arguments, got %d", len(args)+2)
panic(err)
Expand All @@ -113,7 +113,7 @@ func (c *Config) promptChoiceInteractiveTemplateFunc(prompt string, choices []an
return valueStr
}

choiceStrs, err := anySliceToStringSlice(choices)
choiceStrs, err := anyToStringSlice(choices)
if err != nil {
panic(err)
}
Expand All @@ -129,7 +129,7 @@ func (c *Config) promptChoiceOnceInteractiveTemplateFunc(
m map[string]any,
path any,
prompt string,
choices []any,
choices any,
args ...string,
) string {
if len(args) > 1 {
Expand Down Expand Up @@ -228,17 +228,32 @@ func (c *Config) promptStringOnceInteractiveTemplateFunc(m map[string]any, path
return c.promptStringInteractiveTemplateFunc(prompt, args...)
}

func anySliceToStringSlice(slice []any) ([]string, error) {
result := make([]string, 0, len(slice))
for _, elem := range slice {
switch elem := elem.(type) {
case []byte:
result = append(result, string(elem))
case string:
result = append(result, elem)
default:
return nil, fmt.Errorf("%v: not a string", elem)
func anyToString(v any) (string, error) {
switch v := v.(type) {
case []byte:
return string(v), nil
case string:
return v, nil
default:
return "", fmt.Errorf("%v: not a string", v)
}
}

func anyToStringSlice(slice any) ([]string, error) {
switch slice := slice.(type) {
case []any:
result := make([]string, 0, len(slice))
for _, elem := range slice {
elemStr, err := anyToString(elem)
if err != nil {
return nil, err
}
result = append(result, elemStr)
}
return result, nil
case []string:
return slice, nil
default:
return nil, fmt.Errorf("%v: not a slice", slice)
}
return result, nil
}
10 changes: 5 additions & 5 deletions internal/cmd/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ func (c *Config) eqFoldTemplateFunc(first, second string, more ...string) bool {
return false
}

func (c *Config) findExecutableTemplateFunc(file string, pathList []any) string {
func (c *Config) findExecutableTemplateFunc(file string, pathList any) string {
files := []string{file}
paths, err := anySliceToStringSlice(pathList)
paths, err := anyToStringSlice(pathList)
if err != nil {
panic(fmt.Errorf("path list: %w", err))
}
Expand All @@ -147,13 +147,13 @@ func (c *Config) findExecutableTemplateFunc(file string, pathList []any) string
}
}

func (c *Config) findOneExecutableTemplateFunc(fileList, pathList []any) string {
files, err := anySliceToStringSlice(fileList)
func (c *Config) findOneExecutableTemplateFunc(fileList, pathList any) string {
files, err := anyToStringSlice(fileList)
if err != nil {
panic(fmt.Errorf("file list: %w", err))
}

paths, err := anySliceToStringSlice(pathList)
paths, err := anyToStringSlice(pathList)
if err != nil {
panic(fmt.Errorf("path list: %w", err))
}
Expand Down

0 comments on commit ad77158

Please sign in to comment.