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

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.

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.