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

Custom resolvers #186

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

tonyhb
Copy link

@tonyhb tonyhb commented Dec 22, 2020

Opening a PR with a prototype as a means to discuss custom resolvers. Based off of this comment in #110, custom resolvers allow us to override default functionality when executing a template. This would open the possibility to:

  • Implement logging and tracing
  • Implement custom error handling logic
  • Implement introspection without navigating the AST

This PR is purely a concept. It has no tests, and it exposes *Runtime directly from a template, which I do not like. So, word of warning, this PR is not ready and the aim here is to open a discussion with maintainers and owners regarding whether Resolver interfaces can be considered.

Stealing from my previous comment:

It adds a single interface - Resolver:

type Resolver interface {
	Resolve(name string) (reflect.Value, error)
}

This allows us to define custom functions overriding default functionality in Resolve. You can always get the default resolver functionality within Runtime by calling runtime.DefaultResolver().

Usage:

type CustomResolver struct {
  default jet.Resolver
}

func (cr *CustomResolver) Resolve(name string) (reflect.Value, error) {
        fmt.Printf("resolving %s\n", name)
        // Here, we can ignore the error from our default resolver, choose to do no resolving at all, etc.
        return cr.default(name)
}

var buf bytes.Buffer
tpl, err := jet.NewSet(jet.NewInMemLoader()).Parse("some-tpl.tpl". source)
runtime := tpl.Runtime()
runtime.WithResolver(&customResolver{})
tpl.ExecuteWith(runtime, &buf, nil, nil)

I don't like how I have to expose the Runtime() func within templates to get the default resolver at all to be honest, but yeah, like I said - it's a hacky POC to show that interfaces here might help.

@tonyhb
Copy link
Author

tonyhb commented Dec 22, 2020

Looks like there are some issues with templates halting execution when eg. iterating over nil ranges. So, no matter what, it doesn't seem like adding a Resolver interface magically lets us customize execution. Kinda writing off this idea but will keep this open for discussion.

edit:

It's possible to get around this by creating a type which implements Ranger.

@@ -40,11 +40,24 @@ func (scope VarMap) SetWriter(name string, v SafeWriter) VarMap {
return scope
}

func (t *Template) Runtime() *Runtime {
st := pool_State.Get().(*Runtime)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will give you any runtime from the pool, possibly a new one, not the one executing the template.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, you should create a runtime, then create a custom resolver to which you pass the runtime, then set the resolver in the runtime, then execute the template.

@sauerbraten
Copy link
Collaborator

I like the overall idea, however I believe it would be good to give users a way to hook and redirect variable declarations as well in order to make a custom resolver really useful. I'm picturing something like a user defined and controlled reflect.Value store.

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

Successfully merging this pull request may close these issues.

2 participants