♻️ Quality: internal/elicit without smells (7 → 0)
- ParseLine: defaultAnswer and parseConfirm; parseChoice with numberedOption and chosenOption; normalizeDefault with orEmpty - Content: contentValue and addOther; valuesOf with textValues - content.go (answers ↔ content) and line_multi.go by theme Checked: ParseLine (8 questions × 28 lines), the defaults, Content and FromContent (3 contents) byte-identical old vs new (temporary golden test). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8b276c0 parent: 6bd1c61 added
internal/elicit/content.go +131 -0 | new file mode 100644 | ||
| @@ -0,0 +1,131 @@ | ||
| 1 | +package elicit | |
| 2 | + | |
| 3 | +import "strings" | |
| 4 | + | |
| 5 | +// Content is an answered form as an elicitation response's `content`: a | |
| 6 | +// string per Choice and Text, a list of strings per Multi, a boolean per | |
| 7 | +// Confirm. | |
| 8 | +func Content(f Form, answers []Answer) map[string]any { | |
| 9 | + content := map[string]any{} | |
| 10 | + for i, q := range f.Questions { | |
| 11 | + if i >= len(answers) { | |
| 12 | + break | |
| 13 | + } | |
| 14 | + a := answers[i] | |
| 15 | + if v, ok := contentValue(q, a.Values); ok { | |
| 16 | + content[q.Name] = v | |
| 17 | + } | |
| 18 | + if q.AllowOther && (a.Other != "" || a.OtherAsked) { | |
| 19 | + addOther(content, q, a) | |
| 20 | + } | |
| 21 | + } | |
| 22 | + return content | |
| 23 | +} | |
| 24 | + | |
| 25 | +// contentValue is a question's values as its content: a list for a Multi, | |
| 26 | +// a boolean for a Confirm, the first value otherwise — none without values. | |
| 27 | +func contentValue(q Question, values []string) (any, bool) { | |
| 28 | + switch q.Kind { | |
| 29 | + case Multi: | |
| 30 | + return toAny(values), true | |
| 31 | + case Confirm: | |
| 32 | + return len(values) > 0 && values[0] == "yes", true | |
| 33 | + } | |
| 34 | + if len(values) == 0 { | |
| 35 | + return nil, false | |
| 36 | + } | |
| 37 | + return values[0], true | |
| 38 | +} | |
| 39 | + | |
| 40 | +// addOther writes the user's own answer: a Choice's value becomes the Other | |
| 41 | +// row, and the typed text goes in <name>_other. | |
| 42 | +func addOther(content map[string]any, q Question, a Answer) { | |
| 43 | + if q.Kind == Choice { | |
| 44 | + content[q.Name] = OtherLabel | |
| 45 | + } | |
| 46 | + if a.Other != "" { | |
| 47 | + content[OtherName(q)] = a.Other | |
| 48 | + } | |
| 49 | +} | |
| 50 | + | |
| 51 | +// FromContent reads an accepted elicitation's `content` back into answers. | |
| 52 | +// The client should have validated it; this reads leniently anyway (a number | |
| 53 | +// as text, a single string where a list was expected), since what it reads | |
| 54 | +// goes to a model, not into a program. | |
| 55 | +func FromContent(f Form, content map[string]any) Result { | |
| 56 | + res := Result{Status: Answered} | |
| 57 | + for _, q := range f.Questions { | |
| 58 | + res.Answers = append(res.Answers, answerOf(q, content)) | |
| 59 | + } | |
| 60 | + return res | |
| 61 | +} | |
| 62 | + | |
| 63 | +// answerOf reads one question's answer from a form's content: its values, | |
| 64 | +// and — when it allows another answer — the text typed in <name>_other. | |
| 65 | +// OtherLabel among the values is the Other row, not an option; picked | |
| 66 | +// with nothing typed, the model is told to ask (OtherAsked). | |
| 67 | +func answerOf(q Question, content map[string]any) Answer { | |
| 68 | + a := Answer{Values: valuesOf(q, content[q.Name])} | |
| 69 | + if !q.AllowOther { | |
| 70 | + return a | |
| 71 | + } | |
| 72 | + picked := false | |
| 73 | + kept := a.Values[:0] | |
| 74 | + for _, v := range a.Values { | |
| 75 | + if v == OtherLabel || isOtherOption(v) { | |
| 76 | + picked = true | |
| 77 | + continue | |
| 78 | + } | |
| 79 | + kept = append(kept, v) | |
| 80 | + } | |
| 81 | + a.Values = kept | |
| 82 | + a.Other = strings.TrimSpace(str(content[OtherName(q)])) | |
| 83 | + if q.Kind == Choice && a.Other != "" { | |
| 84 | + a.Values = nil // a choice has one answer: the typed one wins | |
| 85 | + } | |
| 86 | + a.OtherAsked = picked && a.Other == "" | |
| 87 | + if len(a.Values) == 0 { | |
| 88 | + a.Values = nil | |
| 89 | + } | |
| 90 | + return a | |
| 91 | +} | |
| 92 | + | |
| 93 | +// valuesOf reads a content value leniently: a boolean as yes or no, a | |
| 94 | +// list as its non-empty strings, anything else as text. | |
| 95 | +func valuesOf(q Question, v any) []string { | |
| 96 | + switch x := v.(type) { | |
| 97 | + case nil: | |
| 98 | + return nil | |
| 99 | + case bool: | |
| 100 | + return []string{yesNo(x)} | |
| 101 | + case []any: | |
| 102 | + var out []string | |
| 103 | + for _, item := range x { | |
| 104 | + if s := str(item); s != "" { | |
| 105 | + out = append(out, s) | |
| 106 | + } | |
| 107 | + } | |
| 108 | + return out | |
| 109 | + } | |
| 110 | + return textValues(q, str(v)) | |
| 111 | +} | |
| 112 | + | |
| 113 | +// textValues is a single value read as text: none when empty, a yes or a no | |
| 114 | +// for a Confirm that reads as one. | |
| 115 | +func textValues(q Question, s string) []string { | |
| 116 | + if s == "" { | |
| 117 | + return nil | |
| 118 | + } | |
| 119 | + if yes, ok := parseYesNo(s); ok && q.Kind == Confirm { | |
| 120 | + return []string{yesNo(yes)} | |
| 121 | + } | |
| 122 | + return []string{s} | |
| 123 | +} | |
| 124 | + | |
| 125 | +func toAny(list []string) []any { | |
| 126 | + out := make([]any, len(list)) | |
| 127 | + for i, s := range list { | |
| 128 | + out[i] = s | |
| 129 | + } | |
| 130 | + return out | |
| 131 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | +package elicit | ||
| 2 | + | ||
| 3 | +import "strings" | ||
| 4 | + | ||
| 5 | +// Content is an answered form as an elicitation response's `content`: a | ||
| 6 | +// string per Choice and Text, a list of strings per Multi, a boolean per | ||
| 7 | +// Confirm. | ||
| 8 | +func Content(f Form, answers []Answer) map[string]any { | ||
| 9 | + content := map[string]any{} | ||
| 10 | + for i, q := range f.Questions { | ||
| 11 | + if i >= len(answers) { | ||
| 12 | + break | ||
| 13 | + } | ||
| 14 | + a := answers[i] | ||
| 15 | + if v, ok := contentValue(q, a.Values); ok { | ||
| 16 | + content[q.Name] = v | ||
| 17 | + } | ||
| 18 | + if q.AllowOther && (a.Other != "" || a.OtherAsked) { | ||
| 19 | + addOther(content, q, a) | ||
| 20 | + } | ||
| 21 | + } | ||
| 22 | + return content | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +// contentValue is a question's values as its content: a list for a Multi, | ||
| 26 | +// a boolean for a Confirm, the first value otherwise — none without values. | ||
| 27 | +func contentValue(q Question, values []string) (any, bool) { | ||
| 28 | + switch q.Kind { | ||
| 29 | + case Multi: | ||
| 30 | + return toAny(values), true | ||
| 31 | + case Confirm: | ||
| 32 | + return len(values) > 0 && values[0] == "yes", true | ||
| 33 | + } | ||
| 34 | + if len(values) == 0 { | ||
| 35 | + return nil, false | ||
| 36 | + } | ||
| 37 | + return values[0], true | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +// addOther writes the user's own answer: a Choice's value becomes the Other | ||
| 41 | +// row, and the typed text goes in <name>_other. | ||
| 42 | +func addOther(content map[string]any, q Question, a Answer) { | ||
| 43 | + if q.Kind == Choice { | ||
| 44 | + content[q.Name] = OtherLabel | ||
| 45 | + } | ||
| 46 | + if a.Other != "" { | ||
| 47 | + content[OtherName(q)] = a.Other | ||
| 48 | + } | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +// FromContent reads an accepted elicitation's `content` back into answers. | ||
| 52 | +// The client should have validated it; this reads leniently anyway (a number | ||
| 53 | +// as text, a single string where a list was expected), since what it reads | ||
| 54 | +// goes to a model, not into a program. | ||
| 55 | +func FromContent(f Form, content map[string]any) Result { | ||
| 56 | + res := Result{Status: Answered} | ||
| 57 | + for _, q := range f.Questions { | ||
| 58 | + res.Answers = append(res.Answers, answerOf(q, content)) | ||
| 59 | + } | ||
| 60 | + return res | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +// answerOf reads one question's answer from a form's content: its values, | ||
| 64 | +// and — when it allows another answer — the text typed in <name>_other. | ||
| 65 | +// OtherLabel among the values is the Other row, not an option; picked | ||
| 66 | +// with nothing typed, the model is told to ask (OtherAsked). | ||
| 67 | +func answerOf(q Question, content map[string]any) Answer { | ||
| 68 | + a := Answer{Values: valuesOf(q, content[q.Name])} | ||
| 69 | + if !q.AllowOther { | ||
| 70 | + return a | ||
| 71 | + } | ||
| 72 | + picked := false | ||
| 73 | + kept := a.Values[:0] | ||
| 74 | + for _, v := range a.Values { | ||
| 75 | + if v == OtherLabel || isOtherOption(v) { | ||
| 76 | + picked = true | ||
| 77 | + continue | ||
| 78 | + } | ||
| 79 | + kept = append(kept, v) | ||
| 80 | + } | ||
| 81 | + a.Values = kept | ||
| 82 | + a.Other = strings.TrimSpace(str(content[OtherName(q)])) | ||
| 83 | + if q.Kind == Choice && a.Other != "" { | ||
| 84 | + a.Values = nil // a choice has one answer: the typed one wins | ||
| 85 | + } | ||
| 86 | + a.OtherAsked = picked && a.Other == "" | ||
| 87 | + if len(a.Values) == 0 { | ||
| 88 | + a.Values = nil | ||
| 89 | + } | ||
| 90 | + return a | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +// valuesOf reads a content value leniently: a boolean as yes or no, a | ||
| 94 | +// list as its non-empty strings, anything else as text. | ||
| 95 | +func valuesOf(q Question, v any) []string { | ||
| 96 | + switch x := v.(type) { | ||
| 97 | + case nil: | ||
| 98 | + return nil | ||
| 99 | + case bool: | ||
| 100 | + return []string{yesNo(x)} | ||
| 101 | + case []any: | ||
| 102 | + var out []string | ||
| 103 | + for _, item := range x { | ||
| 104 | + if s := str(item); s != "" { | ||
| 105 | + out = append(out, s) | ||
| 106 | + } | ||
| 107 | + } | ||
| 108 | + return out | ||
| 109 | + } | ||
| 110 | + return textValues(q, str(v)) | ||
| 111 | +} | ||
| 112 | + | ||
| 113 | +// textValues is a single value read as text: none when empty, a yes or a no | ||
| 114 | +// for a Confirm that reads as one. | ||
| 115 | +func textValues(q Question, s string) []string { | ||
| 116 | + if s == "" { | ||
| 117 | + return nil | ||
| 118 | + } | ||
| 119 | + if yes, ok := parseYesNo(s); ok && q.Kind == Confirm { | ||
| 120 | + return []string{yesNo(yes)} | ||
| 121 | + } | ||
| 122 | + return []string{s} | ||
| 123 | +} | ||
| 124 | + | ||
| 125 | +func toAny(list []string) []any { | ||
| 126 | + out := make([]any, len(list)) | ||
| 127 | + for i, s := range list { | ||
| 128 | + out[i] = s | ||
| 129 | + } | ||
| 130 | + return out | ||
| 131 | +} | ||
modified
internal/elicit/elicit.go +11 -8 | @@ -234,21 +234,24 @@ func normalizeDefault(q Question, raw string) string { | ||
| 234 | 234 | raw = strings.TrimSpace(raw) |
| 235 | 235 | switch q.Kind { |
| 236 | 236 | case Choice: |
| 237 | - if option, ok := matchOption(q.Options, raw); ok { | |
| 238 | - return option | |
| 239 | - } | |
| 240 | - return "" | |
| 237 | + return orEmpty(matchOption(q.Options, raw)) | |
| 241 | 238 | case Confirm: |
| 242 | - if yes, ok := parseYesNo(raw); ok { | |
| 243 | - return yesNo(yes) | |
| 244 | - } | |
| 245 | - return "" | |
| 239 | + yes, ok := parseYesNo(raw) | |
| 240 | + return orEmpty(yesNo(yes), ok) | |
| 246 | 241 | case Text: |
| 247 | 242 | return raw |
| 248 | 243 | } |
| 249 | 244 | return "" // Multi: no default |
| 250 | 245 | } |
| 251 | 246 | |
| 247 | +// orEmpty is s when ok, "" otherwise. | |
| 248 | +func orEmpty(s string, ok bool) string { | |
| 249 | + if !ok { | |
| 250 | + return "" | |
| 251 | + } | |
| 252 | + return s | |
| 253 | +} | |
| 254 | + | |
| 252 | 255 | // otherWords are the options a model writes for "something else": they are |
| 253 | 256 | // the Other row mm adds, not options of their own. |
| 254 | 257 | var otherWords = map[string]bool{ |
| @@ -234,21 +234,24 @@ func normalizeDefault(q Question, raw string) string { | |||
| 234 | raw = strings.TrimSpace(raw) | 234 | raw = strings.TrimSpace(raw) |
| 235 | switch q.Kind { | 235 | switch q.Kind { |
| 236 | case Choice: | 236 | case Choice: |
| 237 | - if option, ok := matchOption(q.Options, raw); ok { | 237 | + return orEmpty(matchOption(q.Options, raw)) |
| 238 | - return option | ||
| 239 | - } | ||
| 240 | - return "" | ||
| 241 | case Confirm: | 238 | case Confirm: |
| 242 | - if yes, ok := parseYesNo(raw); ok { | 239 | + yes, ok := parseYesNo(raw) |
| 243 | - return yesNo(yes) | 240 | + return orEmpty(yesNo(yes), ok) |
| 244 | - } | ||
| 245 | - return "" | ||
| 246 | case Text: | 241 | case Text: |
| 247 | return raw | 242 | return raw |
| 248 | } | 243 | } |
| 249 | return "" // Multi: no default | 244 | return "" // Multi: no default |
| 250 | } | 245 | } |
| 251 | 246 | ||
| 247 | +// orEmpty is s when ok, "" otherwise. | ||
| 248 | +func orEmpty(s string, ok bool) string { | ||
| 249 | + if !ok { | ||
| 250 | + return "" | ||
| 251 | + } | ||
| 252 | + return s | ||
| 253 | +} | ||
| 254 | + | ||
| 252 | // otherWords are the options a model writes for "something else": they are | 255 | // otherWords are the options a model writes for "something else": they are |
| 253 | // the Other row mm adds, not options of their own. | 256 | // the Other row mm adds, not options of their own. |
| 254 | var otherWords = map[string]bool{ | 257 | var otherWords = map[string]bool{ |
modified
internal/elicit/line.go +39 -85 | @@ -90,10 +90,7 @@ func Hint(q Question) string { | ||
| 90 | 90 | func ParseLine(q Question, line string) (Answer, bool) { |
| 91 | 91 | line = strings.TrimSpace(line) |
| 92 | 92 | if line == "" { |
| 93 | - if q.Default == "" { | |
| 94 | - return Answer{}, false | |
| 95 | - } | |
| 96 | - return Answer{Values: []string{q.Default}}, true | |
| 93 | + return defaultAnswer(q) | |
| 97 | 94 | } |
| 98 | 95 | switch q.Kind { |
| 99 | 96 | case Choice: |
| @@ -101,15 +98,29 @@ func ParseLine(q Question, line string) (Answer, bool) { | ||
| 101 | 98 | case Multi: |
| 102 | 99 | return parseMulti(q, line) |
| 103 | 100 | case Confirm: |
| 104 | - yes, ok := parseYesNo(line) | |
| 105 | - if !ok { | |
| 106 | - return Answer{}, false | |
| 107 | - } | |
| 108 | - return Answer{Values: []string{yesNo(yes)}}, true | |
| 101 | + return parseConfirm(line) | |
| 109 | 102 | } |
| 110 | 103 | return Answer{Values: []string{line}}, true |
| 111 | 104 | } |
| 112 | 105 | |
| 106 | +// defaultAnswer is what an empty line answers: the default, when there is | |
| 107 | +// one. | |
| 108 | +func defaultAnswer(q Question) (Answer, bool) { | |
| 109 | + if q.Default == "" { | |
| 110 | + return Answer{}, false | |
| 111 | + } | |
| 112 | + return Answer{Values: []string{q.Default}}, true | |
| 113 | +} | |
| 114 | + | |
| 115 | +// parseConfirm: a yes or a no, in English or in French. | |
| 116 | +func parseConfirm(line string) (Answer, bool) { | |
| 117 | + yes, ok := parseYesNo(line) | |
| 118 | + if !ok { | |
| 119 | + return Answer{}, false | |
| 120 | + } | |
| 121 | + return Answer{Values: []string{yesNo(yes)}}, true | |
| 122 | +} | |
| 123 | + | |
| 113 | 124 | // parseChoice: a number in range, an option's text or the start of exactly |
| 114 | 125 | // one option ("n" for "No" — models ask yes/no as a choice too), or — like |
| 115 | 126 | // the "Other" row of a menu — any other text, taken as the answer: the |
| @@ -118,15 +129,9 @@ func ParseLine(q Question, line string) (Answer, bool) { | ||
| 118 | 129 | // IsOtherPick. |
| 119 | 130 | func parseChoice(q Question, line string) (Answer, bool) { |
| 120 | 131 | if n, err := strconv.Atoi(line); err == nil { |
| 121 | - if n < 1 || n > len(q.Options) { | |
| 122 | - return Answer{}, false | |
| 123 | - } | |
| 124 | - return Answer{Values: []string{q.Options[n-1]}}, true | |
| 132 | + return numberedOption(q, n) | |
| 125 | 133 | } |
| 126 | - if option, ok := matchOption(q.Options, line); ok { | |
| 127 | - return Answer{Values: []string{option}}, true | |
| 128 | - } | |
| 129 | - if option, ok := matchPrefix(q.Options, line); ok { | |
| 134 | + if option, ok := chosenOption(q.Options, line); ok { | |
| 130 | 135 | return Answer{Values: []string{option}}, true |
| 131 | 136 | } |
| 132 | 137 | if q.AllowOther { |
| @@ -135,6 +140,23 @@ func parseChoice(q Question, line string) (Answer, bool) { | ||
| 135 | 140 | return Answer{Values: []string{line}}, true |
| 136 | 141 | } |
| 137 | 142 | |
| 143 | +// numberedOption is option n (from 1); a number out of range answers | |
| 144 | +// nothing. | |
| 145 | +func numberedOption(q Question, n int) (Answer, bool) { | |
| 146 | + if n < 1 || n > len(q.Options) { | |
| 147 | + return Answer{}, false | |
| 148 | + } | |
| 149 | + return Answer{Values: []string{q.Options[n-1]}}, true | |
| 150 | +} | |
| 151 | + | |
| 152 | +// chosenOption is the option the text names, or the one option it starts. | |
| 153 | +func chosenOption(options []string, text string) (string, bool) { | |
| 154 | + if option, ok := matchOption(options, text); ok { | |
| 155 | + return option, true | |
| 156 | + } | |
| 157 | + return matchPrefix(options, text) | |
| 158 | +} | |
| 159 | + | |
| 138 | 160 | // matchPrefix finds the one option the text starts, ignoring case; two |
| 139 | 161 | // candidates or none, and the text stands for itself. |
| 140 | 162 | func matchPrefix(options []string, text string) (string, bool) { |
| @@ -150,74 +172,6 @@ func matchPrefix(options []string, text string) (string, bool) { | ||
| 150 | 172 | return found, found != "" |
| 151 | 173 | } |
| 152 | 174 | |
| 153 | -// parseMulti: numbers separated by commas or spaces, every one in range. | |
| 154 | -// Duplicates count once; the options keep their order. When the question | |
| 155 | -// allows another answer, the line may also hold text — "1, 3, a QA box" — | |
| 156 | -// which is the user's own answer (Other), beside the ticked options. | |
| 157 | -func parseMulti(q Question, line string) (Answer, bool) { | |
| 158 | - picked := make([]bool, len(q.Options)) | |
| 159 | - var other []string | |
| 160 | - for _, item := range strings.FieldsFunc(line, func(r rune) bool { return r == ',' || r == ';' }) { | |
| 161 | - item = strings.TrimSpace(item) | |
| 162 | - numbers, ok := pickNumbers(q, item, picked) | |
| 163 | - switch { | |
| 164 | - case ok: | |
| 165 | - case numbers: | |
| 166 | - return Answer{}, false // a number out of range | |
| 167 | - default: | |
| 168 | - if option, found := matchOption(q.Options, item); found { | |
| 169 | - picked[indexOf(q.Options, option)] = true | |
| 170 | - continue | |
| 171 | - } | |
| 172 | - if !q.AllowOther { | |
| 173 | - return Answer{}, false | |
| 174 | - } | |
| 175 | - other = append(other, item) | |
| 176 | - } | |
| 177 | - } | |
| 178 | - var a Answer | |
| 179 | - for i, on := range picked { | |
| 180 | - if on { | |
| 181 | - a.Values = append(a.Values, q.Options[i]) | |
| 182 | - } | |
| 183 | - } | |
| 184 | - a.Other = strings.Join(other, ", ") | |
| 185 | - return a, len(a.Values) > 0 || a.Other != "" | |
| 186 | -} | |
| 187 | - | |
| 188 | -// pickNumbers ticks the options an item names by number ("1 3" counts as | |
| 189 | -// two). numbers says the item is made of numbers only; ok that every one is | |
| 190 | -// in range. | |
| 191 | -func pickNumbers(q Question, item string, picked []bool) (numbers, ok bool) { | |
| 192 | - fields := strings.Fields(item) | |
| 193 | - var ns []int | |
| 194 | - for _, field := range fields { | |
| 195 | - n, err := strconv.Atoi(field) | |
| 196 | - if err != nil { | |
| 197 | - return false, false | |
| 198 | - } | |
| 199 | - ns = append(ns, n) | |
| 200 | - } | |
| 201 | - for _, n := range ns { | |
| 202 | - if n < 1 || n > len(q.Options) { | |
| 203 | - return true, false | |
| 204 | - } | |
| 205 | - } | |
| 206 | - for _, n := range ns { | |
| 207 | - picked[n-1] = true | |
| 208 | - } | |
| 209 | - return len(ns) > 0, len(ns) > 0 | |
| 210 | -} | |
| 211 | - | |
| 212 | -func indexOf(list []string, s string) int { | |
| 213 | - for i, x := range list { | |
| 214 | - if x == s { | |
| 215 | - return i | |
| 216 | - } | |
| 217 | - } | |
| 218 | - return -1 | |
| 219 | -} | |
| 220 | - | |
| 221 | 175 | // IsSkip reports whether a typed line declines the question. |
| 222 | 176 | func IsSkip(line string) bool { |
| 223 | 177 | return strings.EqualFold(strings.TrimSpace(line), "/skip") |
| @@ -90,10 +90,7 @@ func Hint(q Question) string { | |||
| 90 | func ParseLine(q Question, line string) (Answer, bool) { | 90 | func ParseLine(q Question, line string) (Answer, bool) { |
| 91 | line = strings.TrimSpace(line) | 91 | line = strings.TrimSpace(line) |
| 92 | if line == "" { | 92 | if line == "" { |
| 93 | - if q.Default == "" { | 93 | + return defaultAnswer(q) |
| 94 | - return Answer{}, false | ||
| 95 | - } | ||
| 96 | - return Answer{Values: []string{q.Default}}, true | ||
| 97 | } | 94 | } |
| 98 | switch q.Kind { | 95 | switch q.Kind { |
| 99 | case Choice: | 96 | case Choice: |
| @@ -101,15 +98,29 @@ func ParseLine(q Question, line string) (Answer, bool) { | |||
| 101 | case Multi: | 98 | case Multi: |
| 102 | return parseMulti(q, line) | 99 | return parseMulti(q, line) |
| 103 | case Confirm: | 100 | case Confirm: |
| 104 | - yes, ok := parseYesNo(line) | 101 | + return parseConfirm(line) |
| 105 | - if !ok { | ||
| 106 | - return Answer{}, false | ||
| 107 | - } | ||
| 108 | - return Answer{Values: []string{yesNo(yes)}}, true | ||
| 109 | } | 102 | } |
| 110 | return Answer{Values: []string{line}}, true | 103 | return Answer{Values: []string{line}}, true |
| 111 | } | 104 | } |
| 112 | 105 | ||
| 106 | +// defaultAnswer is what an empty line answers: the default, when there is | ||
| 107 | +// one. | ||
| 108 | +func defaultAnswer(q Question) (Answer, bool) { | ||
| 109 | + if q.Default == "" { | ||
| 110 | + return Answer{}, false | ||
| 111 | + } | ||
| 112 | + return Answer{Values: []string{q.Default}}, true | ||
| 113 | +} | ||
| 114 | + | ||
| 115 | +// parseConfirm: a yes or a no, in English or in French. | ||
| 116 | +func parseConfirm(line string) (Answer, bool) { | ||
| 117 | + yes, ok := parseYesNo(line) | ||
| 118 | + if !ok { | ||
| 119 | + return Answer{}, false | ||
| 120 | + } | ||
| 121 | + return Answer{Values: []string{yesNo(yes)}}, true | ||
| 122 | +} | ||
| 123 | + | ||
| 113 | // parseChoice: a number in range, an option's text or the start of exactly | 124 | // parseChoice: a number in range, an option's text or the start of exactly |
| 114 | // one option ("n" for "No" — models ask yes/no as a choice too), or — like | 125 | // one option ("n" for "No" — models ask yes/no as a choice too), or — like |
| 115 | // the "Other" row of a menu — any other text, taken as the answer: the | 126 | // the "Other" row of a menu — any other text, taken as the answer: the |
| @@ -118,15 +129,9 @@ func ParseLine(q Question, line string) (Answer, bool) { | |||
| 118 | // IsOtherPick. | 129 | // IsOtherPick. |
| 119 | func parseChoice(q Question, line string) (Answer, bool) { | 130 | func parseChoice(q Question, line string) (Answer, bool) { |
| 120 | if n, err := strconv.Atoi(line); err == nil { | 131 | if n, err := strconv.Atoi(line); err == nil { |
| 121 | - if n < 1 || n > len(q.Options) { | 132 | + return numberedOption(q, n) |
| 122 | - return Answer{}, false | ||
| 123 | - } | ||
| 124 | - return Answer{Values: []string{q.Options[n-1]}}, true | ||
| 125 | } | 133 | } |
| 126 | - if option, ok := matchOption(q.Options, line); ok { | 134 | + if option, ok := chosenOption(q.Options, line); ok { |
| 127 | - return Answer{Values: []string{option}}, true | ||
| 128 | - } | ||
| 129 | - if option, ok := matchPrefix(q.Options, line); ok { | ||
| 130 | return Answer{Values: []string{option}}, true | 135 | return Answer{Values: []string{option}}, true |
| 131 | } | 136 | } |
| 132 | if q.AllowOther { | 137 | if q.AllowOther { |
| @@ -135,6 +140,23 @@ func parseChoice(q Question, line string) (Answer, bool) { | |||
| 135 | return Answer{Values: []string{line}}, true | 140 | return Answer{Values: []string{line}}, true |
| 136 | } | 141 | } |
| 137 | 142 | ||
| 143 | +// numberedOption is option n (from 1); a number out of range answers | ||
| 144 | +// nothing. | ||
| 145 | +func numberedOption(q Question, n int) (Answer, bool) { | ||
| 146 | + if n < 1 || n > len(q.Options) { | ||
| 147 | + return Answer{}, false | ||
| 148 | + } | ||
| 149 | + return Answer{Values: []string{q.Options[n-1]}}, true | ||
| 150 | +} | ||
| 151 | + | ||
| 152 | +// chosenOption is the option the text names, or the one option it starts. | ||
| 153 | +func chosenOption(options []string, text string) (string, bool) { | ||
| 154 | + if option, ok := matchOption(options, text); ok { | ||
| 155 | + return option, true | ||
| 156 | + } | ||
| 157 | + return matchPrefix(options, text) | ||
| 158 | +} | ||
| 159 | + | ||
| 138 | // matchPrefix finds the one option the text starts, ignoring case; two | 160 | // matchPrefix finds the one option the text starts, ignoring case; two |
| 139 | // candidates or none, and the text stands for itself. | 161 | // candidates or none, and the text stands for itself. |
| 140 | func matchPrefix(options []string, text string) (string, bool) { | 162 | func matchPrefix(options []string, text string) (string, bool) { |
| @@ -150,74 +172,6 @@ func matchPrefix(options []string, text string) (string, bool) { | |||
| 150 | return found, found != "" | 172 | return found, found != "" |
| 151 | } | 173 | } |
| 152 | 174 | ||
| 153 | -// parseMulti: numbers separated by commas or spaces, every one in range. | ||
| 154 | -// Duplicates count once; the options keep their order. When the question | ||
| 155 | -// allows another answer, the line may also hold text — "1, 3, a QA box" — | ||
| 156 | -// which is the user's own answer (Other), beside the ticked options. | ||
| 157 | -func parseMulti(q Question, line string) (Answer, bool) { | ||
| 158 | - picked := make([]bool, len(q.Options)) | ||
| 159 | - var other []string | ||
| 160 | - for _, item := range strings.FieldsFunc(line, func(r rune) bool { return r == ',' || r == ';' }) { | ||
| 161 | - item = strings.TrimSpace(item) | ||
| 162 | - numbers, ok := pickNumbers(q, item, picked) | ||
| 163 | - switch { | ||
| 164 | - case ok: | ||
| 165 | - case numbers: | ||
| 166 | - return Answer{}, false // a number out of range | ||
| 167 | - default: | ||
| 168 | - if option, found := matchOption(q.Options, item); found { | ||
| 169 | - picked[indexOf(q.Options, option)] = true | ||
| 170 | - continue | ||
| 171 | - } | ||
| 172 | - if !q.AllowOther { | ||
| 173 | - return Answer{}, false | ||
| 174 | - } | ||
| 175 | - other = append(other, item) | ||
| 176 | - } | ||
| 177 | - } | ||
| 178 | - var a Answer | ||
| 179 | - for i, on := range picked { | ||
| 180 | - if on { | ||
| 181 | - a.Values = append(a.Values, q.Options[i]) | ||
| 182 | - } | ||
| 183 | - } | ||
| 184 | - a.Other = strings.Join(other, ", ") | ||
| 185 | - return a, len(a.Values) > 0 || a.Other != "" | ||
| 186 | -} | ||
| 187 | - | ||
| 188 | -// pickNumbers ticks the options an item names by number ("1 3" counts as | ||
| 189 | -// two). numbers says the item is made of numbers only; ok that every one is | ||
| 190 | -// in range. | ||
| 191 | -func pickNumbers(q Question, item string, picked []bool) (numbers, ok bool) { | ||
| 192 | - fields := strings.Fields(item) | ||
| 193 | - var ns []int | ||
| 194 | - for _, field := range fields { | ||
| 195 | - n, err := strconv.Atoi(field) | ||
| 196 | - if err != nil { | ||
| 197 | - return false, false | ||
| 198 | - } | ||
| 199 | - ns = append(ns, n) | ||
| 200 | - } | ||
| 201 | - for _, n := range ns { | ||
| 202 | - if n < 1 || n > len(q.Options) { | ||
| 203 | - return true, false | ||
| 204 | - } | ||
| 205 | - } | ||
| 206 | - for _, n := range ns { | ||
| 207 | - picked[n-1] = true | ||
| 208 | - } | ||
| 209 | - return len(ns) > 0, len(ns) > 0 | ||
| 210 | -} | ||
| 211 | - | ||
| 212 | -func indexOf(list []string, s string) int { | ||
| 213 | - for i, x := range list { | ||
| 214 | - if x == s { | ||
| 215 | - return i | ||
| 216 | - } | ||
| 217 | - } | ||
| 218 | - return -1 | ||
| 219 | -} | ||
| 220 | - | ||
| 221 | // IsSkip reports whether a typed line declines the question. | 175 | // IsSkip reports whether a typed line declines the question. |
| 222 | func IsSkip(line string) bool { | 176 | func IsSkip(line string) bool { |
| 223 | return strings.EqualFold(strings.TrimSpace(line), "/skip") | 177 | return strings.EqualFold(strings.TrimSpace(line), "/skip") |
added
internal/elicit/line_multi.go +74 -0 | new file mode 100644 | ||
| @@ -0,0 +1,74 @@ | ||
| 1 | +package elicit | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "strconv" | |
| 5 | + "strings" | |
| 6 | +) | |
| 7 | + | |
| 8 | +// parseMulti: numbers separated by commas or spaces, every one in range. | |
| 9 | +// Duplicates count once; the options keep their order. When the question | |
| 10 | +// allows another answer, the line may also hold text — "1, 3, a QA box" — | |
| 11 | +// which is the user's own answer (Other), beside the ticked options. | |
| 12 | +func parseMulti(q Question, line string) (Answer, bool) { | |
| 13 | + picked := make([]bool, len(q.Options)) | |
| 14 | + var other []string | |
| 15 | + for _, item := range strings.FieldsFunc(line, func(r rune) bool { return r == ',' || r == ';' }) { | |
| 16 | + item = strings.TrimSpace(item) | |
| 17 | + numbers, ok := pickNumbers(q, item, picked) | |
| 18 | + switch { | |
| 19 | + case ok: | |
| 20 | + case numbers: | |
| 21 | + return Answer{}, false // a number out of range | |
| 22 | + default: | |
| 23 | + if option, found := matchOption(q.Options, item); found { | |
| 24 | + picked[indexOf(q.Options, option)] = true | |
| 25 | + continue | |
| 26 | + } | |
| 27 | + if !q.AllowOther { | |
| 28 | + return Answer{}, false | |
| 29 | + } | |
| 30 | + other = append(other, item) | |
| 31 | + } | |
| 32 | + } | |
| 33 | + var a Answer | |
| 34 | + for i, on := range picked { | |
| 35 | + if on { | |
| 36 | + a.Values = append(a.Values, q.Options[i]) | |
| 37 | + } | |
| 38 | + } | |
| 39 | + a.Other = strings.Join(other, ", ") | |
| 40 | + return a, len(a.Values) > 0 || a.Other != "" | |
| 41 | +} | |
| 42 | + | |
| 43 | +// pickNumbers ticks the options an item names by number ("1 3" counts as | |
| 44 | +// two). numbers says the item is made of numbers only; ok that every one is | |
| 45 | +// in range. | |
| 46 | +func pickNumbers(q Question, item string, picked []bool) (numbers, ok bool) { | |
| 47 | + fields := strings.Fields(item) | |
| 48 | + var ns []int | |
| 49 | + for _, field := range fields { | |
| 50 | + n, err := strconv.Atoi(field) | |
| 51 | + if err != nil { | |
| 52 | + return false, false | |
| 53 | + } | |
| 54 | + ns = append(ns, n) | |
| 55 | + } | |
| 56 | + for _, n := range ns { | |
| 57 | + if n < 1 || n > len(q.Options) { | |
| 58 | + return true, false | |
| 59 | + } | |
| 60 | + } | |
| 61 | + for _, n := range ns { | |
| 62 | + picked[n-1] = true | |
| 63 | + } | |
| 64 | + return len(ns) > 0, len(ns) > 0 | |
| 65 | +} | |
| 66 | + | |
| 67 | +func indexOf(list []string, s string) int { | |
| 68 | + for i, x := range list { | |
| 69 | + if x == s { | |
| 70 | + return i | |
| 71 | + } | |
| 72 | + } | |
| 73 | + return -1 | |
| 74 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | +package elicit | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "strconv" | ||
| 5 | + "strings" | ||
| 6 | +) | ||
| 7 | + | ||
| 8 | +// parseMulti: numbers separated by commas or spaces, every one in range. | ||
| 9 | +// Duplicates count once; the options keep their order. When the question | ||
| 10 | +// allows another answer, the line may also hold text — "1, 3, a QA box" — | ||
| 11 | +// which is the user's own answer (Other), beside the ticked options. | ||
| 12 | +func parseMulti(q Question, line string) (Answer, bool) { | ||
| 13 | + picked := make([]bool, len(q.Options)) | ||
| 14 | + var other []string | ||
| 15 | + for _, item := range strings.FieldsFunc(line, func(r rune) bool { return r == ',' || r == ';' }) { | ||
| 16 | + item = strings.TrimSpace(item) | ||
| 17 | + numbers, ok := pickNumbers(q, item, picked) | ||
| 18 | + switch { | ||
| 19 | + case ok: | ||
| 20 | + case numbers: | ||
| 21 | + return Answer{}, false // a number out of range | ||
| 22 | + default: | ||
| 23 | + if option, found := matchOption(q.Options, item); found { | ||
| 24 | + picked[indexOf(q.Options, option)] = true | ||
| 25 | + continue | ||
| 26 | + } | ||
| 27 | + if !q.AllowOther { | ||
| 28 | + return Answer{}, false | ||
| 29 | + } | ||
| 30 | + other = append(other, item) | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + var a Answer | ||
| 34 | + for i, on := range picked { | ||
| 35 | + if on { | ||
| 36 | + a.Values = append(a.Values, q.Options[i]) | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | + a.Other = strings.Join(other, ", ") | ||
| 40 | + return a, len(a.Values) > 0 || a.Other != "" | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +// pickNumbers ticks the options an item names by number ("1 3" counts as | ||
| 44 | +// two). numbers says the item is made of numbers only; ok that every one is | ||
| 45 | +// in range. | ||
| 46 | +func pickNumbers(q Question, item string, picked []bool) (numbers, ok bool) { | ||
| 47 | + fields := strings.Fields(item) | ||
| 48 | + var ns []int | ||
| 49 | + for _, field := range fields { | ||
| 50 | + n, err := strconv.Atoi(field) | ||
| 51 | + if err != nil { | ||
| 52 | + return false, false | ||
| 53 | + } | ||
| 54 | + ns = append(ns, n) | ||
| 55 | + } | ||
| 56 | + for _, n := range ns { | ||
| 57 | + if n < 1 || n > len(q.Options) { | ||
| 58 | + return true, false | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + for _, n := range ns { | ||
| 62 | + picked[n-1] = true | ||
| 63 | + } | ||
| 64 | + return len(ns) > 0, len(ns) > 0 | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | +func indexOf(list []string, s string) int { | ||
| 68 | + for i, x := range list { | ||
| 69 | + if x == s { | ||
| 70 | + return i | ||
| 71 | + } | ||
| 72 | + } | ||
| 73 | + return -1 | ||
| 74 | +} | ||
modified
internal/elicit/schema.go +0 -110 | @@ -182,113 +182,3 @@ func firstText(values ...any) string { | ||
| 182 | 182 | } |
| 183 | 183 | return "" |
| 184 | 184 | } |
| 185 | - | |
| 186 | -// Content is an answered form as an elicitation response's `content`: a | |
| 187 | -// string per Choice and Text, a list of strings per Multi, a boolean per | |
| 188 | -// Confirm. | |
| 189 | -func Content(f Form, answers []Answer) map[string]any { | |
| 190 | - content := map[string]any{} | |
| 191 | - for i, q := range f.Questions { | |
| 192 | - if i >= len(answers) { | |
| 193 | - break | |
| 194 | - } | |
| 195 | - a := answers[i] | |
| 196 | - values := a.Values | |
| 197 | - switch q.Kind { | |
| 198 | - case Multi: | |
| 199 | - content[q.Name] = toAny(values) | |
| 200 | - case Confirm: | |
| 201 | - content[q.Name] = len(values) > 0 && values[0] == "yes" | |
| 202 | - default: | |
| 203 | - if len(values) > 0 { | |
| 204 | - content[q.Name] = values[0] | |
| 205 | - } | |
| 206 | - } | |
| 207 | - if q.AllowOther && (a.Other != "" || a.OtherAsked) { | |
| 208 | - if q.Kind == Choice { | |
| 209 | - content[q.Name] = OtherLabel | |
| 210 | - } | |
| 211 | - if a.Other != "" { | |
| 212 | - content[OtherName(q)] = a.Other | |
| 213 | - } | |
| 214 | - } | |
| 215 | - } | |
| 216 | - return content | |
| 217 | -} | |
| 218 | - | |
| 219 | -// FromContent reads an accepted elicitation's `content` back into answers. | |
| 220 | -// The client should have validated it; this reads leniently anyway (a number | |
| 221 | -// as text, a single string where a list was expected), since what it reads | |
| 222 | -// goes to a model, not into a program. | |
| 223 | -func FromContent(f Form, content map[string]any) Result { | |
| 224 | - res := Result{Status: Answered} | |
| 225 | - for _, q := range f.Questions { | |
| 226 | - res.Answers = append(res.Answers, answerOf(q, content)) | |
| 227 | - } | |
| 228 | - return res | |
| 229 | -} | |
| 230 | - | |
| 231 | -// answerOf reads one question's answer from a form's content: its values, | |
| 232 | -// and — when it allows another answer — the text typed in <name>_other. | |
| 233 | -// OtherLabel among the values is the Other row, not an option; picked | |
| 234 | -// with nothing typed, the model is told to ask (OtherAsked). | |
| 235 | -func answerOf(q Question, content map[string]any) Answer { | |
| 236 | - a := Answer{Values: valuesOf(q, content[q.Name])} | |
| 237 | - if !q.AllowOther { | |
| 238 | - return a | |
| 239 | - } | |
| 240 | - picked := false | |
| 241 | - kept := a.Values[:0] | |
| 242 | - for _, v := range a.Values { | |
| 243 | - if v == OtherLabel || isOtherOption(v) { | |
| 244 | - picked = true | |
| 245 | - continue | |
| 246 | - } | |
| 247 | - kept = append(kept, v) | |
| 248 | - } | |
| 249 | - a.Values = kept | |
| 250 | - a.Other = strings.TrimSpace(str(content[OtherName(q)])) | |
| 251 | - if q.Kind == Choice && a.Other != "" { | |
| 252 | - a.Values = nil // a choice has one answer: the typed one wins | |
| 253 | - } | |
| 254 | - a.OtherAsked = picked && a.Other == "" | |
| 255 | - if len(a.Values) == 0 { | |
| 256 | - a.Values = nil | |
| 257 | - } | |
| 258 | - return a | |
| 259 | -} | |
| 260 | - | |
| 261 | -func valuesOf(q Question, v any) []string { | |
| 262 | - switch x := v.(type) { | |
| 263 | - case nil: | |
| 264 | - return nil | |
| 265 | - case bool: | |
| 266 | - return []string{yesNo(x)} | |
| 267 | - case []any: | |
| 268 | - var out []string | |
| 269 | - for _, item := range x { | |
| 270 | - if s := str(item); s != "" { | |
| 271 | - out = append(out, s) | |
| 272 | - } | |
| 273 | - } | |
| 274 | - return out | |
| 275 | - } | |
| 276 | - s := str(v) | |
| 277 | - if s == "" { | |
| 278 | - return nil | |
| 279 | - } | |
| 280 | - if q.Kind == Confirm { | |
| 281 | - if yes, ok := parseYesNo(s); ok { | |
| 282 | - return []string{yesNo(yes)} | |
| 283 | - } | |
| 284 | - } | |
| 285 | - return []string{s} | |
| 286 | -} | |
| 287 | - | |
| 288 | -func toAny(list []string) []any { | |
| 289 | - out := make([]any, len(list)) | |
| 290 | - for i, s := range list { | |
| 291 | - out[i] = s | |
| 292 | - } | |
| 293 | - return out | |
| 294 | -} | |
| @@ -182,113 +182,3 @@ func firstText(values ...any) string { | |||
| 182 | } | 182 | } |
| 183 | return "" | 183 | return "" |
| 184 | } | 184 | } |
| 185 | - | ||
| 186 | -// Content is an answered form as an elicitation response's `content`: a | ||
| 187 | -// string per Choice and Text, a list of strings per Multi, a boolean per | ||
| 188 | -// Confirm. | ||
| 189 | -func Content(f Form, answers []Answer) map[string]any { | ||
| 190 | - content := map[string]any{} | ||
| 191 | - for i, q := range f.Questions { | ||
| 192 | - if i >= len(answers) { | ||
| 193 | - break | ||
| 194 | - } | ||
| 195 | - a := answers[i] | ||
| 196 | - values := a.Values | ||
| 197 | - switch q.Kind { | ||
| 198 | - case Multi: | ||
| 199 | - content[q.Name] = toAny(values) | ||
| 200 | - case Confirm: | ||
| 201 | - content[q.Name] = len(values) > 0 && values[0] == "yes" | ||
| 202 | - default: | ||
| 203 | - if len(values) > 0 { | ||
| 204 | - content[q.Name] = values[0] | ||
| 205 | - } | ||
| 206 | - } | ||
| 207 | - if q.AllowOther && (a.Other != "" || a.OtherAsked) { | ||
| 208 | - if q.Kind == Choice { | ||
| 209 | - content[q.Name] = OtherLabel | ||
| 210 | - } | ||
| 211 | - if a.Other != "" { | ||
| 212 | - content[OtherName(q)] = a.Other | ||
| 213 | - } | ||
| 214 | - } | ||
| 215 | - } | ||
| 216 | - return content | ||
| 217 | -} | ||
| 218 | - | ||
| 219 | -// FromContent reads an accepted elicitation's `content` back into answers. | ||
| 220 | -// The client should have validated it; this reads leniently anyway (a number | ||
| 221 | -// as text, a single string where a list was expected), since what it reads | ||
| 222 | -// goes to a model, not into a program. | ||
| 223 | -func FromContent(f Form, content map[string]any) Result { | ||
| 224 | - res := Result{Status: Answered} | ||
| 225 | - for _, q := range f.Questions { | ||
| 226 | - res.Answers = append(res.Answers, answerOf(q, content)) | ||
| 227 | - } | ||
| 228 | - return res | ||
| 229 | -} | ||
| 230 | - | ||
| 231 | -// answerOf reads one question's answer from a form's content: its values, | ||
| 232 | -// and — when it allows another answer — the text typed in <name>_other. | ||
| 233 | -// OtherLabel among the values is the Other row, not an option; picked | ||
| 234 | -// with nothing typed, the model is told to ask (OtherAsked). | ||
| 235 | -func answerOf(q Question, content map[string]any) Answer { | ||
| 236 | - a := Answer{Values: valuesOf(q, content[q.Name])} | ||
| 237 | - if !q.AllowOther { | ||
| 238 | - return a | ||
| 239 | - } | ||
| 240 | - picked := false | ||
| 241 | - kept := a.Values[:0] | ||
| 242 | - for _, v := range a.Values { | ||
| 243 | - if v == OtherLabel || isOtherOption(v) { | ||
| 244 | - picked = true | ||
| 245 | - continue | ||
| 246 | - } | ||
| 247 | - kept = append(kept, v) | ||
| 248 | - } | ||
| 249 | - a.Values = kept | ||
| 250 | - a.Other = strings.TrimSpace(str(content[OtherName(q)])) | ||
| 251 | - if q.Kind == Choice && a.Other != "" { | ||
| 252 | - a.Values = nil // a choice has one answer: the typed one wins | ||
| 253 | - } | ||
| 254 | - a.OtherAsked = picked && a.Other == "" | ||
| 255 | - if len(a.Values) == 0 { | ||
| 256 | - a.Values = nil | ||
| 257 | - } | ||
| 258 | - return a | ||
| 259 | -} | ||
| 260 | - | ||
| 261 | -func valuesOf(q Question, v any) []string { | ||
| 262 | - switch x := v.(type) { | ||
| 263 | - case nil: | ||
| 264 | - return nil | ||
| 265 | - case bool: | ||
| 266 | - return []string{yesNo(x)} | ||
| 267 | - case []any: | ||
| 268 | - var out []string | ||
| 269 | - for _, item := range x { | ||
| 270 | - if s := str(item); s != "" { | ||
| 271 | - out = append(out, s) | ||
| 272 | - } | ||
| 273 | - } | ||
| 274 | - return out | ||
| 275 | - } | ||
| 276 | - s := str(v) | ||
| 277 | - if s == "" { | ||
| 278 | - return nil | ||
| 279 | - } | ||
| 280 | - if q.Kind == Confirm { | ||
| 281 | - if yes, ok := parseYesNo(s); ok { | ||
| 282 | - return []string{yesNo(yes)} | ||
| 283 | - } | ||
| 284 | - } | ||
| 285 | - return []string{s} | ||
| 286 | -} | ||
| 287 | - | ||
| 288 | -func toAny(list []string) []any { | ||
| 289 | - out := make([]any, len(list)) | ||
| 290 | - for i, s := range list { | ||
| 291 | - out[i] = s | ||
| 292 | - } | ||
| 293 | - return out | ||
| 294 | -} | ||