(ask Asker, message string, items []T, getKey func(item T) string)
| 66 | } |
| 67 | |
| 68 | func SelectMapWithNew[T any](ask Asker, message string, items []T, getKey func(item T) string) (T, bool, error) { |
| 69 | optionMap, options := MakeItemMapAndOptions(items, getKey) |
| 70 | var selectedValue T |
| 71 | var selectedKey string |
| 72 | if err := ask(&surveyext.Select{ |
| 73 | Message: message, |
| 74 | Options: options, |
| 75 | }, &selectedKey); err != nil { |
| 76 | return selectedValue, false, err |
| 77 | } |
| 78 | if selectedKey == constants.PromptCreateNew { |
| 79 | return *new(T), true, nil |
| 80 | } |
| 81 | selectedValue, ok := optionMap[selectedKey] |
| 82 | if !ok { // without this explict check SelectMap can return nil, nil which people don't expect |
| 83 | return *new(T), false, fmt.Errorf("SelectMap did not get valid answer (selectedKey=%s)", selectedKey) |
| 84 | } |
| 85 | return selectedValue, false, nil |
| 86 | } |
| 87 | |
| 88 | func MakeItemMapAndOptions[T any](items []T, getKey func(item T) string) (map[string]T, []string) { |
| 89 | optionMap := make(map[string]T, len(items)) |
no test coverage detected