https://github.com/smasher164/sumgen
sumgen generates interface method implementations from sum-type declarations.
https://github.com/smasher164/sumgen
generate golang interface sum variant
Last synced: about 1 year ago
JSON representation
sumgen generates interface method implementations from sum-type declarations.
- Host: GitHub
- URL: https://github.com/smasher164/sumgen
- Owner: smasher164
- License: mit
- Created: 2017-12-07T07:01:12.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-12-24T23:30:04.000Z (over 6 years ago)
- Last Synced: 2025-04-26T15:50:09.980Z (about 1 year ago)
- Topics: generate, golang, interface, sum, variant
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
`sumgen` generates interface method implementations from sum-type declarations.
```
go get -u github.com/smasher164/sumgen
```
Usage: `sumgen` is a tool intended for `go generate`, but can also be run standalone. An example declaration of a sum-type would be
```
package main
//go:generate sumgen "Expr = KeyValueExpr | *CallExpr"
type Expr interface {
expr()
}
type KeyValueExpr struct {
Key Expr
Colon int
Value Expr
}
type CallExpr struct {
Fun Expr
Lparen int
Args []Expr
Ellipsis bool
Rparen int
}
func main() {}
```
Running `go generate` in the package directory will output the following to a file named **DIRECTORY_NAME**_sumgen.go.
```
// Code generated by "sumgen"; DO NOT EDIT.
package main
func (_ KeyValueExpr) expr() { panic("default implementation") }
func (_ *CallExpr) expr() { panic("default implementation") }
```
Each declaration follows the following structure:
```
Def = LhsType "=" RhsType { "|" RhsType } .
LhsType = identifier .
RhsType = [ * ] identifier .
```
More than one declaration can be be made per `interface_name`.
To make the `concrete_typename` a pointer receiver, simply add a `'*'` in front of it.