struct2interface

package module
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2023 License: MIT Imports: 12 Imported by: 3

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeDir

func MakeDir(dir string) error

Types

type Method

type Method struct {
	Code string
	Docs []string
}

func (*Method) Lines

func (m *Method) Lines() []string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL