Skip to content

Buildergen is a code generation tool to create a builder class for generating structs

License

Notifications You must be signed in to change notification settings

Jh123x/buildergen

Repository files navigation

BuilderGen

Go Reference CI Badge

BuilderGen is a tool used for generating builders for Golang structs.

Features

  • Generate builder files from structs
  • Config paths to generate all structs
  • Generate builders with local imports in a different package
  • Custom code generation template

QuickStart

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

Step 1: Install this package

go install github.com/Jh123x/buildergen@latest

Install this package start using it

Step 2: Use the package

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
}

Step 3: Using the builder

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
}

About

Buildergen is a code generation tool to create a builder class for generating structs

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published