Type-driven code generation for Go
This release includes a new -f
“force” flag. It allows gen to tolerate certain classes of type-check errors. Learn more…
Added stringer as a built-in typewriter.
Fix watch error behavior
Added the watch
command, which automatically runs gen on file changes.
From the changelog:
Tags now have support for type parameters, for example:
// +gen foo:"Bar[qux], Qaz[thing, stuff]"
type MyType struct{}
Those type parameters (qux, thing, stuff) are properly evaluated as types, and passed to your typewriters.
Speaking of above, types are evaluated for Numeric, Ordered and Comparable. Templates, in turn, can have type constraints.
For example, you can declare your Sum template only to be applicable to Numeric types, and your Set to Comparable types.
Third-party typewriters are added to your package using a new command, add
. It looks like this:
gen add github.com/clipperhouse/set
That’s a plain old Go import path.
After adding, you can mark up a type like:
// +gen set slice:"GroupBy[string], Select[Foo]"
type MyType struct{}
As always, it’s up to the third-party typewriter to determine behavior. In this case, a “naked” set
tag is enough.
We deprecated the unintuitive gen custom
command, add
replaces it.
Previous versions of gen would generate a dozen or so LINQ-style slice methods simply by marking up:
// +gen
type MyType struct{}
We’ve opted for explicitness moving forward – in the case of slices, you’ll write this instead:
// +gen slice:"Where, SortBy, Any"
type MyType struct{}
In other words, only the methods you want.
Certain methods, such as Select and GroupBy require an additional type parameter. I won’t bore you with the convoluted old way. Now it’s:
// +gen slice:"GroupBy[string], Select[Foo]"
type MyType struct{}
Those type parameters are properly evaluated, and typewriters get full type information on them.
The main built-in typewriter used to be called genwriter
, it is now called slice
. Instead of the generated slice type being called Things, it’s now called ThingSlice.
slice is now the only built-in typewriter.
We’ve deprecated the built-in container typewriter, instead splitting it into optional Set, List and Ring typewriters.
You can add them using the add
command described above:
gen add github.com/clipperhouse/linkedlist
For those developing their own typewriters: the TypeWriter
interface got smaller. It’s now:
type TypeWriter interface {
Name() string
Imports(t Type) []ImportSpec
Write(w io.Writer, t Type) error
}
Validate
is gone, it was awkward. The easy fix there was to allow Write to return an error. WriteHeader
is gone, there was little use for it in practice. WriteBody
is now simply Write
.
We also run goimports on generated code, so if your typewriter only uses the standard library, you might not need to specify anything for Imports() -- they’ll automagically be added to the generated source.
Let me (@clipperhouse) know if any questions.
Bug fix for pointers: https://github.com/clipperhouse/gen/commit/b6ba7bc72eb7a80223c455993f0b69c202fe789b Bug fix for ‘greater’ func: https://github.com/clipperhouse/gen/commit/f5254a89f8eef8796d4b391002c3cf19e4324c41 Feature: Go-style error messages with line number, etc: https://github.com/clipperhouse/gen/commit/ed75c38f8b9855bd680997cd9d7fda1a5ffd0dd7
A proper tag parser; API unchanged.
Bug fixes
Support for _gen.go
third-party typewriters has been added. See changelog.