https://github.com/bannzai/constructor
constructor is generated Constructor for struct.
https://github.com/bannzai/constructor
Last synced: about 1 year ago
JSON representation
constructor is generated Constructor for struct.
- Host: GitHub
- URL: https://github.com/bannzai/constructor
- Owner: bannzai
- License: mit
- Created: 2019-05-07T16:52:40.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2022-12-21T12:01:43.000Z (over 3 years ago)
- Last Synced: 2025-04-06T05:00:01.104Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 170 KB
- Stars: 18
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# constructor
`constructor` is generated [Constructor](https://golang.org/doc/effective_go.html#composite_literals) for struct.
Example result.
```go
type Foo struct {
Bar string
}
func NewFoo(bar string) Foo {
return Foo{
Bar: bar,
}
}
```
## Install
Getting `constructor` via `go get`.
```shell
$ go get -u github.com/bannzai/constructor
```
## Usage
First, You can prepare configuration files when exec `constructor setup`. After created `constructor.tpl`.
`constructor` necessary these configuration files.
```shell
$ 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.
```shell
$ constructor generate --source=structure/code.go --destination=structure/code.constructor.go --package=structure
```
$ cat structure/code.constructor.go
```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,
}
}
```
## Help
#### constructor --help
```shell
$ 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
```shell
$ 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.
```
## go:generate
**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
```
## Customize template
It is possible to use customize template.
Two ways for preparing template files.
1. Edit `constructor.tpl`
2. Create new `[YOUR_CUSTOMIZE_TEMPLATE].tpl`
And, It can be passed to `constructor generate [REQUIRED_FLAGS] --template=[YOUR_CUSTOMIZE_TEMPLATE].tpl`.
## LICENSE
**constructor** is available under the MIT license. See the LICENSE file for more info.