Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Struct's method calling not working #8

Closed
phcostabh opened this issue Jul 16, 2014 · 9 comments
Closed

Struct's method calling not working #8

phcostabh opened this issue Jul 16, 2014 · 9 comments

Comments

@phcostabh
Copy link

When we have to print the result of a strutc's method it doesn't show anything.

Ex.:

type Test struct {
    Child string
}

func (t *Test) MyFunc() string {
    return "My Func Result"
}

func main() {
    tpl, err := pongo2.FromString("Child: {{ Test.Child }} | MyFunc: {{ Test.MyFunc() }}")
    if err != nil {
        panic(err)
    }
    out, err := tpl.Execute(pongo2.Context{"Test":  &Test{ Child: "Test Child"} })
    if err != nil {
        panic(err)
    }
    fmt.Println(out) // Child: TestChild | MyFunc: 
}

What I could figure out is that when parsing Pongo2 knows that a function should be called, but when resolving, the function is not called and only the struct's properties is printed.

@flosch
Copy link
Owner

flosch commented Jul 16, 2014

Have you tried it with the latest master? I tried your example code and pongo2 panics with the following (correct) error:

panic: [Execution Error in <string> | Line 1 Col 38 (<Token Typ=Identifier (4) Val='Test' Line=1 Col=38>)] Function return type of 'Test.MyFunc' must be of type *Value.

All functions called by pongo2 must be of type func(*pongo2.Value, ....) *pongo2.Value. Your function would therefore look like this:

func (t *Test) MyFunc() *pongo2.Value {
    return pongo2.AsValue("My Func Result")
}

The result is the following:

$ go run test-pongo2.go 
Child: Test Child | MyFunc: My Func Result

Hope this helps.

@flosch
Copy link
Owner

flosch commented Jul 16, 2014

Just to clarfiy: You can return whatever type you want through pongo2.AsValue(). pongo2 relies on its own little type system and therefore requires all argument types/return types to be a *pongo2.Value.

The pongo2.Value documentation: http://godoc.org/github.com/flosch/pongo2#Value

A working example from the tests: https://github.com/flosch/pongo2/blob/master/pongo2_template_test.go#L34

@flosch flosch closed this as completed Jul 16, 2014
@flosch flosch added the invalid label Jul 16, 2014
@phcostabh
Copy link
Author

Thak you. It did help.

Now my only issue is to figure out a way to keep my code dry as I need to use MyFunc an other places other then in the template, where I can't rely on pongo2.Value.

For now I'm gonna wrap MyFunc in the PrintMyFunc function so that I can retrun pongo2.Value form it.

@flosch
Copy link
Owner

flosch commented Jul 16, 2014

I'll keep that in mind and think about that. Maybe I can make pongo2 more flexible in calling such functions.

Update: I created issue #9 for that.

@flosch
Copy link
Owner

flosch commented Jul 16, 2014

BTW, you can use pongo2.Values outside pongo2 if that makes your life simpler. For example,

func (t *Test) MyFunc() *pongo2.Value {
    return pongo2.AsValue("My Func Result")
}

you can simply call yourTestInstance.MyFunc().String() to get your string back (Bool(), Integer(), etc. does work too). You can even use MyFunc().Interface() to convert your value back to the original type (for example testInstance.MyFunc().Instance().(*myStruct)). Have a look on the documentation on which methods are supported on pongo2.Values.

But I understand that will only be a workaround for you.

flosch added a commit that referenced this issue Jul 16, 2014
@flosch
Copy link
Owner

flosch commented Jul 16, 2014

I pushed two commits to allow arbitrary function argument types and return types. Does this work for you?

@phcostabh
Copy link
Author

I'm gonna test it right now.

@phcostabh
Copy link
Author

Worked like a charm! Thak you a lot!

@flosch
Copy link
Owner

flosch commented Jul 17, 2014

You're welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants