https://github.com/nchern/go-codegen
Yet another code generator for golang; generics included
https://github.com/nchern/go-codegen
automation code-generation code-generator codegen generic golang gotools
Last synced: 12 months ago
JSON representation
Yet another code generator for golang; generics included
- Host: GitHub
- URL: https://github.com/nchern/go-codegen
- Owner: nchern
- License: mit
- Created: 2017-04-11T23:49:35.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-07-30T08:01:48.000Z (almost 3 years ago)
- Last Synced: 2024-06-20T19:24:13.934Z (about 2 years ago)
- Topics: automation, code-generation, code-generator, codegen, generic, golang, gotools
- Language: Go
- Homepage:
- Size: 351 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://goreportcard.com/report/github.com/nchern/go-codegen)
[](https://gocover.io/github.com/nchern/go-codegen)
# go-codegen
With a lack of templates some of the gophers really miss them...
Yet another attempt of code generaton in Golang.
## Testing
```bash
make test # generates code and runs tests
```
## Install
```bash
make install
```
## Usage
```bash
# Prints help
$ go-codegen -h
```
### Generics
#### Usage
```bash
# Outputs built-in string to int map implementation to stdout
go-codegen generic -t=hashmap string int
# Reads generic source from file pkg/generic/list/list.go and outputs typed implementation to stdout
go-codegen generic -f pkg/generic/list/list.go string
# More help on available built-ins and other command line params
go-codegen help generic
```
Generics are implemented by parsing an input go source file into an AST and substituting predefined "generic" types. Currently only a fixed list of such types is supported: `T0, T1, ..., T5`. The advantage of such approach as the generic implementation(input source) is the correct go source that can be tested.
See build-ins as [examples](pkg/generic/list/list.go) of how to define a generic type.
See [test code](tests/generic) for more examples.
### Immutables
#### Usage
```bash
# Gets a .go source file with defined interfaces and outputs implementation along with builder class to create instances
go-codegen --pkg=model immutable -f tests/immutable/model/model.go
```
An experiment. Inspired by Java Immutables. See more examples in [test code](tests/immutable/).
### Constructor
Generates constructor function for struct. More details see [here](docs/constructor.md).
### Impl
Generates interface implementations stubs. More details see [here](docs/impl.md).
## Nice to have
* Integration with [gen](http://alikewise.com/gen/)