interfacer

package module
v0.0.0-...-0f37144 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2019 License: MIT Imports: 8 Imported by: 0

README

interfacer GoDoc

Code generation tools for Go's interfaces.

Tools available in this repository:

cmd/interfacer GoDoc

Generates an interface for a named type.

Installation

~ $ go get github.com/rjeczalik/interfaces/cmd/interfacer

Usage

~ $ interfacer -help
Usage of interfacer:
  -all
        Include also unexported methods.
  -as string
        Generated interface name. (default "main.Interface")
  -for string
        Type to generate an interface for.
  -o string
        Output file. (default "-")

Example

  • generate by manually
~ $ interfacer -for os.File -as mock.File
  • generate by go generate
//go:generate interfacer -for os.File -as mock.File -o file_iface.go
~ $ go generate  ./...
  • output
// Created by interfacer; DO NOT EDIT

package mock

import (
        "os"
)

// File is an interface generated for "os".File.
type File interface {
        Chdir() error
        Chmod(os.FileMode) error
        Chown(int, int) error
        Close() error
        Fd() uintptr
        Name() string
        Read([]byte) (int, error)
        ReadAt([]byte, int64) (int, error)
        Readdir(int) ([]os.FileInfo, error)
        Readdirnames(int) ([]string, error)
        Seek(int64, int) (int64, error)
        Stat() (os.FileInfo, error)
        Sync() error
        Truncate(int64) error
        Write([]byte) (int, error)
        WriteAt([]byte, int64) (int, error)
        WriteString(string) (int, error)
}

Documentation

Overview

Package interfacer provides functionality for parsing and building interface type models for simple code generation purposes.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Func

type Func struct {
	Definition string `json:"definition,omitempty"` // Go code representation of the function
	Ins        []Type `json:"ins,omitempty"`        // input parameters
	Outs       []Type `json:"outs,omitempty"`       // output parameters
}

Func represents an interface function.

func (Func) Deps

func (f Func) Deps() []string

Deps gives a list of packages the function depends on. E.g. if the function represents Serve(net.Listener, http.Handler) error, calling Deps() will return []string{"http", "net"}.

The packages are sorted by name.

func (Func) String

func (f Func) String() string

String gives Go code representation of the function.

Example
f := interfaces.Func{
	Definition: "Close() error",
	Outs:       []interfaces.Type{{}},
}
fmt.Println(f)
Output:

Close() error

type Interface

type Interface []Func

Interface represents a typed interface.

func New

func New(query string) (Interface, error)

New builds an interface definition for a type specified by the query. Supported query format is "package".Type (similar to what gorename tool accepts).

The function expects sources for the requested type to be present in current GOPATH.

Example
i, err := interfaces.New(`github.com/sbward/interfacer.ExampleBaz`)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println("Interface:")
for _, fn := range i {
	fmt.Println(fn)
}
fmt.Println("Dependencies:")
for _, dep := range i.Deps() {
	fmt.Println(dep)
}
Output:

Interface:
A(int) int
B(*string, io.Writer, interfaces_test.ExampleFoo) (*interfaces_test.ExampleFoo, int)
C(map[string]int, *interfaces.Options, *http.Client) (chan []string, error)
D(*map[interface{}]struct{}, interface{}) (chan struct{}, []interface{})
E(*[]map[*flag.FlagSet]struct{}, [3]string)
F(bool, ...string)
Dependencies:
flag
github.com/sbward/interfacer
github.com/sbward/interfacer_test
io
net/http

func NewWithOptions

func NewWithOptions(opts *Options) (Interface, error)

NewWithOptions builds an interface definition for a type specified by the given Options.

The Options may be used to specify e.g. different GOPATH if sources for requested type are not available in the current one.

Example
opts := &interfaces.Options{
	Query: &interfaces.Query{
		Package:  "net",
		TypeName: "Interface",
	},
}
i, err := interfaces.NewWithOptions(opts)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println("Interface:")
for _, fn := range i {
	fmt.Println(fn)
}
fmt.Println("Dependencies:")
for _, dep := range i.Deps() {
	fmt.Println(dep)
}
Output:

Interface:
Addrs() ([]net.Addr, error)
MulticastAddrs() ([]net.Addr, error)
Dependencies:
net

func (Interface) Deps

func (i Interface) Deps() []string

Deps gives a list of packages the interface depends on.

type Options

type Options struct {
	Query      *Query         // a named type
	Context    *build.Context // build context; see go/build godoc for details
	Unexported bool           // whether to include unexported methods
}

Options is used for altering behavior of New() function.

type Query

type Query struct {
	TypeName string `json:"name,omitempty"`
	Package  string `json:"package,omitempty"`
}

Query represents a named type request.

func ParseQuery

func ParseQuery(query string) (*Query, error)

ParseQuery gives new Query for the given query text.

type Type

type Type struct {
	Package    string `json:"package,omitempty"`    // package name the type is defined in; empty for builtin
	ImportPath string `json:"importPath,omitempty"` // import path of the package
}

Type is a simple representation of a single parameter type.

Notes

Bugs

  • Does not work with recursive types.

  • Does not work with with more than one level of indirection (pointer to pointers).

  • Does not and will not work with struct literals.

  • May incorrectly generate dependencies for a map types which key and value are named types imported from different packages. As a workaround run goimports over the output file.

Directories

Path Synopsis
cmd
interfacer command
Command interfacer
Command interfacer

Jump to

Keyboard shortcuts

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