Documentation
¶
Overview ¶
Package interfaces 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 Field ¶
type Field struct { Name string `json:"name,omitempty"` Type Type `json:"type,omitempty"` Tags Tags `json:"tags,omitempty"` }
Field
type Func ¶
type Func struct { Name string `json:"name,omitempty"` // name of the function Ins []Type `json:"ins,omitempty"` // input parameters Outs []Type `json:"outs,omitempty"` // output parameters IsVariadic bool // whether the function is variadic }
Func represents an interface function.
func (Func) Deps ¶
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.
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 ¶
package main import ( "fmt" "github.com/rjeczalik/interfaces" ) func main() { i, err := interfaces.New(`github.com/rjeczalik/interfaces.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) Dependencies: flag github.com/rjeczalik/interfaces github.com/rjeczalik/interfaces_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 ¶
package main import ( "fmt" "github.com/rjeczalik/interfaces" ) func main() { 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 CSVHeader []string CSVRecord []string TimeFormat string }
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 ¶
ParseQuery gives new Query for the given query text.
type Type ¶
type Type struct { Name string `json:"name,omitempty"` // type name 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 IsPointer bool `json:"isPointer,omitempty"` // whether the parameter is a pointer IsComposite bool `json:"isComposite,omitempty"` // whether the type is map, slice, chan or array IsFunc bool `json:"isFunc,omitempty"` // whether the type if function }
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.