https://github.com/tada-team/bang
go:generate from .yml
https://github.com/tada-team/bang
codegeneration codegenerator go go-generate golang yml
Last synced: about 1 month ago
JSON representation
go:generate from .yml
- Host: GitHub
- URL: https://github.com/tada-team/bang
- Owner: tada-team
- Created: 2021-08-05T01:32:27.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-08-13T21:23:30.000Z (almost 5 years ago)
- Last Synced: 2025-03-16T08:28:39.303Z (about 1 year ago)
- Topics: codegeneration, codegenerator, go, go-generate, golang, yml
- Language: Go
- Homepage:
- Size: 12.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
Awesome Lists containing this project
README
## Bang
Sugar for `go generate`.
Reads yml data from .go source file and render template with variables.
### How to use it
1. Install:
```bash
go install github.com/tada-team/bang
```
2. Write line `//go:generate bang $GOFILE:$GOLINE`, and `//`-commented yaml content below:
```go
package mypackage
//go:generate bang $GOFILE:$GOLINE
//
// ...yaml...
//
```
Yaml format is:
```yaml
template:
vars:
dest:
```
3. run `go generate`
Template will be rendered and code wil be formatted.
### Example
```go
package main
//go:generate bang $GOFILE:$GOLINE
// dest: main_generated.go
// vars:
// package: main
// types:
// - int
// - float64
// template: >
// package {{ .package }}
//
// {{ range $type := .types }}
// func {{ $type }}Sum(a, b {{ $type }}) {{ $type }} {
// return a + b
// }
// {{ end }}
//
```
Result (`main_generated.go`):
```go
// Code generated by Bang.go DO NOT EDIT.
package main
func intSum(a, b int) int {
return a + b
}
func float64Sum(a, b float64) float64 {
return a + b
}
```
### Optional command line flags
* `-dest` argument overrides `dest` key in yaml
* `-template` argument takes `template` from given template file
* `-vars` argument takes `vars` from given yaml file
* `-verbose` argument adds more output
Example:
```go
package main
//go:generate bang -verbose -dest=main_generated.go -template=main.tpl $GOFILE:$GOLINE
```
`vars.tpl`:
```yaml
package: main
types:
- int
- float64
```
`main.tpl`:
```gotemplate
package {{ .package }}
{{ range $type := .types }}
func {{ $type }}Sum(a, b {{ $type }}) {{ $type }} {
return a + b
}
{{ end }}
```