BuilderGen is a tool used for generating builders for Golang structs
.
structs
structs
Note: There is also a way to use this package using a yaml
file.
For more information please take a look at the Usage Docs
go install github.com/Jh123x/buildergen@latest
Install this package start using it
Write the go generate comment as shown in the example below.
package examples
import "github.com/Jh123x/buildergen/examples/nested"
//go:generate buildergen -src=./test.go -name Person
type Person struct {
ID int
Name string
Email *string // Optional field
PhoneBook []*Contact
MapVal map[string]string `json:"map_val"`
T nested.Test
}
type Contact struct {
Name string
Phone string
}
After running the go generate, you can use the builder similar to what is shown below.
var defaultPerson = &Person{
ID: 1,
Name: "John",
Email: nil,
}
...
func TestXXX(t *testing.T){
clonedPerson := NewPersonBuilder(defaultPerson).WithID(12).WithName("Johnny").Build() // ID and Name changes
...
// Use clonedPerson
}