forgejo-mcp/pkg/to/number.go
Dmitry Mikushin 9f87831c14 Fix number parameter parsing: handle string-encoded numbers from MCP clients
MCP clients (e.g. Claude Code) may serialize number parameters as JSON
strings instead of JSON numbers. The Go type assertion .(float64) silently
returns 0 for string values, causing "issue does not exist [index: 0]"
errors.

Add to.Float64() helper that handles float64, int, int64, and string types
via strconv.ParseFloat. Replace all 62 bare .(float64) assertions across
9 operation files with to.Float64() calls.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-17 15:08:59 +01:00

30 lines
722 B
Go

package to
import (
"fmt"
"strconv"
)
// Float64 extracts a float64 from an interface{} value.
// Handles both JSON number (float64) and string-encoded numbers,
// which can occur when MCP clients serialize number parameters as strings.
func Float64(v interface{}) (float64, error) {
switch val := v.(type) {
case float64:
return val, nil
case int:
return float64(val), nil
case int64:
return float64(val), nil
case string:
f, err := strconv.ParseFloat(val, 64)
if err != nil {
return 0, fmt.Errorf("cannot parse %q as number: %w", val, err)
}
return f, nil
case nil:
return 0, fmt.Errorf("parameter is nil")
default:
return 0, fmt.Errorf("unexpected type %T for number parameter", v)
}
}