At this moment, this project is in development. It is not ready for production use. See the Contributing section if you would like to help make it better.
✨ Never write toil/boilerplate code again! ✨
Thanks to GenZ, you can generate code for your own needs, without having to write a single line of code.
GenZ is a template-based generator for Go. A single binary that can be called with the native go generate
to automate generation of your Go code based on templates.
You can install GenZ in different ways, depending on your needs. We strongly recommend using the Go version for production use.
go install github.com/Joffref/genz
git clone https://github.com/Joffref/genz.git
cd genz
make install
Let's say you have several types defined in your project (such as Human
as defined below).
package main
type Human struct {
//+required
Firstname string
//+required
Lastname string
Age uint
}
And you want to add a Validate()
method on these type.
With GenZ, all you have to do is to write a template once:
package main
import (
"fmt"
)
func (v {{ .Type.InternalName }}) Validate() error {
{{ range .Attributes }} {{ $attribute := . }}
{{ if eq .Type.InternalName "string" }}{{ range .Comments }}
{{ if eq . "+required" }}
if v.{{ $attribute.Name }} == "" {
return fmt.Errorf("attribute '{{ $attribute.Name }}' must be set")
}
{{ end }}{{ end }}{{ end }}{{ end }}
return nil
}
And generate the associated Go code tied to your type :
- Add
//go:generate genz -type Human -template validator.tmpl -output human.gen.go
on your go code. - Call
go generate ./...
And your validator is generated ✨
package main
import (
"fmt"
)
func (v Human) Validate() error {
if v.Firstname == "" {
return fmt.Errorf("attribute 'Firstname' must be set")
}
if v.Lastname == "" {
return fmt.Errorf("attribute 'Lastname' must be set")
}
return nil
}
Checkout other examples in /examples folder.
Explore built-in examples
, clone repo, and run go generate ./...
in the root
Usage of genz:
genz [flags] -type T -template foo.tmpl [directory]
genz [flags] -type T -template foo.tmpl files... # Must be a single package
Flags:
-output string
output file name; default srcdir/<type>.gen.go
-tags string
comma-separated list of build tags to apply
-template string
go-template local or remote file
-type string
comma-separated list of type names; must be set
If you would like to contribute to this project, please read the CONTRIBUTING.md file.
This project is licensed under the Apache 2.0 - see the LICENSE file for details.
This project is governed by the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code.