Decoders are now field mappers.

Mappers are responsible for mapping from command-line input to Go. This
is typically just decoding, but also includes other information such as
if the field is a bool.
This commit is contained in:
Alec Thomas
2018-06-03 16:07:30 +10:00
committed by Gerald Kaszuba
parent c8b487e49c
commit 48af58cefa
9 changed files with 337 additions and 288 deletions
+5 -2
View File
@@ -130,7 +130,7 @@ type Value struct {
Name string
Help string
Default string
Decoder Decoder
Mapper Mapper
Tag *Tag
Value reflect.Value
Required bool
@@ -144,13 +144,16 @@ func (v *Value) IsCumulative() bool {
}
func (v *Value) IsBool() bool {
if m, ok := v.Mapper.(BoolMapper); ok && m.IsBool() {
return true
}
return v.Value.Kind() == reflect.Bool
}
// Parse tokens into value, parse, and validate, but do not write to the field.
func (v *Value) Parse(scan *Scanner) (reflect.Value, error) {
value := reflect.New(v.Value.Type()).Elem()
err := v.Decoder.Decode(&DecoderContext{Value: v}, scan, value)
err := v.Mapper.Decode(&DecoderContext{Value: v}, scan, value)
if err == nil {
v.Set = true
}