Skip to content

Development helper program that generates a Golang interface by inspecting the structure methods of an existing .go file.

License

Notifications You must be signed in to change notification settings

sysulq/struct2interface

Repository files navigation

struct2interface

Go

This is a development helper program that generates a Golang interface by inspecting the structure methods of an existing .go file. The primary use case is to generate interfaces for mockery, so that mockery can generate mocks from those interfaces. This makes unit testing easier.

Install

go install github.com/hnlq715/struct2interface/cmd/struct2interface@latest

Usage

Here is the help output of struct2interface:

$ struct2interface --help
Usage:
  struct2interface [flags]

Flags:
  -d, --dir string   Go source file dir to read (default ".")
  -h, --help         help for struct2interface

As an example, let's say you wanted to generate an interface for the Method structure in this sample code:

package testdata

// Method describes the code and documentation
// tied into a method
type Method struct {
	Code string
	Docs []string
}

// Lines return a []string consisting of
// the documentation and code appended
// in chronological order
func (m *Method) Lines() []string {
	var lines []string
	lines = append(lines, m.Docs...)
	lines = append(lines, m.Code)
	return lines
}

The struct2interface helper program can generate this interface for you:

$ struct2interface -d testdata
struct2interface: testdata: wrote testdata/interface_Method.go
struct2interface: testdata: wrote testdata/interface_Method1.go

About

Development helper program that generates a Golang interface by inspecting the structure methods of an existing .go file.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages