add AllKinds to the public API. Use it as a single source of truth.

This commit is contained in:
2026-08-01 04:18:48 +01:00
parent c55ab96a4f
commit af06fab1f1
2 changed files with 115 additions and 19 deletions

View File

@@ -1,6 +1,10 @@
package lottery
import "fmt"
import (
"fmt"
"slices"
"strings"
)
type Kind string
@@ -12,22 +16,43 @@ const (
KindPowerball Kind = "powerball"
)
func ParseKind(kind string) (Kind, error) {
switch kind {
case "lotto":
return KindLotto, nil
case "euromillions":
return KindEuroMillions, nil
case "setforlife":
return KindSetForLife, nil
case "thunderball":
return KindThunderball, nil
case "powerball":
return KindPowerball, nil
default:
return "", fmt.Errorf(
"invalid lottery kind: %s, must be one of: lotto, euromillions, setforlife, thunderball, powerball",
kind,
)
}
var allKinds = []Kind{
KindLotto,
KindEuroMillions,
KindSetForLife,
KindThunderball,
KindPowerball,
}
var allKindsText = []string{
string(KindLotto),
string(KindEuroMillions),
string(KindSetForLife),
string(KindThunderball),
string(KindPowerball),
}
var allKindsCSV = strings.Join(allKindsText, ", ")
// AllKinds returns the complete, ordered list of supported lottery kinds.
//
// The returned slice is a copy and can be modified safely by callers.
func AllKinds() []Kind {
kinds := make([]Kind, len(allKinds))
copy(kinds, allKinds)
return kinds
}
// ParseKind parses a string into a Kind, returning an error if the string is not a valid kind.
func ParseKind(kind string) (Kind, error) {
parsed := Kind(kind)
if slices.Contains(allKinds, parsed) {
return parsed, nil
}
return "", fmt.Errorf(
"invalid lottery kind: %s, must be one of: %s",
kind,
allKindsCSV,
)
}