Lint package pkg/plugins/pluginrpc-gen

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2015-09-04 14:55:56 +02:00
parent 825f8fb744
commit 721af25e56
4 changed files with 26 additions and 18 deletions

View file

@ -6,12 +6,15 @@ type wobble struct {
Inception *wobble Inception *wobble
} }
// Fooer is an empty interface used for tests.
type Fooer interface{} type Fooer interface{}
// Fooer2 is an interface used for tests.
type Fooer2 interface { type Fooer2 interface {
Foo() Foo()
} }
// Fooer3 is an interface used for tests.
type Fooer3 interface { type Fooer3 interface {
Foo() Foo()
Bar(a string) Bar(a string)
@ -21,14 +24,17 @@ type Fooer3 interface {
Wiggle() (w wobble) Wiggle() (w wobble)
} }
// Fooer4 is an interface used for tests.
type Fooer4 interface { type Fooer4 interface {
Foo() error Foo() error
} }
// Bar is an interface used for tests.
type Bar interface { type Bar interface {
Boo(a string, b string) (s string, err error) Boo(a string, b string) (s string, err error)
} }
// Fooer5 is an interface used for tests.
type Fooer5 interface { type Fooer5 interface {
Foo() Foo()
Bar Bar

View file

@ -72,7 +72,7 @@ func main() {
InterfaceType string InterfaceType string
RPCName string RPCName string
BuildTags map[string]struct{} BuildTags map[string]struct{}
*parsedPkg *ParsedPkg
}{toLower(*typeName), *rpcName, flBuildTags.GetValues(), pkg} }{toLower(*typeName), *rpcName, flBuildTags.GetValues(), pkg}
var buf bytes.Buffer var buf bytes.Buffer

View file

@ -9,18 +9,20 @@ import (
"reflect" "reflect"
) )
var ErrBadReturn = errors.New("found return arg with no name: all args must be named") var errBadReturn = errors.New("found return arg with no name: all args must be named")
type ErrUnexpectedType struct { type errUnexpectedType struct {
expected string expected string
actual interface{} actual interface{}
} }
func (e ErrUnexpectedType) Error() string { func (e errUnexpectedType) Error() string {
return fmt.Sprintf("got wrong type expecting %s, got: %v", e.expected, reflect.TypeOf(e.actual)) return fmt.Sprintf("got wrong type expecting %s, got: %v", e.expected, reflect.TypeOf(e.actual))
} }
type parsedPkg struct { // ParsedPkg holds information about a package that has been parsed,
// its name and the list of functions.
type ParsedPkg struct {
Name string Name string
Functions []function Functions []function
} }
@ -41,14 +43,14 @@ func (a *arg) String() string {
return a.Name + " " + a.ArgType return a.Name + " " + a.ArgType
} }
// Parses the given file for an interface definition with the given name // Parse parses the given file for an interface definition with the given name.
func Parse(filePath string, objName string) (*parsedPkg, error) { func Parse(filePath string, objName string) (*ParsedPkg, error) {
fs := token.NewFileSet() fs := token.NewFileSet()
pkg, err := parser.ParseFile(fs, filePath, nil, parser.AllErrors) pkg, err := parser.ParseFile(fs, filePath, nil, parser.AllErrors)
if err != nil { if err != nil {
return nil, err return nil, err
} }
p := &parsedPkg{} p := &ParsedPkg{}
p.Name = pkg.Name.Name p.Name = pkg.Name.Name
obj, exists := pkg.Scope.Objects[objName] obj, exists := pkg.Scope.Objects[objName]
if !exists { if !exists {
@ -59,11 +61,11 @@ func Parse(filePath string, objName string) (*parsedPkg, error) {
} }
spec, ok := obj.Decl.(*ast.TypeSpec) spec, ok := obj.Decl.(*ast.TypeSpec)
if !ok { if !ok {
return nil, ErrUnexpectedType{"*ast.TypeSpec", obj.Decl} return nil, errUnexpectedType{"*ast.TypeSpec", obj.Decl}
} }
iface, ok := spec.Type.(*ast.InterfaceType) iface, ok := spec.Type.(*ast.InterfaceType)
if !ok { if !ok {
return nil, ErrUnexpectedType{"*ast.InterfaceType", spec.Type} return nil, errUnexpectedType{"*ast.InterfaceType", spec.Type}
} }
p.Functions, err = parseInterface(iface) p.Functions, err = parseInterface(iface)
@ -90,11 +92,11 @@ func parseInterface(iface *ast.InterfaceType) ([]function, error) {
case *ast.Ident: case *ast.Ident:
spec, ok := f.Obj.Decl.(*ast.TypeSpec) spec, ok := f.Obj.Decl.(*ast.TypeSpec)
if !ok { if !ok {
return nil, ErrUnexpectedType{"*ast.TypeSpec", f.Obj.Decl} return nil, errUnexpectedType{"*ast.TypeSpec", f.Obj.Decl}
} }
iface, ok := spec.Type.(*ast.InterfaceType) iface, ok := spec.Type.(*ast.InterfaceType)
if !ok { if !ok {
return nil, ErrUnexpectedType{"*ast.TypeSpec", spec.Type} return nil, errUnexpectedType{"*ast.TypeSpec", spec.Type}
} }
funcs, err := parseInterface(iface) funcs, err := parseInterface(iface)
if err != nil { if err != nil {
@ -103,7 +105,7 @@ func parseInterface(iface *ast.InterfaceType) ([]function, error) {
} }
functions = append(functions, funcs...) functions = append(functions, funcs...)
default: default:
return nil, ErrUnexpectedType{"*astFuncType or *ast.Ident", f} return nil, errUnexpectedType{"*astFuncType or *ast.Ident", f}
} }
} }
return functions, nil return functions, nil
@ -137,7 +139,7 @@ func parseArgs(fields []*ast.Field) ([]arg, error) {
var args []arg var args []arg
for _, f := range fields { for _, f := range fields {
if len(f.Names) == 0 { if len(f.Names) == 0 {
return nil, ErrBadReturn return nil, errBadReturn
} }
for _, name := range f.Names { for _, name := range f.Names {
var typeName string var typeName string
@ -147,11 +149,11 @@ func parseArgs(fields []*ast.Field) ([]arg, error) {
case *ast.StarExpr: case *ast.StarExpr:
i, ok := argType.X.(*ast.Ident) i, ok := argType.X.(*ast.Ident)
if !ok { if !ok {
return nil, ErrUnexpectedType{"*ast.Ident", f.Type} return nil, errUnexpectedType{"*ast.Ident", f.Type}
} }
typeName = "*" + i.Name typeName = "*" + i.Name
default: default:
return nil, ErrUnexpectedType{"*ast.Ident or *ast.StarExpr", f.Type} return nil, errUnexpectedType{"*ast.Ident or *ast.StarExpr", f.Type}
} }
args = append(args, arg{name.Name, typeName}) args = append(args, arg{name.Name, typeName})

View file

@ -22,7 +22,7 @@ func TestParseEmptyInterface(t *testing.T) {
func TestParseNonInterfaceType(t *testing.T) { func TestParseNonInterfaceType(t *testing.T) {
_, err := Parse(testFixture, "wobble") _, err := Parse(testFixture, "wobble")
if _, ok := err.(ErrUnexpectedType); !ok { if _, ok := err.(errUnexpectedType); !ok {
t.Fatal("expected type error when parsing non-interface type") t.Fatal("expected type error when parsing non-interface type")
} }
} }
@ -109,7 +109,7 @@ func TestParseWithMultipleFuncs(t *testing.T) {
func TestParseWithUnamedReturn(t *testing.T) { func TestParseWithUnamedReturn(t *testing.T) {
_, err := Parse(testFixture, "Fooer4") _, err := Parse(testFixture, "Fooer4")
if !strings.HasSuffix(err.Error(), ErrBadReturn.Error()) { if !strings.HasSuffix(err.Error(), errBadReturn.Error()) {
t.Fatalf("expected ErrBadReturn, got %v", err) t.Fatalf("expected ErrBadReturn, got %v", err)
} }
} }