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

Allow defining template user functions #74

Merged
merged 2 commits into from Oct 13, 2022
Merged

Allow defining template user functions #74

merged 2 commits into from Oct 13, 2022

Conversation

heaths
Copy link
Contributor

@heaths heaths commented Sep 20, 2022

Resolves #73

@samcoe samcoe self-assigned this Sep 20, 2022
@samcoe samcoe self-requested a review September 20, 2022 05:07
}
}

// RegisterFunc registers a named function or overwrites a built-in function.
// Call this before Parse.
func (t *Template) RegisterFunc(name string, f func(fields ...interface{}) (string, error)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Only question I have is should we change this to be Funcs and have the argument be a template.FuncMap to match the text/template package? My vote would be yes to keep consistency with the text/template package since we have been doing that with Parse and Execute. I know I was the one who suggested RegisterFunc in the first place 😞

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've been trying to think of something along these lines as well. Having written an example for testing, the user ends up having to do a lot of what text/template does internally.

Do we support devs calling RegisterFuncs multiple times and just merge the maps? Last wins, which would be consistent with possibly overriding defaults?

Copy link
Contributor

Choose a reason for hiding this comment

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

That is what I was thinking. We support calling Funcs multiple times and just merging the maps where the latest entry for a key wins. That is how Funcs works in text/template.

Copy link
Contributor

Choose a reason for hiding this comment

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

@heaths I went ahead and made the changes I suggested. Let me know what you think.

Copy link
Contributor Author

@heaths heaths left a comment

Choose a reason for hiding this comment

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

Thanks for making the changes. It was on my TODO list but a bit down the priority list.

pkg/template/template.go Show resolved Hide resolved
@samcoe samcoe merged commit 05415b9 into cli:trunk Oct 13, 2022
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.

Template support for additional functions
2 participants