-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathoption.go
More file actions
402 lines (355 loc) · 10.7 KB
/
option.go
File metadata and controls
402 lines (355 loc) · 10.7 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package figtree
import (
"encoding/json"
"fmt"
"reflect"
"regexp"
"emperror.dev/errors"
"github.com/coryb/walky"
"gopkg.in/yaml.v3"
)
const (
defaultSource = "default"
overrideSource = "override"
promptSource = "prompt"
yamlSource = "yaml"
jsonSource = "json"
)
type option interface {
IsDefined() bool
GetValue() any
SetValue(any) error
SetSource(SourceLocation)
GetSource() SourceLocation
IsDefault() bool
IsOverride() bool
}
// StringifyValue is global variable to indicate if the Option should be
// serialized as just the value (when value is true) or if the entire Option
// struct should be serialized. This is a hack, and not recommended for general
// usage, but can be useful for debugging.
var StringifyValue = true
// stringMapRegex is used in option parsing for map types Set routines
var stringMapRegex = regexp.MustCompile("[:=]")
// FileCoordinate represents the line/column of an option
type FileCoordinate struct {
Line int
Column int
}
// ideally these would be const if Go supported const structs?
var (
// DefaultSource will be the value of the `Source` property
// for Option[T] when they are constructed via `NewOption[T]`.
DefaultSource = NewSource(defaultSource)
// OverrideSource will be the value of the `Source` property
// for Option[T] when they are populated via kingpin command
// line option.
OverrideSource = NewSource(overrideSource)
)
type SourceLocation struct {
Name string
Location *FileCoordinate
}
func (s SourceLocation) String() string {
if s.Location != nil {
return fmt.Sprintf("%s:%d:%d", s.Name, s.Location.Line, s.Location.Column)
}
return s.Name
}
type SourceOption func(*SourceLocation) *SourceLocation
func WithLocation(location *FileCoordinate) SourceOption {
return func(s *SourceLocation) *SourceLocation {
s.Location = location
return s
}
}
func NewSource(name string, opts ...SourceOption) SourceLocation {
l := SourceLocation{
Name: name,
}
for _, o := range opts {
o(&l)
}
return l
}
type Option[T any] struct {
Source SourceLocation
Defined bool
Value T
}
func NewOption[T any](dflt T) Option[T] {
return Option[T]{
Source: NewSource(defaultSource),
Defined: true,
Value: dflt,
}
}
func (o Option[T]) IsDefined() bool {
return o.Defined
}
func (o *Option[T]) SetSource(source SourceLocation) {
o.Source = source
}
func (o *Option[T]) GetSource() SourceLocation {
return o.Source
}
func (o *Option[T]) IsDefault() bool {
return o.Source.Name == defaultSource
}
func (o *Option[T]) IsOverride() bool {
return o.Source.Name == overrideSource
}
func (o Option[T]) GetValue() any {
return o.Value
}
// WriteAnswer implements the Settable interface as defined by the
// survey prompting library:
// https://github.com/AlecAivazis/survey/blob/v2.3.5/core/write.go#L15-L18
func (o *Option[T]) WriteAnswer(name string, value any) error {
if v, ok := value.(T); ok {
o.Value = v
o.Defined = true
o.Source = NewSource(promptSource)
return nil
}
return errors.Errorf("Got %T expected %T type: %v", value, o.Value, value)
}
// Set implements part of the Value interface as defined by the kingpin command
// line option library:
// https://github.com/alecthomas/kingpin/blob/v1.3.4/values.go#L26-L29
func (o *Option[T]) Set(s string) error {
err := convertString(s, &o.Value)
if err != nil {
return err
}
o.Source = OverrideSource
o.Defined = true
return nil
}
// String implements part of the Value interface as defined by the kingpin
// command line option library:
// https://github.com/alecthomas/kingpin/blob/v1.3.4/values.go#L26-L29
func (o Option[T]) String() string {
if StringifyValue {
return fmt.Sprint(o.Value)
}
return fmt.Sprintf("{Source:%s Defined:%t Value:%v}", o.Source, o.Defined, o.Value)
}
// SetValue implements the Settings interface as defined by the kingpin
// command line option library:
// https://github.com/alecthomas/kingpin/blob/v1.3.4/parsers.go#L13-L15
func (o *Option[T]) SetValue(v any) error {
if val, ok := v.(T); ok {
o.Value = val
o.Defined = true
return nil
}
// look for type conversions as well, like:
// (*Option[float64]).SetValue(float32)
// There might be a better way to do this, but with
// Generics I could not find a better way to convert
// the input type to match the Option type.
dst := reflect.ValueOf(o.Value)
dstType := reflect.ValueOf(v).Type()
src := reflect.ValueOf(v)
if src.CanConvert(dstType) {
dst.Set(src.Convert(dstType))
o.Defined = true
return nil
}
return errors.Errorf("Got %T expected %T type: %v", v, o.Value, v)
}
// UnmarshalYAML implement the Unmarshaler interface used by the
// yaml library:
// https://github.com/go-yaml/yaml/blob/v3.0.1/yaml.go#L36-L38
func (o *Option[T]) UnmarshalYAML(node *yaml.Node) error {
if err := node.Decode(&o.Value); err != nil {
return walky.NewYAMLError(err, node)
}
var loc *FileCoordinate
if node.Line > 0 || node.Column > 0 {
loc = &FileCoordinate{Line: node.Line, Column: node.Column}
}
o.Source = NewSource(yamlSource, WithLocation(loc))
o.Defined = true
return nil
}
// MarshalYAML implements the Marshaler interface used by the yaml library:
// https://github.com/go-yaml/yaml/blob/v3.0.1/yaml.go#L50-L52
func (o Option[T]) MarshalYAML() (any, error) {
if StringifyValue {
// First double check if the Value has a custom Marshaler.
// Note we can't use `o.Value.(yaml.Marshaler)` directly because
// you cannot do type assertions on generic types. First we check
// if Value is a direct (non pointer) type
var q any = &o.Value
if marshaler, ok := q.(yaml.Marshaler); ok {
return marshaler.MarshalYAML()
}
// Now we try again for cases where Value is a pointer type.
q = o.Value
if marshaler, ok := q.(yaml.Marshaler); ok {
return marshaler.MarshalYAML()
}
return o.Value, nil
}
// need a copy of this struct without the MarshalYAML interface attached
return struct {
Value T
Source string
Defined bool
}{
Value: o.Value,
Source: o.Source.String(),
Defined: o.Defined,
}, nil
}
// UnmarshalJSON implements the Unmarshaler interface as defined by json:
// https://cs.opensource.google/go/go/+/refs/tags/go1.18.3:src/encoding/json/decode.go;l=118-120
func (o *Option[T]) UnmarshalJSON(b []byte) error {
if err := json.Unmarshal(b, &o.Value); err != nil {
return err
}
o.Source = NewSource(jsonSource)
o.Defined = true
return nil
}
// MarshalJSON implements the Marshaler interface as defined by json:
// https://cs.opensource.google/go/go/+/refs/tags/go1.18.3:src/encoding/json/encode.go;l=225-227
func (o Option[T]) MarshalJSON() ([]byte, error) {
if StringifyValue {
return json.Marshal(o.Value)
}
// need a copy of this struct without the MarshalJSON interface attached
return json.Marshal(struct {
Value T
Source string
Defined bool
}{
Value: o.Value,
Source: o.Source.String(),
Defined: o.Defined,
})
}
// IsBoolFlag implements part of the boolFlag interface as defined by the
// kingpin command line option library:
// https://github.com/alecthomas/kingpin/blob/v1.3.4/values.go#L42-L45
func (o Option[T]) IsBoolFlag() bool {
// TODO hopefully Go will get template specializations so we can
// implement this function specifically for Option[bool], but for
// now we have to use runtime reflection to determine the type.
v := reflect.ValueOf(o.Value)
if v.Kind() == reflect.Bool {
return true
}
return false
}
type MapOption[T any] map[string]Option[T]
// Set implements part of the Value interface as defined by the kingpin command
// line option library:
// https://github.com/alecthomas/kingpin/blob/v1.3.4/values.go#L26-L29
func (o *MapOption[T]) Set(value string) error {
parts := stringMapRegex.Split(value, 2)
if len(parts) != 2 {
return errors.Errorf("expected KEY=VALUE got '%s'", value)
}
val := Option[T]{}
if err := val.Set(parts[1]); err != nil {
return err
}
(*o)[parts[0]] = val
return nil
}
// IsCumulative implements part of the remainderArg interface as defined by the
// kingpin command line option library:
// https://github.com/alecthomas/kingpin/blob/v1.3.4/values.go#L49-L52
func (o MapOption[T]) IsCumulative() bool {
return true
}
// String implements part of the Value interface as defined by the kingpin
// command line option library:
// https://github.com/alecthomas/kingpin/blob/v1.3.4/values.go#L26-L29
func (o MapOption[T]) String() string {
return fmt.Sprint(map[string]Option[T](o))
}
func (o MapOption[T]) Map() map[string]T {
tmp := map[string]T{}
for k, v := range o {
tmp[k] = v.Value
}
return tmp
}
// WriteAnswer implements the Settable interface as defined by the
// survey prompting library:
// https://github.com/AlecAivazis/survey/blob/v2.3.5/core/write.go#L15-L18
func (o *MapOption[T]) WriteAnswer(name string, value any) error {
tmp := Option[T]{}
if v, ok := value.(T); ok {
tmp.Value = v
tmp.Defined = true
tmp.Source = NewSource(promptSource)
(*o)[name] = tmp
return nil
}
return errors.Errorf("Got %T expected %T type: %v", value, tmp.Value, value)
}
func (o MapOption[T]) IsDefined() bool {
// true if the map has any keys
return len(o) > 0
}
type ListOption[T any] []Option[T]
// Set implements part of the Value interface as defined by the kingpin command
// line option library:
// https://github.com/alecthomas/kingpin/blob/v1.3.4/values.go#L26-L29
func (o *ListOption[T]) Set(value string) error {
val := Option[T]{}
if err := val.Set(value); err != nil {
return err
}
*o = append(*o, val)
return nil
}
// WriteAnswer implements the Settable interface as defined by the
// survey prompting library:
// https://github.com/AlecAivazis/survey/blob/v2.3.5/core/write.go#L15-L18
func (o *ListOption[T]) WriteAnswer(name string, value any) error {
tmp := Option[T]{}
if v, ok := value.(T); ok {
tmp.Value = v
tmp.Defined = true
tmp.Source = NewSource(promptSource)
*o = append(*o, tmp)
return nil
}
return errors.Errorf("Got %T expected %T type: %v", value, tmp.Value, value)
}
// IsCumulative implements part of the remainderArg interface as defined by the
// kingpin command line option library:
// https://github.com/alecthomas/kingpin/blob/v1.3.4/values.go#L49-L52
func (o ListOption[T]) IsCumulative() bool {
return true
}
// String implements part of the Value interface as defined by the kingpin
// command line option library:
// https://github.com/alecthomas/kingpin/blob/v1.3.4/values.go#L26-L29
func (o ListOption[T]) String() string {
return fmt.Sprint([]Option[T](o))
}
func (o ListOption[T]) Append(values ...T) ListOption[T] {
results := o
for _, val := range values {
results = append(results, NewOption(val))
}
return results
}
func (o ListOption[T]) Slice() []T {
tmp := []T{}
for _, elem := range o {
tmp = append(tmp, elem.Value)
}
return tmp
}
func (o ListOption[T]) IsDefined() bool {
// true if the list is not empty
return len(o) > 0
}