-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag_reflect_setter.go
81 lines (77 loc) · 2.17 KB
/
flag_reflect_setter.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
package clif
import (
"context"
"reflect"
)
func newValueFromValueSetter(ctx context.Context, flag FlagValue, target reflect.Value) (reflect.Value, error) {
methodName := "SetFromFlagValue"
receiver := recursivelyPopulatePointer(ctx, target)
method := receiver.MethodByName(methodName)
if !method.IsValid() {
return target, FlagSetterMethodInvalidError{
Receiver: receiver.Interface(),
Method: methodName,
}
}
results := method.Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(flag)})
err := results[0].Interface()
if err != nil {
if e, ok := err.(error); ok {
return target, e
}
return target, FlagSetterResponseError{
Receiver: receiver.Interface(),
Method: methodName,
Value: err,
}
}
return receiver, nil
}
func newValuesFromValuesSetter(ctx context.Context, values FlagValues, target reflect.Value) (reflect.Value, error) {
methodName := "SetFromFlagValues"
receiver := recursivelyPopulatePointer(ctx, target)
method := receiver.MethodByName(methodName)
if !method.IsValid() {
return target, FlagSetterMethodInvalidError{
Receiver: receiver.Interface(),
Method: methodName,
}
}
results := method.Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(values)})
err := results[0].Interface()
if err != nil {
if e, ok := err.(error); ok {
return target, e
}
return target, FlagSetterResponseError{
Receiver: receiver.Interface(),
Method: methodName,
Value: err,
}
}
return receiver, nil
}
func newSetFromSetter(ctx context.Context, flags FlagSet, target reflect.Value) (reflect.Value, error) {
methodName := "SetFromFlagSet"
receiver := recursivelyPopulatePointer(ctx, target)
method := receiver.MethodByName(methodName)
if !method.IsValid() {
return target, FlagSetterMethodInvalidError{
Receiver: receiver.Interface(),
Method: methodName,
}
}
results := method.Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(flags)})
err := results[0].Interface()
if err != nil {
if e, ok := err.(error); ok {
return target, e
}
return target, FlagSetterResponseError{
Receiver: receiver.Interface(),
Method: methodName,
Value: err,
}
}
return receiver, nil
}