Skip to content

Add support for template plugins #2745

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

Merged
merged 2 commits into from
Jun 14, 2022

Conversation

kevinbarbour
Copy link
Contributor

This PR adds the ability to use a go plugin to add custom functions to the templating library when using custom templates.

In #2741 I added the sprig library to provide some templating functionality we needed for a project, but it may be valuable to have the ability for users to inject their own template functions without modifications to the go-swagger codebase.

Haven't checked that this compiles on Windows yet, and I'd like to write some tests around the plugin functionality itself but wanted to gauge interest and see if something like this could be a valuable contribution to the project.

Note that plugins are not supported on Windows hence the splitting of the code into files with windows and !windows build tags.

@casualjim
Copy link
Member

I think this is an interesting idea, I always wanted to do plugins for adding extra string formats.

What kind of uses would this unlock beyond what you can achieve with the template config file? I'm not against it, just curious what you have in mind

@kevinbarbour
Copy link
Contributor Author

As far as I'm aware the template config file doesn't let you alter the way in which templates are processed, it just lets you configure which templates are used, the output filename, etc. But if I am missing something let me know.

With this plugin setup you are able to add additional functions to the funcmap for template processing. For a silly example, let's say you needed to use a rot13 function in your templates.

You can write the following plugin:

package main

import "text/template"

func rot13(s string) string {
	// implement me
	return s
}

func AddFuncs(f template.FuncMap) {
	f["rot13"] = rot13
}

Build it as a plugin, which will create plugin.so:
go build -buildmode=plugin plugin.go

Use your plugin function within one of your custom templates:
{{ rot13 .Item }}

And then run swagger generate using the plugin:
swagger generate cli -t gen --spec=spec.json -T templates -p plugin.so

I do not actually have a specific use-case that needs this at the moment, but the idea is that in the future if you need some additional templating functionality you don't need to add it to the go-swagger codebase like I did with #2741, you can just pull in your own plugins and modify them as needed.

@kevinbarbour kevinbarbour force-pushed the template-plugins branch 2 times, most recently from 27bab8f to 20f498e Compare March 24, 2022 14:43
@kevinbarbour kevinbarbour force-pushed the template-plugins branch 4 times, most recently from c7c369f to 520d167 Compare April 13, 2022 12:32
@kevinbarbour
Copy link
Contributor Author

Fixed up an issue with tests and got the windows build fixed. The failing AppVeyor tests are due to an unrelated issue (#2762).

@casualjim Curious if you have any further feedback on this, and my explained use case above.

This allows the use of go plugins to supply your own custom template
functions for generating code from custom templates with go-swagger.

Builds plugin functionality in only for non-windows builds.

Signed-off-by: Kevin Barbour <kevinbarbourd@gmail.com>
@casualjim
Copy link
Member

This looks good to me

This model could also be used for strfmt extensions which is an often requested feature

@kevinbarbour kevinbarbour changed the title POC: Add support for template plugins POC: Jun 14, 2022
@kevinbarbour kevinbarbour changed the title POC: Add support for template plugins Jun 14, 2022
@kevinbarbour kevinbarbour marked this pull request as ready for review June 14, 2022 06:45
@kevinbarbour
Copy link
Contributor Author

This looks good to me

This model could also be used for strfmt extensions which is an often requested feature

Thanks Ivan! Should be ready to merge now.

I will look into the strfmt use case as well.

@casualjim casualjim merged commit d1774f8 into go-swagger:master Jun 14, 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.

2 participants