getFloat safely extracts a numeric value from a map[string]interface{} as float64. This function handles multiple numeric types that can appear in JSON-decoded data, including float64, int, int64, and numeric strings. It performs safe type assertions and conversions. Parameters: - data: The map to
(data map[string]interface{}, key string)
| 43 | // |
| 44 | // Returns the numeric value as float64, or 0 if not found or not convertible. |
| 45 | func getFloat(data map[string]interface{}, key string) float64 { |
| 46 | if val, ok := data[key]; ok { |
| 47 | switch v := val.(type) { |
| 48 | case float64: |
| 49 | if isFiniteFloat(v) { |
| 50 | return v |
| 51 | } |
| 52 | case int: |
| 53 | return float64(v) |
| 54 | case int64: |
| 55 | return float64(v) |
| 56 | case string: |
| 57 | // Try to convert string to float |
| 58 | var f float64 |
| 59 | if n, err := fmt.Sscanf(v, "%f", &f); err == nil && n > 0 && isFiniteFloat(f) { |
| 60 | return f |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return 0 |
| 66 | } |
| 67 | |
| 68 | // getBool safely extracts a boolean value from a map[string]interface{}. |
| 69 | // |