go-struct-builder

module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2024 License: Apache-2.0

README

go-struct-builder

go-struct-builder is a tool that implements builder pattern for structs by using codegen

Build Status

Installation

Go 1.18+
go install github.com/slavaavr/go-struct-builder/cmd/gosb@v1.0.0

Quick start

  • To generate the struct builder add the appropriate go:generate comment:
// input.go - the file where struct is located

//go:generate gosb -source=input.go
type A struct {
	F1 int
	F2 string
}

Then run the command:

go generate ./...
  • There are two types of struct fields: required and optional. By default, every pointer** value is optional and the rest of them are required. To change the default behaviour add the appropriate struct tags:
//go:generate gosb -source=input.go
type B struct {
	F1 *int `gosb:"required"`
	F2 string `gosb:"optional"`
}

Generated builder will check if required fields were provided.

  • For a private struct a private builder will be generated.
  • If struct has private fields, along with the builder getter methods will be generated.
  • For more examples see the testdata folder.

** and the Option type fromgithub.com/samber/mo package.

Flags

The gosb command is used to generate builder pattern for structs annotated with go:generate gosb comment. It supports the following flags:

  • -source: A file containing struct the builder must be generated for
  • -features: Comma separated list of features:
    • ptr: Generates additional method for every pointer field without the pointer in the argument
    • arr: Generates additional method for every array field by using vararg in the argument
    • opt: Generates additional method for every Option field provided by the github.com/samber/mo library by unwrapping the Option type and setting a value directly

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL