|
| 1 | +//go:build go1.23 |
| 2 | + |
| 3 | +package dials |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "maps" |
| 8 | + "slices" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +// ValueMap provides a mapping from a string to a type of your choice to be used |
| 13 | +// like an enum. Users should implement the `DialsValueMap()` method to provide |
| 14 | +// all possible mappings values. Any values not in the mapping will fail when |
| 15 | +// unmarshaling. |
| 16 | +type ValueMap[T any] interface { |
| 17 | + DialsValueMap() map[string]T |
| 18 | +} |
| 19 | + |
| 20 | +// Enum allows you to treat a certain type like an enumeration. Enums are |
| 21 | +// integrated into the flags packages to automatically amend the `Usage` text to |
| 22 | +// include all allowed values. |
| 23 | +type Enum[T ValueMap[T]] struct { |
| 24 | + Value T |
| 25 | +} |
| 26 | + |
| 27 | +// EnumValuable is an interface that is useful to get all the allowed enum |
| 28 | +// values as strings. It is used by the flag packages to adjust the usage text |
| 29 | +// to include all available values. |
| 30 | +type EnumValuable interface { |
| 31 | + AllowedEnumValues() []string |
| 32 | +} |
| 33 | + |
| 34 | +// AllowedEnumValues implements the [EnumValuable] interface. |
| 35 | +func (e Enum[T]) AllowedEnumValues() []string { |
| 36 | + return slices.Sorted(maps.Keys(e.Value.DialsValueMap())) |
| 37 | +} |
| 38 | + |
| 39 | +// UnmarshalText implements [encoding.TextUnmarshaler] so that we can map from |
| 40 | +// strings to our typed enum. If there is no appropriate mapping, |
| 41 | +// [ErrInvalidEnumValue] is returned. |
| 42 | +func (e *Enum[T]) UnmarshalText(text []byte) error { |
| 43 | + allVals := e.Value.DialsValueMap() |
| 44 | + if mapping, ok := allVals[string(text)]; ok { |
| 45 | + e.Value = mapping |
| 46 | + return nil |
| 47 | + } |
| 48 | + return &ErrInvalidEnumValue{ |
| 49 | + Input: string(text), |
| 50 | + Allowed: e.AllowedEnumValues(), |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// FuzzyEnum is just like an [Enum], but does case-insensitive comparisons. Just |
| 55 | +// like Enums, FuzzyEnums are integrated into the flags packages to |
| 56 | +// automatically amend the `Usage` text to include all allowed values and will |
| 57 | +// also indicate that the matching is case-insensitive. |
| 58 | +type FuzzyEnum[T ValueMap[T]] struct { |
| 59 | + Value T |
| 60 | +} |
| 61 | + |
| 62 | +// FuzzyEnumComparer is an interface that allows us to detect that |
| 63 | +// case-insensitive comparison has been used. |
| 64 | +type FuzzyEnumComparer interface { |
| 65 | + isFuzzy() |
| 66 | +} |
| 67 | + |
| 68 | +// AllowedEnumValues implements the [EnumValuable] interface. |
| 69 | +func (f FuzzyEnum[T]) AllowedEnumValues() []string { |
| 70 | + return slices.Sorted(maps.Keys(f.Value.DialsValueMap())) |
| 71 | +} |
| 72 | + |
| 73 | +// isFuzzy implements the FuzzyEnumComparer interface. |
| 74 | +func (f FuzzyEnum[T]) isFuzzy() {} |
| 75 | + |
| 76 | +// UnmarshalText implements [encoding.TextUnmarshaler] and does a case-insensitive |
| 77 | +// comparison of the string to map back to the appropriate value from the |
| 78 | +// enumeration. If there is no appropriate mapping, [ErrInvalidEnumValue] is |
| 79 | +// returned. |
| 80 | +func (f *FuzzyEnum[T]) UnmarshalText(text []byte) error { |
| 81 | + allVals := f.Value.DialsValueMap() |
| 82 | + noCase := make(map[string]T, len(allVals)) |
| 83 | + for k, v := range allVals { |
| 84 | + noCase[strings.ToLower(k)] = v |
| 85 | + } |
| 86 | + |
| 87 | + if mapping, ok := noCase[strings.ToLower(string(text))]; ok { |
| 88 | + f.Value = mapping |
| 89 | + return nil |
| 90 | + } |
| 91 | + |
| 92 | + return &ErrInvalidEnumValue{ |
| 93 | + Input: string(text), |
| 94 | + Allowed: f.AllowedEnumValues(), |
| 95 | + Fuzzy: true, |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// ErrInvalidEnumValue is an error that is returned when there is no mapping for |
| 100 | +// a particular value in the enumeration. |
| 101 | +type ErrInvalidEnumValue struct { |
| 102 | + // All the allowed inputs. |
| 103 | + Allowed []string |
| 104 | + // The input that was provided (not in the Allowed list). |
| 105 | + Input string |
| 106 | + // true if the comparison is case-insensitive. |
| 107 | + Fuzzy bool |
| 108 | +} |
| 109 | + |
| 110 | +// Error implements the error interface. |
| 111 | +func (e *ErrInvalidEnumValue) Error() string { |
| 112 | + return fmt.Sprintf("value %q is not part of the allowed enumeration: %+v, fuzzy: %t", e.Input, e.Allowed, e.Fuzzy) |
| 113 | +} |
| 114 | + |
| 115 | +// StringValueMap is a helper that converts string-ish typed-enums to the |
| 116 | +// mapping table expected by the [ValueMap] interface. |
| 117 | +func StringValueMap[T ~string](in ...T) map[string]T { |
| 118 | + mapping := make(map[string]T, len(in)) |
| 119 | + for _, element := range in { |
| 120 | + mapping[string(element)] = element |
| 121 | + } |
| 122 | + return mapping |
| 123 | +} |
| 124 | + |
| 125 | +// StringerValueMap is a helper that converts types that implement the |
| 126 | +// [fmt.Stringer] interface to the mapping table expected by the [ValueMap] |
| 127 | +// interface. |
| 128 | +func StringerValueMap[T fmt.Stringer](in ...T) map[string]T { |
| 129 | + mapping := make(map[string]T, len(in)) |
| 130 | + for _, element := range in { |
| 131 | + mapping[element.String()] = element |
| 132 | + } |
| 133 | + return mapping |
| 134 | +} |
| 135 | + |
| 136 | +// EnumValue is a helper that's particularly useful when setting enum defaults in configuration. |
| 137 | +func EnumValue[T ValueMap[T]](in T) Enum[T] { |
| 138 | + return Enum[T]{ |
| 139 | + Value: in, |
| 140 | + } |
| 141 | +} |
0 commit comments