-
Notifications
You must be signed in to change notification settings - Fork 4
/
variant.go
174 lines (136 loc) · 3.78 KB
/
variant.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
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
package libcmd
import (
"fmt"
"reflect"
"strconv"
)
type variant struct {
raw interface{}
refValue reflect.Value
defaultValue reflect.Value
isBool bool
isStr bool
isSet bool
}
func varFromInterface(target, defaultValue interface{}) *variant {
ref := reflect.ValueOf(target)
if ref.Kind() == reflect.Ptr {
if ref.IsNil() {
panic("nil pointer on variant creation")
}
ref = reflect.Indirect(ref)
}
def := reflect.ValueOf(defaultValue)
return varFromReflect(target, ref, def)
}
func varFromReflect(raw interface{}, target, defaultValue reflect.Value) *variant {
return &variant{
raw: raw,
refValue: target,
defaultValue: defaultValue,
isBool: target.Kind() == reflect.Bool,
isStr: target.Kind() == reflect.String,
}
}
func varFromCustom(target CustomArg, defaultValue string) *variant {
return &variant{
raw: target,
refValue: reflect.ValueOf(target),
defaultValue: reflect.ValueOf(defaultValue),
isStr: true,
}
}
func (v *variant) setValue(value string) error {
if v.refValue.Type().Implements(customArgType) {
ca, _ := v.refValue.Interface().(CustomArg)
if err := ca.Set(value); err != nil {
return err
}
v.isSet = true
return nil
}
converted, err := valueAsKind(value, v.refValue.Kind(), v.refValue.Type())
if err != nil {
return err
}
v.refValue.Set(converted)
v.isSet = true
return nil
}
func (v *variant) useDefault() error {
if v.isSet {
return nil
}
if v.refValue.Type().Implements(customArgType) {
ca, _ := v.refValue.Interface().(CustomArg)
str := v.defaultValue.String()
return ca.Set(str) //nolint: errcheck
}
zero := reflect.Zero(v.refValue.Type())
defaultIsZero := zero.Interface() == v.defaultValue.Interface()
valueIsZero := zero.Interface() == v.refValue.Interface()
// if we have a value, and our default is zero, we keep it
if defaultIsZero && !valueIsZero {
return nil
}
v.refValue.Set(v.defaultValue)
return nil
}
func (v *variant) defaultAsString() string {
zero := reflect.Zero(v.refValue.Type())
if zero.Interface() == v.defaultValue.Interface() {
return ""
}
return fmt.Sprintf("%v", v.defaultValue.Interface())
}
// return the value converted to a *COMPATIBLE* kind, optionally casted to
// an *EXACT* type
func valueAsKind(value string, kind reflect.Kind, exactType reflect.Type) (reflect.Value, error) {
// used only for numeric types; 'simpler' types are returned directly
name := kind.String()
size := bitSizeOf(kind)
var parsed interface{}
var err error
switch kind {
case reflect.String:
return reflect.ValueOf(value), nil
case reflect.Bool:
vv, err := strconv.ParseBool(value)
if err != nil {
return reflect.Value{}, conversionErr{value: value, typeName: "boolean"}
}
return reflect.ValueOf(vv), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
parsed, err = strconv.ParseInt(value, 10, size)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
parsed, err = strconv.ParseUint(value, 10, size)
case reflect.Float32, reflect.Float64:
parsed, err = strconv.ParseFloat(value, size)
}
if err != nil {
return reflect.Value{}, conversionErr{value: value, typeName: name}
}
if parsed == nil {
return reflect.Value{}, unsupportedErr{value: value, typeName: name}
}
converted := reflect.ValueOf(parsed)
if exactType != nil {
converted = converted.Convert(exactType)
}
return converted, nil
}
// bit size of numeric types
func bitSizeOf(kind reflect.Kind) int {
switch kind {
case reflect.Int8, reflect.Uint8:
return 8
case reflect.Int16, reflect.Uint16:
return 16
case reflect.Int32, reflect.Uint32, reflect.Float32:
return 32
case reflect.Int64, reflect.Uint64, reflect.Float64:
return 64
default:
return 0
}
}