constructor
is generated Constructor for struct.
Example result.
type Foo struct {
Bar string
}
func NewFoo(bar string) Foo {
return Foo{
Bar: bar,
}
}
Getting constructor
via go get
.
$ go get -u github.com/bannzai/constructor
First, You can prepare configuration files when exec constructor setup
. After created constructor.tpl
.
constructor
necessary these configuration files.
$ constructor setup
Next, Execute constructor generate
, after you edited configuration files.
And you confirm diff between before and after executing it. You found about constructor functions .go
file.
$ constructor generate --source=structure/code.go --destination=structure/code.constructor.go --package=structure
$ cat structure/code.constructor.go
// DO NOT EDIT THIS FILE.
// File generated by constructor.
// https://github.com/bannzai/constructor
package structure
// NewCodeStructure insitanciate Code
func NewCodeStructure(
filePath Path,
structs []Struct,
) Code {
return Code{
FilePath: filePath,
Structs: structs,
}
}
// NewFieldStructure insitanciate Field
func NewFieldStructure(
name string,
_type string,
) Field {
return Field{
Name: name,
Type: _type,
}
}
// NewStructStructure insitanciate Struct
func NewStructStructure(
fields []Field,
name string,
) Struct {
return Struct{
Fields: fields,
Name: name,
}
}
$ constructor --help
This application is a tool to generate constructor functions for each struct quickly.
When you execute "constructor generate [flags]",
It is generating constructor functions under the package.
You get "./constructor.tpl" via to execute "constructor setup".
This is default template for [constructor].
You can edit this template, If you customize generated files and pass it.
Usage:
construtor [flags]
construtor [command]
Available Commands:
generate generate constructor functions
help Help about any command
setup setup will create ./constructor.yaml
Flags:
-h, --help help for construtor
Use "construtor [command] --help" for more information about a command.
$ constructor generate --help
constructor can be add constructor functions for each go struct.
Usage:
construtor generate [flags]
Flags:
--destination string Destination go file path
-h, --help help for generate
--ignoreFields string Not contains generated fields. It is list with commas. (e.g id,name,age
--package string Package name for generated constructor.
--source string Source go file path
--template string Constructor functions format template file path. Default is ./constructor.tpl (default "constructor.tpl")
--type string Specify struct about generated constructor function.
constructor recommended to use go:generate
.
For example.
//go:generate constructor generate --source=$GOFILE --destination=$GOPATH/src/github.com/bannzai/constructor/generator/constructor.constructor.go --package=$GOPACKAGE --template=$GOPATH/src/github.com/bannzai/constructor/constructor.tpl --type=Constructor --ignoreFields=TemplateReader,SourceCodeReader
It is possible to use customize template.
Two ways for preparing template files.
- Edit
constructor.tpl
- Create new
[YOUR_CUSTOMIZE_TEMPLATE].tpl
And, It can be passed to constructor generate [REQUIRED_FLAGS] --template=[YOUR_CUSTOMIZE_TEMPLATE].tpl
.
constructor is available under the MIT license. See the LICENSE file for more info.