-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum.go
More file actions
157 lines (135 loc) · 4.67 KB
/
Copy pathenum.go
File metadata and controls
157 lines (135 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package tinyflags
import (
"fmt"
"reflect"
"strconv"
"strings"
"github.com/containeroo/tinyflags/internal/dynamic"
"github.com/containeroo/tinyflags/internal/engine"
"github.com/containeroo/tinyflags/internal/scalar"
)
type enumValue interface {
~string |
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}
// EnumChoice maps one user-facing enum name to its typed value.
type EnumChoice[T enumValue] struct {
Name string
Value T
}
// Choice returns an enum choice for named enum helpers.
func Choice[T enumValue](name string, value T) EnumChoice[T] {
return EnumChoice[T]{Name: name, Value: value}
}
// EnumVar defines a typed enum flag and binds it to the given pointer.
// Only values listed in allowed are accepted.
func EnumVar[T enumValue](f *FlagSet, ptr *T, name string, def T, usage string, allowed ...T) *scalar.ScalarFlag[T] {
flag := engine.RegisterStaticScalar(
f.impl,
ptr,
name,
usage,
def,
parseEnumValue[T],
formatEnumValue[T],
)
return flag.Choices(allowed...)
}
// Enum defines a typed enum flag and returns its handle.
// Only values listed in allowed are accepted.
func Enum[T enumValue](f *FlagSet, name string, def T, usage string, allowed ...T) *scalar.ScalarFlag[T] {
return EnumVar(f, new(T), name, def, usage, allowed...)
}
// DynamicEnum defines a typed enum flag on a dynamic group.
// Only values listed in allowed are accepted.
func DynamicEnum[T enumValue](g *DynamicGroup, field string, def T, usage string, allowed ...T) *dynamic.ScalarFlag[T] {
return dynamic.Enum(g, field, def, usage, allowed...)
}
// EnumMapVar defines a typed enum flag with user-facing names and binds it to the given pointer.
func EnumMapVar[T enumValue](f *FlagSet, ptr *T, name string, def T, usage string, choices ...EnumChoice[T]) *scalar.ScalarFlag[T] {
parse, format, allowed := enumChoiceHooks(choices)
flag := engine.RegisterStaticScalar(f.impl, ptr, name, usage, def, parse, format)
flag.Allowed(allowed...)
return flag
}
// EnumMap defines a typed enum flag with user-facing names and returns its handle.
func EnumMap[T enumValue](f *FlagSet, name string, def T, usage string, choices ...EnumChoice[T]) *scalar.ScalarFlag[T] {
return EnumMapVar(f, new(T), name, def, usage, choices...)
}
// DynamicEnumMap defines a typed enum flag with user-facing names on a dynamic group.
func DynamicEnumMap[T enumValue](g *DynamicGroup, field string, def T, usage string, choices ...EnumChoice[T]) *dynamic.ScalarFlag[T] {
return dynamic.EnumMap(g, field, def, usage, dynamicEnumChoices(choices)...)
}
func parseEnumValue[T enumValue](raw string) (T, error) {
var zero T
typ := reflect.TypeOf(zero)
out := reflect.New(typ).Elem()
switch typ.Kind() {
case reflect.String:
out.SetString(raw)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := strconv.ParseInt(raw, 10, typ.Bits())
if err != nil {
return zero, err
}
out.SetInt(v)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v, err := strconv.ParseUint(raw, 10, typ.Bits())
if err != nil {
return zero, err
}
out.SetUint(v)
default:
return zero, fmt.Errorf("unsupported enum kind %s", typ.Kind())
}
return out.Interface().(T), nil
}
func formatEnumValue[T enumValue](v T) string {
value := reflect.ValueOf(v)
switch value.Kind() {
case reflect.String:
return value.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(value.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(value.Uint(), 10)
default:
return fmt.Sprintf("%v", v)
}
}
func enumChoiceHooks[T enumValue](choices []EnumChoice[T]) (func(string) (T, error), func(T) string, []string) {
names := make([]string, 0, len(choices))
byName := make(map[string]T, len(choices))
byValue := make(map[T]string, len(choices))
for _, choice := range choices {
names = append(names, choice.Name)
byName[choice.Name] = choice.Value
if _, exists := byValue[choice.Value]; !exists {
byValue[choice.Value] = choice.Name
}
}
parse := func(raw string) (T, error) {
val, ok := byName[raw]
if ok {
return val, nil
}
var zero T
return zero, fmt.Errorf("must be one of: %s", strings.Join(names, ", "))
}
format := func(v T) string {
name, ok := byValue[v]
if ok {
return name
}
return formatEnumValue(v)
}
return parse, format, names
}
func dynamicEnumChoices[T enumValue](choices []EnumChoice[T]) []dynamic.EnumChoice[T] {
out := make([]dynamic.EnumChoice[T], len(choices))
for i, choice := range choices {
out[i] = dynamic.EnumChoice[T]{Name: choice.Name, Value: choice.Value}
}
return out
}