PaesslerAG/gval

Struct methods do not accept func arguments

larrycinnabar opened this issue · 1 comments

type Client struct{}
func (c *Client) Foo(s string) string {
	return s
}

gval.Evaluate(`client.Foo("bar")`, map[string]interface{}{"client": c}) // works: does return "bar"

But

type Client struct{}
func (c *Client) Foo(s string, f func()) string {
        f()
	return s
}

gval.Evaluate(`client.Foo("bar", func(){})`, map[string]interface{}{"client": c}) // does not work

returns&errors.errorString{s:"parsing error: client.Foo(\"bar\", func(){})\t:1:25 - 1:26 unexpected \"{\" while scanning arguments expected \")\" or \",\""} a <nil>

Yes gval cannot interprete go closures. That would require an entire golang interpreter.

But you can use closure parameter:

type Client struct{}
func (c *Client) Foo(s string, f func()) string {
        f()
	return s
}

gval.Evaluate(`client.Foo("bar", closure)`, map[string]interface{}{"client": c, "closure" : func(){}})

or closure return types:

type Client struct{}
func (c *Client) Foo(s string, f func()) string {
        f()
	return s
}

func (c *Client) MakeClosure() func(){
	return func(){}
}

gval.Evaluate(`client.Foo("bar", client.MakeClosure())`, map[string]interface{}{"client": c} )