Skip to content

Commit 69f5dbd

Browse files
committed
Refactor getIntValFromExtraVars for type handling
Simplifies and improves type conversion logic in getIntValFromExtraVars by handling json.Number, float64, and string representations more robustly. This change ensures better compatibility with varied input types and reduces code complexity.
1 parent d8f5b7b commit 69f5dbd

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

console/service/internal/controllers/cluster/post_cluster.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -368,24 +368,23 @@ func getIntValFromExtraVars(m map[string]interface{}, key string) int {
368368
if !ok || v == nil {
369369
return 0
370370
}
371-
switch t := v.(type) {
372-
case float64:
373-
return int(t)
374-
case int:
375-
return t
376-
case int32:
377-
return int(t)
378-
case int64:
379-
return int(t)
380-
case string:
381-
n, err := strconv.Atoi(t)
382-
if err != nil {
383-
return 0
384-
}
371+
372+
if num, ok := v.(json.Number); ok {
373+
n, _ := strconv.Atoi(num.String())
385374
return n
386-
default:
387-
return 0
388375
}
376+
377+
if f, ok := v.(float64); ok {
378+
return int(f)
379+
}
380+
381+
s := fmt.Sprintf("%v", v)
382+
383+
if fl, err := strconv.ParseFloat(s, 64); err == nil {
384+
return int(fl)
385+
}
386+
387+
return 0
389388
}
390389

391390
type InventoryJson struct {

0 commit comments

Comments
 (0)