2015-06-08 11:33:06 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/parser"
|
|
|
|
"go/token"
|
2016-04-09 17:42:24 -04:00
|
|
|
"path"
|
2015-06-08 11:33:06 -04:00
|
|
|
"reflect"
|
2016-04-09 17:42:24 -04:00
|
|
|
"strings"
|
2015-06-08 11:33:06 -04:00
|
|
|
)
|
|
|
|
|
2015-09-04 08:55:56 -04:00
|
|
|
var errBadReturn = errors.New("found return arg with no name: all args must be named")
|
2015-06-08 11:33:06 -04:00
|
|
|
|
2015-09-04 08:55:56 -04:00
|
|
|
type errUnexpectedType struct {
|
2015-06-08 11:33:06 -04:00
|
|
|
expected string
|
|
|
|
actual interface{}
|
|
|
|
}
|
|
|
|
|
2015-09-04 08:55:56 -04:00
|
|
|
func (e errUnexpectedType) Error() string {
|
2015-06-08 11:33:06 -04:00
|
|
|
return fmt.Sprintf("got wrong type expecting %s, got: %v", e.expected, reflect.TypeOf(e.actual))
|
|
|
|
}
|
|
|
|
|
2015-09-04 08:55:56 -04:00
|
|
|
// ParsedPkg holds information about a package that has been parsed,
|
|
|
|
// its name and the list of functions.
|
|
|
|
type ParsedPkg struct {
|
2015-06-08 11:33:06 -04:00
|
|
|
Name string
|
|
|
|
Functions []function
|
2016-04-09 17:42:24 -04:00
|
|
|
Imports []importSpec
|
2015-06-08 11:33:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type function struct {
|
|
|
|
Name string
|
|
|
|
Args []arg
|
|
|
|
Returns []arg
|
|
|
|
Doc string
|
|
|
|
}
|
|
|
|
|
|
|
|
type arg struct {
|
2016-04-09 17:42:24 -04:00
|
|
|
Name string
|
|
|
|
ArgType string
|
|
|
|
PackageSelector string
|
2015-06-08 11:33:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *arg) String() string {
|
2015-06-12 09:25:32 -04:00
|
|
|
return a.Name + " " + a.ArgType
|
2015-06-08 11:33:06 -04:00
|
|
|
}
|
|
|
|
|
2016-04-09 17:42:24 -04:00
|
|
|
type importSpec struct {
|
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *importSpec) String() string {
|
|
|
|
var ss string
|
|
|
|
if len(s.Name) != 0 {
|
|
|
|
ss += s.Name
|
|
|
|
}
|
|
|
|
ss += s.Path
|
|
|
|
return ss
|
|
|
|
}
|
|
|
|
|
2015-09-04 08:55:56 -04:00
|
|
|
// Parse parses the given file for an interface definition with the given name.
|
|
|
|
func Parse(filePath string, objName string) (*ParsedPkg, error) {
|
2015-06-08 11:33:06 -04:00
|
|
|
fs := token.NewFileSet()
|
|
|
|
pkg, err := parser.ParseFile(fs, filePath, nil, parser.AllErrors)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-04 08:55:56 -04:00
|
|
|
p := &ParsedPkg{}
|
2015-06-08 11:33:06 -04:00
|
|
|
p.Name = pkg.Name.Name
|
|
|
|
obj, exists := pkg.Scope.Objects[objName]
|
|
|
|
if !exists {
|
|
|
|
return nil, fmt.Errorf("could not find object %s in %s", objName, filePath)
|
|
|
|
}
|
|
|
|
if obj.Kind != ast.Typ {
|
|
|
|
return nil, fmt.Errorf("exected type, got %s", obj.Kind)
|
|
|
|
}
|
|
|
|
spec, ok := obj.Decl.(*ast.TypeSpec)
|
|
|
|
if !ok {
|
2015-09-04 08:55:56 -04:00
|
|
|
return nil, errUnexpectedType{"*ast.TypeSpec", obj.Decl}
|
2015-06-08 11:33:06 -04:00
|
|
|
}
|
|
|
|
iface, ok := spec.Type.(*ast.InterfaceType)
|
|
|
|
if !ok {
|
2015-09-04 08:55:56 -04:00
|
|
|
return nil, errUnexpectedType{"*ast.InterfaceType", spec.Type}
|
2015-06-08 11:33:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p.Functions, err = parseInterface(iface)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-04-09 17:42:24 -04:00
|
|
|
// figure out what imports will be needed
|
|
|
|
imports := make(map[string]importSpec)
|
|
|
|
for _, f := range p.Functions {
|
|
|
|
args := append(f.Args, f.Returns...)
|
|
|
|
for _, arg := range args {
|
|
|
|
if len(arg.PackageSelector) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, i := range pkg.Imports {
|
|
|
|
if i.Name != nil {
|
|
|
|
if i.Name.Name != arg.PackageSelector {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
imports[i.Path.Value] = importSpec{Name: arg.PackageSelector, Path: i.Path.Value}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
_, name := path.Split(i.Path.Value)
|
|
|
|
splitName := strings.Split(name, "-")
|
|
|
|
if len(splitName) > 1 {
|
|
|
|
name = splitName[len(splitName)-1]
|
|
|
|
}
|
|
|
|
// import paths have quotes already added in, so need to remove them for name comparison
|
|
|
|
name = strings.TrimPrefix(name, `"`)
|
|
|
|
name = strings.TrimSuffix(name, `"`)
|
|
|
|
if name == arg.PackageSelector {
|
|
|
|
imports[i.Path.Value] = importSpec{Path: i.Path.Value}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, spec := range imports {
|
|
|
|
p.Imports = append(p.Imports, spec)
|
|
|
|
}
|
|
|
|
|
2015-06-08 11:33:06 -04:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseInterface(iface *ast.InterfaceType) ([]function, error) {
|
|
|
|
var functions []function
|
|
|
|
for _, field := range iface.Methods.List {
|
|
|
|
switch f := field.Type.(type) {
|
|
|
|
case *ast.FuncType:
|
|
|
|
method, err := parseFunc(field)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if method == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
functions = append(functions, *method)
|
|
|
|
case *ast.Ident:
|
|
|
|
spec, ok := f.Obj.Decl.(*ast.TypeSpec)
|
|
|
|
if !ok {
|
2015-09-04 08:55:56 -04:00
|
|
|
return nil, errUnexpectedType{"*ast.TypeSpec", f.Obj.Decl}
|
2015-06-08 11:33:06 -04:00
|
|
|
}
|
|
|
|
iface, ok := spec.Type.(*ast.InterfaceType)
|
|
|
|
if !ok {
|
2015-09-04 08:55:56 -04:00
|
|
|
return nil, errUnexpectedType{"*ast.TypeSpec", spec.Type}
|
2015-06-08 11:33:06 -04:00
|
|
|
}
|
|
|
|
funcs, err := parseInterface(iface)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
functions = append(functions, funcs...)
|
|
|
|
default:
|
2015-09-04 08:55:56 -04:00
|
|
|
return nil, errUnexpectedType{"*astFuncType or *ast.Ident", f}
|
2015-06-08 11:33:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return functions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseFunc(field *ast.Field) (*function, error) {
|
|
|
|
f := field.Type.(*ast.FuncType)
|
|
|
|
method := &function{Name: field.Names[0].Name}
|
|
|
|
if _, exists := skipFuncs[method.Name]; exists {
|
|
|
|
fmt.Println("skipping:", method.Name)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if f.Params != nil {
|
|
|
|
args, err := parseArgs(f.Params.List)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
method.Args = args
|
|
|
|
}
|
|
|
|
if f.Results != nil {
|
|
|
|
returns, err := parseArgs(f.Results.List)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing function returns for %q: %v", method.Name, err)
|
|
|
|
}
|
|
|
|
method.Returns = returns
|
|
|
|
}
|
|
|
|
return method, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseArgs(fields []*ast.Field) ([]arg, error) {
|
|
|
|
var args []arg
|
|
|
|
for _, f := range fields {
|
|
|
|
if len(f.Names) == 0 {
|
2015-09-04 08:55:56 -04:00
|
|
|
return nil, errBadReturn
|
2015-06-08 11:33:06 -04:00
|
|
|
}
|
|
|
|
for _, name := range f.Names {
|
2016-04-09 17:42:24 -04:00
|
|
|
p, err := parseExpr(f.Type)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-06-08 11:33:06 -04:00
|
|
|
}
|
2016-04-09 17:42:24 -04:00
|
|
|
args = append(args, arg{name.Name, p.value, p.pkg})
|
2015-06-08 11:33:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return args, nil
|
|
|
|
}
|
2016-04-09 17:42:24 -04:00
|
|
|
|
|
|
|
type parsedExpr struct {
|
|
|
|
value string
|
|
|
|
pkg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseExpr(e ast.Expr) (parsedExpr, error) {
|
|
|
|
var parsed parsedExpr
|
|
|
|
switch i := e.(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
parsed.value += i.Name
|
|
|
|
case *ast.StarExpr:
|
|
|
|
p, err := parseExpr(i.X)
|
|
|
|
if err != nil {
|
|
|
|
return parsed, err
|
|
|
|
}
|
|
|
|
parsed.value += "*"
|
|
|
|
parsed.value += p.value
|
|
|
|
parsed.pkg = p.pkg
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
p, err := parseExpr(i.X)
|
|
|
|
if err != nil {
|
|
|
|
return parsed, err
|
|
|
|
}
|
|
|
|
parsed.pkg = p.value
|
|
|
|
parsed.value += p.value + "."
|
|
|
|
parsed.value += i.Sel.Name
|
|
|
|
case *ast.MapType:
|
|
|
|
parsed.value += "map["
|
|
|
|
p, err := parseExpr(i.Key)
|
|
|
|
if err != nil {
|
|
|
|
return parsed, err
|
|
|
|
}
|
|
|
|
parsed.value += p.value
|
|
|
|
parsed.value += "]"
|
|
|
|
p, err = parseExpr(i.Value)
|
|
|
|
if err != nil {
|
|
|
|
return parsed, err
|
|
|
|
}
|
|
|
|
parsed.value += p.value
|
|
|
|
parsed.pkg = p.pkg
|
|
|
|
case *ast.ArrayType:
|
|
|
|
parsed.value += "[]"
|
|
|
|
p, err := parseExpr(i.Elt)
|
|
|
|
if err != nil {
|
|
|
|
return parsed, err
|
|
|
|
}
|
|
|
|
parsed.value += p.value
|
|
|
|
parsed.pkg = p.pkg
|
|
|
|
default:
|
|
|
|
return parsed, errUnexpectedType{"*ast.Ident or *ast.StarExpr", i}
|
|
|
|
}
|
|
|
|
return parsed, nil
|
|
|
|
}
|