An open API service indexing awesome lists of open source software.

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

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 }}
```