GoWrap is a command line tool for generating decorators for Go interfaces
GoWrap is a command line tool for generating decorators for Go interfaces
GoWrap is a command line tool that generates decorators for Go interface types using simple templates. With GoWrap you can easily add metrics, tracing, fallbacks, pools, and many other features into your existing code in a few seconds.
go install github.com/hexdigest/gowrap/cmd/gowrap@latestgo get -u github.com/hexdigest/gowrap/cmd/gowrap…This will generate an implementation of the io.Reader interface wrapped with prometheus metrics
$ gowrap gen -p io -i Reader -t prometheus -o reader_with_metrics.goThis will generate a fallback decorator for the Connector interface that can be found in the ./connector subpackage:
$ gowrap gen -p ./connector -i Connector -t fallback -o ./connector/with_metrics.goRun gowrap help for more options
When you specify a template with the "-t" flag, gowrap will first search for and use the local file with this name. If the file is not found, gowrap will look for the template here and use it if found.
List of available templates:
func Validate() error method on each argument if it's presentfunc Validate() error method on each argument if it's present and wraps returned error with twirp.Malformed errorfunc Validate() error method on each argument if it's present and returns InvalidArgument error in case when validation failedBy default GoWrap places the //go:generate instruction into the generated code.
This allows you to regenerate decorators' code just by typing go generate ./... when you change the source interface type declaration.
However if you used a remote template, the //go:generate instruction will contain the HTTPS URL of the template and therefore
you will need to have internet connection in order to regenerate decorators. In order to avoid this, you can copy templates from the GoWrap repository
to local files and add them to your version control system:
$ gowrap template copy fallback templates/fallbackThe above command will fetch the fallback template and copy it to the templates/fallback local file. After template is copied, you can generate decorators using this local template:
$ gowrap gen -p io -i Reader -t templates/fallback reader_with_fallback.goYou can always write your own template that will provide the desired functionality to your interfaces. If you think that your template might be useful to others, please consider adding it to our template repository.
The structure of information passed to templates is documented with the TemplateInputs struct.
In the templates, all functions provided by the sprig template library are available.
Additionally gowrap includes the following template functions:
up: returns the input with all Unicode letters mapped to their upper case.down: returns the input with all Unicode letters mapped to their lower case.upFirst: returns the input with the first Unicode letter mapped to their upper case.downFirst: returns the input with the first Unicode letter mapped to their lower case.replace: returns the input with all occurences of the first argument replaced with the second argument.snake: returns the input in snake case representation.Here's my Patreon page. Thank you!
No open issues yet, or sync has not completed.