-
Notifications
You must be signed in to change notification settings - Fork 110
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
base: master
Are you sure you want to change the base?
Custom resolvers #186
Conversation
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 |
@@ -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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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 |
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:
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
: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:
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.