Skip to content

Commit a8d51b2

Browse files
committed
updated GetBoolField to use if instead of single case switch.
also updated GetIntField to follow GetStringField structure.
1 parent 2aef07c commit a8d51b2

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

pkg/kstatus/status/util.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,18 @@ func GetStringField(obj map[string]interface{}, fieldPath string, defaultValue s
119119
return rv
120120
}
121121

122-
// GetIntField return field as string defaulting to value if not found
122+
// GetIntField return field as int defaulting to value if not found
123123
func GetIntField(obj map[string]interface{}, fieldPath string, defaultValue int) int {
124+
var rv = defaultValue
125+
124126
fields := strings.Split(fieldPath, ".")
125127
if fields[0] == "" {
126128
fields = fields[1:]
127129
}
128130

129131
val, found, err := apiunstructured.NestedFieldNoCopy(obj, fields...)
130132
if !found || err != nil {
131-
return defaultValue
133+
return rv
132134
}
133135

134136
switch v := val.(type) {
@@ -139,23 +141,25 @@ func GetIntField(obj map[string]interface{}, fieldPath string, defaultValue int)
139141
case int64:
140142
return int(v)
141143
}
142-
return defaultValue
144+
return rv
143145
}
144146

147+
// GetBoolField return field as boolean defaulting to value if not found
145148
func GetBoolField(obj map[string]interface{}, fieldPath string, defaultValue bool) bool {
149+
var rv = defaultValue
150+
146151
fields := strings.Split(fieldPath, ".")
147152
if fields[0] == "" {
148153
fields = fields[1:]
149154
}
150155

151156
val, found, err := apiunstructured.NestedFieldNoCopy(obj, fields...)
152157
if !found || err != nil {
153-
return defaultValue
158+
return rv
154159
}
155160

156-
switch v := val.(type) {
157-
case bool:
161+
if v, ok := val.(bool); ok {
158162
return v
159163
}
160-
return defaultValue
164+
return rv
161165
}

0 commit comments

Comments
 (0)