@@ -22,6 +22,7 @@ import (
22
22
"reflect"
23
23
"strconv"
24
24
"strings"
25
+ "time"
25
26
26
27
utilnet "k8s.io/apimachinery/pkg/util/net"
27
28
)
@@ -47,54 +48,91 @@ func findNestedElement(s string, c interface{}) (reflect.Value, error) {
47
48
48
49
// setElement sets the supplied element to the value in the supplied string. The string will be coerced to the correct type.
49
50
func setElement (e reflect.Value , v string ) error {
50
- switch e .Kind () {
51
- case reflect .Int , reflect .Int32 , reflect .Int64 :
52
- i , err := strconv .Atoi (v )
51
+ switch e .Interface ().(type ) {
52
+ case int , int32 , int64 :
53
+ return convertInt (e , v )
54
+ case string :
55
+ return convertString (e , v )
56
+ case float32 , float64 :
57
+ return convertFloat (e , v )
58
+ case bool :
59
+ return convertBool (e , v )
60
+ case net.IP :
61
+ ip := net .ParseIP (v )
62
+ if ip == nil {
63
+ return fmt .Errorf ("Error converting input %s to an IP." , v )
64
+ }
65
+ e .Set (reflect .ValueOf (ip ))
66
+ case net.IPNet :
67
+ _ , cidr , err := net .ParseCIDR (v )
53
68
if err != nil {
54
- return fmt .Errorf ("Error converting input %s to an integer : %s" , v , err )
69
+ return fmt .Errorf ("Error converting input %s to a CIDR : %s" , v , err )
55
70
}
56
- e .SetInt (int64 (i ))
57
- case reflect .String :
58
- e .SetString (v )
59
- case reflect .Float32 , reflect .Float64 :
60
- f , err := strconv .ParseFloat (v , 64 )
71
+ e .Set (reflect .ValueOf (* cidr ))
72
+ case utilnet.PortRange :
73
+ pr , err := utilnet .ParsePortRange (v )
61
74
if err != nil {
62
- return fmt .Errorf ("Error converting input %s to a float : %s" , v , err )
75
+ return fmt .Errorf ("Error converting input %s to PortRange : %s" , v , err )
63
76
}
64
- e .SetFloat ( f )
65
- case reflect . Bool :
66
- b , err := strconv . ParseBool (v )
77
+ e .Set ( reflect . ValueOf ( * pr ) )
78
+ case time. Duration :
79
+ dur , err := time . ParseDuration (v )
67
80
if err != nil {
68
- return fmt .Errorf ("Error converting input %s to a bool : %s" , v , err )
81
+ return fmt .Errorf ("Error converting input %s to Duration : %s" , v , err )
69
82
}
70
- e .SetBool (b )
83
+ e .Set (reflect .ValueOf (dur ))
84
+ case []string :
85
+ vals := strings .Split (v , "," )
86
+ e .Set (reflect .ValueOf (vals ))
71
87
default :
72
- switch t := e .Interface ().(type ) {
73
- case net.IP :
74
- ip := net .ParseIP (v )
75
- if ip == nil {
76
- return fmt .Errorf ("Error converting input %s to an IP." , v )
77
- }
78
- e .Set (reflect .ValueOf (ip ))
79
- case net.IPNet :
80
- _ , cidr , err := net .ParseCIDR (v )
81
- if err != nil {
82
- return fmt .Errorf ("Error converting input %s to a CIDR: %s" , v , err )
83
- }
84
- e .Set (reflect .ValueOf (* cidr ))
85
- case utilnet.PortRange :
86
- pr , err := utilnet .ParsePortRange (v )
87
- if err != nil {
88
- return fmt .Errorf ("Error converting input %s to PortRange: %s" , v , err )
89
- }
90
- e .Set (reflect .ValueOf (* pr ))
91
- case []string :
92
- vals := strings .Split (v , "," )
93
- e .Set (reflect .ValueOf (vals ))
88
+ // Last ditch attempt to convert anything based on its underlying kind.
89
+ // This covers any types that are aliased to a native type
90
+ switch e .Kind () {
91
+ case reflect .Int , reflect .Int32 , reflect .Int64 :
92
+ return convertInt (e , v )
93
+ case reflect .String :
94
+ return convertString (e , v )
95
+ case reflect .Float32 , reflect .Float64 :
96
+ return convertFloat (e , v )
97
+ case reflect .Bool :
98
+ return convertBool (e , v )
94
99
default :
95
- return fmt .Errorf ("Unable to set type %T." , t )
100
+ return fmt .Errorf ("Unable to set type %T." , e . Kind () )
96
101
}
97
102
}
103
+
104
+ return nil
105
+ }
106
+
107
+ func convertInt (e reflect.Value , v string ) error {
108
+ i , err := strconv .Atoi (v )
109
+ if err != nil {
110
+ return fmt .Errorf ("Error converting input %s to an integer: %s" , v , err )
111
+ }
112
+ e .SetInt (int64 (i ))
113
+ return nil
114
+ }
115
+
116
+ func convertString (e reflect.Value , v string ) error {
117
+ e .SetString (v )
118
+ return nil
119
+ }
120
+
121
+ func convertFloat (e reflect.Value , v string ) error {
122
+ f , err := strconv .ParseFloat (v , 64 )
123
+ if err != nil {
124
+ return fmt .Errorf ("Error converting input %s to a float: %s" , v , err )
125
+ }
126
+ e .SetFloat (f )
127
+ return nil
128
+ }
129
+
130
+ func convertBool (e reflect.Value , v string ) error {
131
+ b , err := strconv .ParseBool (v )
132
+ if err != nil {
133
+ return fmt .Errorf ("Error converting input %s to a bool: %s" , v , err )
134
+ }
135
+ e .SetBool (b )
98
136
return nil
99
137
}
100
138
0 commit comments