Go plugin: keep float parameters typed (#33162)

This commit is contained in:
Dennis Klipp 2026-08-25 14:23:53 +02:00 • committed by GitHub
parent 022171ffde
commit 9beca9b22f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 86 additions and 1 deletions

View file

@ -202,11 +202,25 @@ func (p *Go) evaluate(vm *interp.Interpreter) (res any, err error) {
func (p *Go) setParam(vm *interp.Interpreter) func(param string, val any) error {
return func(param string, val any) error {
_, err := vm.Eval(fmt.Sprintf("%s := %#v;", param, val))
_, err := vm.Eval(fmt.Sprintf("%s := %s;", param, goLiteral(val)))
return err
}
}
// goLiteral renders val as Go source. Floats need their type spelled out:
// %#v prints float64(100) as `100`, which the interpreter infers as int. Any
// arithmetic mixing the parameter with a float constant then fails to compile.
func goLiteral(val any) string {
switch v := val.(type) {
case float32:
return fmt.Sprintf("float32(%v)", v)
case float64:
return fmt.Sprintf("float64(%v)", v)
default:
return fmt.Sprintf("%#v", val)
}
}
var _ IntSetter = (*Go)(nil)
// IntSetter sends int request