getInt safely extracts an integer value from a map[string]interface{}. This function handles multiple numeric types that can appear in JSON-decoded data, including int, float64, and numeric strings. It performs safe type assertions and conversions, truncating floating-point values to integers. Par
(data map[string]interface{}, key string)
| 108 | // |
| 109 | // Returns the integer value or 0 if not found or not convertible. |
| 110 | func getInt(data map[string]interface{}, key string) int { |
| 111 | if val, ok := data[key]; ok { |
| 112 | switch v := val.(type) { |
| 113 | case int: |
| 114 | return v |
| 115 | case float64: |
| 116 | return int(v) |
| 117 | case string: |
| 118 | var i int |
| 119 | if _, err := fmt.Sscanf(v, "%d", &i); err == nil { |
| 120 | return i |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return 0 |
| 126 | } |
| 127 | |
| 128 | // FormatBytes converts a byte count into a human-readable string with appropriate units. |
| 129 | // |
no outgoing calls
no test coverage detected