Home

Awesome

goderive

Build Status Go Report Card GoDoc

goderive derives mundane golang functions that you do not want to maintain and keeps them up to date.

<a href="http://www.youtube.com/watch?feature=player_embedded&v=qFYByoGFIUE" target="_blank"> <img src="https://img.youtube.com/vi/qFYByoGFIUE/maxres2.jpg" alt="Watch the video" width="480" border="10" /> </a>

It does this by parsing your go code for functions, which are not implemented, and then generates these functions for you by deriving their implementations from the input parameter types.

Examples

In the following code the deriveEqual function will be spotted as a function that was not implemented (or was previously derived) and has a prefix deriveEqual.

package main

type MyStruct struct {
	Int64     int64
	StringPtr *string
}

func (this *MyStruct) Equal(that *MyStruct) bool {
	return deriveEqual(this, that)
}

goderive will then generate the following code in a derived.gen.go file in the same package:

func deriveEqual(this, that *MyStruct) bool {
	return (this == nil && that == nil) ||
		this != nil && that != nil &&
			this.Int64 == that.Int64 &&
			((this.StringPtr == nil && that.StringPtr == nil) ||
        (this.StringPtr != nil && that.StringPtr != nil && *(this.StringPtr) == *(that.StringPtr)))
}

Recursive Examples:

Set Examples:

Functional Examples:

Concurrency Examples:

Functions

Recursive Functions:

Set Functions:

Functional Functions:

Concurrency Functions:

When goderive walks over your code it is looking for a function that:

Functions which have been previously derived will be regenerated to keep them up to date with the latest modifications to your types. This keeps these functions, which are truly mundane to write, maintainable.

For example when someone in your team adds a new field to a struct and forgets to update the CopyTo method. This problem is solved by goderive, by generating generated functions given the new types.

Function prefixes are by default deriveCamelCaseFunctionName, for example deriveEqual. These are customizable using command line flags.

You can derive functions for different types by using different suffixes with the same prefix. For example, if you wish to derive Equal for types MyStruct and MySecondStruct, name the functions deriveEqualMyStruct and deriveEqualMySecondStruct and goderive will derive both.

Let goderive edit your function names in your source code, by enabling autoname and dedup using the command line flags. These flags respectively make sure that your functions have unique names and that you don't generate multiple functions that do the same thing.

How to run

install the latest version of goderive globally using:

go install github.com/awalterschulze/goderive@latest

goderive can be run from the command line:

goderive ./...

, using the same path semantics as the go tool.

You can also run goderive using go generate

And you can customize specific function prefixes

Or you can customize all function prefixes

You can let goderive rename your functions using the -autoname and -dedup flags. If these flags are not used, goderive will not touch your code and rather return an error.

Customization

The derive package allows you to create your own code generator plugins, see all the current plugins for examples.

You can also create your own vanity binary. Including your own generators and/or customization of function prefixes, etc. This should be easy to figure out by looking at main.go

Inspired By

Users

These projects use goderive:

Please let us know if you are using goderive by opening an issue or a pull request that adds your project to the list.

Mentioned

Please let us know if you mention goderive in a blog post, talk or go experience report, so that we can add a link to our list.

Presentations