getBool safely extracts a boolean value from a map[string]interface{}. This function handles multiple representations of boolean values that can appear in JSON or form data: - bool: true/false - int: 0 (false) or non-zero (true) - float64: 0.0 (false) or non-zero (true) - string: "true", "1" (true)
(data map[string]interface{}, key string)
| 80 | // |
| 81 | // Returns the boolean value or false if not found or not convertible. |
| 82 | func getBool(data map[string]interface{}, key string) bool { |
| 83 | if val, ok := data[key]; ok { |
| 84 | switch v := val.(type) { |
| 85 | case bool: |
| 86 | return v |
| 87 | case int: |
| 88 | return v != 0 |
| 89 | case float64: |
| 90 | return v != 0 |
| 91 | case string: |
| 92 | return v == "1" || strings.EqualFold(v, "true") |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return false |
| 97 | } |
| 98 | |
| 99 | // getInt safely extracts an integer value from a map[string]interface{}. |
| 100 | // |
no outgoing calls
no test coverage detected