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.
type Interface ¶
type Interface []Func
Interface represents a typed interface.
func New ¶
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 ¶
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
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.
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.

