/
mapvalue.go
77 lines (63 loc) · 1.51 KB
/
mapvalue.go
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
package goflagbuilder
import (
"fmt"
"reflect"
"strconv"
)
type mapvalue struct {
name string
mapval reflect.Value
keyval reflect.Value
elementval reflect.Value
}
func (x *mapvalue) String() string {
return x.elementval.String()
}
func (x *mapvalue) Set(str string) error {
switch x.elementval.Kind() {
case reflect.Bool:
b, err := strconv.ParseBool(str)
if err != nil {
return err
}
x.mapval.SetMapIndex(x.keyval, reflect.ValueOf(b))
case reflect.Float64:
f, err := strconv.ParseFloat(str, 64)
if err != nil {
return err
}
x.mapval.SetMapIndex(x.keyval, reflect.ValueOf(f))
case reflect.Int64:
i, err := strconv.ParseInt(str, 0, 64)
if err != nil {
return err
}
x.mapval.SetMapIndex(x.keyval, reflect.ValueOf(i))
case reflect.Int:
i, err := strconv.ParseInt(str, 0, 0)
if err != nil {
return err
}
x.mapval.SetMapIndex(x.keyval, reflect.ValueOf(i))
case reflect.String:
x.mapval.SetMapIndex(x.keyval, reflect.ValueOf(str))
case reflect.Uint64:
i, err := strconv.ParseUint(str, 0, 64)
if err != nil {
return err
}
x.mapval.SetMapIndex(x.keyval, reflect.ValueOf(i))
case reflect.Uint:
i, err := strconv.ParseUint(str, 0, 0)
if err != nil {
return err
}
x.mapval.SetMapIndex(x.keyval, reflect.ValueOf(i))
default:
return fmt.Errorf("Unsupported type %v kind %v for prefix %s", x.elementval.Type(), x.elementval.Kind(), x.name)
}
return nil
}
func (x *mapvalue) IsBoolFlag() bool {
return x.elementval.Kind() == reflect.Bool
}