toInt converts a value to int, handling both float64 and string representations. Some MCP clients send numeric values as strings. It rejects NaN, ±Inf, fractional values, and values outside the int range.
(val any)
| 44 | // Some MCP clients send numeric values as strings. It rejects NaN, ±Inf, |
| 45 | // fractional values, and values outside the int range. |
| 46 | func toInt(val any) (int, error) { |
| 47 | var f float64 |
| 48 | switch v := val.(type) { |
| 49 | case float64: |
| 50 | f = v |
| 51 | case string: |
| 52 | var err error |
| 53 | f, err = strconv.ParseFloat(v, 64) |
| 54 | if err != nil { |
| 55 | return 0, fmt.Errorf("invalid numeric value: %s", v) |
| 56 | } |
| 57 | default: |
| 58 | return 0, fmt.Errorf("expected number, got %T", val) |
| 59 | } |
| 60 | if math.IsNaN(f) || math.IsInf(f, 0) { |
| 61 | return 0, fmt.Errorf("non-finite numeric value") |
| 62 | } |
| 63 | if f != math.Trunc(f) { |
| 64 | return 0, fmt.Errorf("non-integer numeric value: %v", f) |
| 65 | } |
| 66 | if f > math.MaxInt || f < math.MinInt { |
| 67 | return 0, fmt.Errorf("numeric value out of int range: %v", f) |
| 68 | } |
| 69 | return int(f), nil |
| 70 | } |
| 71 | |
| 72 | // toInt64 converts a value to int64, handling both float64 and string representations. |
| 73 | // Some MCP clients send numeric values as strings. It rejects NaN, ±Inf, |
no outgoing calls
no test coverage detected