Skip to content

Commit 88840be

Browse files
committed
generic: enum - include valid symbols in error msg
1 parent 556dbbf commit 88840be

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

internal/generic/enum.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ type Enum interface {
1212
fmt.Stringer
1313
}
1414

15-
// TODO: this should be a constructor+method
16-
// makeEnum(start, end) Enum; Enum.Parse(s)
1715
func ParseEnum[e Enum](start, end e, s string) (e, error) {
1816
normalized := strings.ToLower(s)
1917
for enum := start; enum <= end; enum++ {
@@ -22,5 +20,12 @@ func ParseEnum[e Enum](start, end e, s string) (e, error) {
2220
return enum, nil
2321
}
2422
}
25-
return start, fmt.Errorf("invalid Enum: \"%s\"", s)
23+
valids := make([]string, end)
24+
for i, sl := 0, start; sl <= end; i, sl = i+1, sl+1 {
25+
valids[i] = fmt.Sprintf(`"%s"`, sl.String())
26+
}
27+
return start, fmt.Errorf(
28+
`invalid Enum: "%s", want one of: %s`,
29+
s, strings.Join(valids, ", "),
30+
)
2631
}

0 commit comments

Comments
 (0)