-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_reflect_value.go
55 lines (50 loc) · 1.59 KB
/
flag_reflect_value.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
package clif
import (
"context"
"math/big"
"reflect"
"time"
)
func newValue(ctx context.Context, flag FlagValue, target reflect.Value) (reflect.Value, error) {
if target.Type().Implements(reflect.TypeOf((*FlagValueSetter)(nil)).Elem()) {
return newValueFromValueSetter(ctx, flag, target)
}
// we want to consider *big.Float and *big.Int to be numbers, not
// pointers
if target.Type() == reflect.TypeOf(big.NewFloat(0)) {
return newValueFromBigFloat(ctx, flag, target)
}
if target.Type() == reflect.TypeOf(big.NewInt(0)) {
return newValueFromBigInt(ctx, flag, target)
}
// we want to handle time.Time values even if they're structs
if target.Type() == reflect.TypeOf(time.Time{}) {
return newValueFromTime(ctx, flag, target)
}
switch target.Kind() { //nolint:exhaustive // we have a default assigned for a reason
case reflect.Bool:
return newValueFromBoolean(ctx, flag, target)
case reflect.String:
return reflect.ValueOf(flag.Raw).Convert(target.Type()), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64:
return newValueFromInt(ctx, flag, target)
case reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64:
return newValueFromUint(ctx, flag, target)
case reflect.Float32, reflect.Float64:
return newValueFromFloat(ctx, flag, target)
case reflect.Ptr:
return newValueFromPointer(ctx, flag, target)
case reflect.Struct, reflect.Slice, reflect.Map:
return target, InvalidConversionError{
Source: flag,
Target: target,
}
default:
return target, InvalidConversionError{
Source: flag,
Target: target,
}
}
}