Skip to content

Commit 93d841c

Browse files
authored
Merge pull request kubernetes#197 from krzysied/cluster_loader_params_parsing_fix
ClusterLoader - Int and float parsing fix
2 parents 340dc4b + d792802 commit 93d841c

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

clusterloader2/pkg/util/util.go

+23-6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"encoding/json"
2222
"fmt"
23+
"strconv"
2324
"time"
2425
)
2526

@@ -82,12 +83,22 @@ func getInt(dict map[string]interface{}, key string, defaultValue int, defaultEr
8283
return defaultValue, defaultError
8384
}
8485

86+
intValue, ok := value.(int)
87+
if ok {
88+
return intValue, nil
89+
}
8590
// Types from interface{} create from json cannot be cast directly to int.
8691
floatValue, ok := value.(float64)
87-
if !ok {
88-
return 0, fmt.Errorf("type assertion error: %v of is not an int", value)
92+
if ok {
93+
return int(floatValue), nil
94+
}
95+
stringValue, ok := value.(string)
96+
if ok {
97+
if i, err := strconv.Atoi(stringValue); err != nil {
98+
return i, nil
99+
}
89100
}
90-
return int(floatValue), nil
101+
return 0, fmt.Errorf("type assertion error: %v of is not an int", value)
91102
}
92103

93104
func getFloat64(dict map[string]interface{}, key string, defaultValue float64, defaultError error) (float64, error) {
@@ -97,10 +108,16 @@ func getFloat64(dict map[string]interface{}, key string, defaultValue float64, d
97108
}
98109

99110
floatValue, ok := value.(float64)
100-
if !ok {
101-
return 0, fmt.Errorf("type assertion error: %v of is not an int", value)
111+
if ok {
112+
return floatValue, nil
113+
}
114+
stringValue, ok := value.(string)
115+
if ok {
116+
if f, err := strconv.ParseFloat(stringValue, 64); err != nil {
117+
return f, nil
118+
}
102119
}
103-
return floatValue, nil
120+
return 0, fmt.Errorf("type assertion error: %v of is not a float", value)
104121
}
105122

106123
func getDuration(dict map[string]interface{}, key string, defaultValue time.Duration, defaultError error) (time.Duration, error) {

0 commit comments

Comments
 (0)