From 721af25e5614a32ebb2e9c6f2a5bf2fb233c8718 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Fri, 4 Sep 2015 14:55:56 +0200 Subject: [PATCH] Lint package pkg/plugins/pluginrpc-gen Signed-off-by: Vincent Demeester --- plugins/pluginrpc-gen/fixtures/foo.go | 6 +++++ plugins/pluginrpc-gen/main.go | 2 +- plugins/pluginrpc-gen/parser.go | 32 ++++++++++++++------------- plugins/pluginrpc-gen/parser_test.go | 4 ++-- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/plugins/pluginrpc-gen/fixtures/foo.go b/plugins/pluginrpc-gen/fixtures/foo.go index 4e73fc1..fcb2b62 100644 --- a/plugins/pluginrpc-gen/fixtures/foo.go +++ b/plugins/pluginrpc-gen/fixtures/foo.go @@ -6,12 +6,15 @@ type wobble struct { Inception *wobble } +// Fooer is an empty interface used for tests. type Fooer interface{} +// Fooer2 is an interface used for tests. type Fooer2 interface { Foo() } +// Fooer3 is an interface used for tests. type Fooer3 interface { Foo() Bar(a string) @@ -21,14 +24,17 @@ type Fooer3 interface { Wiggle() (w wobble) } +// Fooer4 is an interface used for tests. type Fooer4 interface { Foo() error } +// Bar is an interface used for tests. type Bar interface { Boo(a string, b string) (s string, err error) } +// Fooer5 is an interface used for tests. type Fooer5 interface { Foo() Bar diff --git a/plugins/pluginrpc-gen/main.go b/plugins/pluginrpc-gen/main.go index 2130af1..772984c 100644 --- a/plugins/pluginrpc-gen/main.go +++ b/plugins/pluginrpc-gen/main.go @@ -72,7 +72,7 @@ func main() { InterfaceType string RPCName string BuildTags map[string]struct{} - *parsedPkg + *ParsedPkg }{toLower(*typeName), *rpcName, flBuildTags.GetValues(), pkg} var buf bytes.Buffer diff --git a/plugins/pluginrpc-gen/parser.go b/plugins/pluginrpc-gen/parser.go index b84fef0..3adeb49 100644 --- a/plugins/pluginrpc-gen/parser.go +++ b/plugins/pluginrpc-gen/parser.go @@ -9,18 +9,20 @@ import ( "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 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)) } -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 Functions []function } @@ -41,14 +43,14 @@ func (a *arg) String() string { return a.Name + " " + a.ArgType } -// Parses the given file for an interface definition with the given name -func Parse(filePath string, objName string) (*parsedPkg, error) { +// Parse parses the given file for an interface definition with the given name. +func Parse(filePath string, objName string) (*ParsedPkg, error) { fs := token.NewFileSet() pkg, err := parser.ParseFile(fs, filePath, nil, parser.AllErrors) if err != nil { return nil, err } - p := &parsedPkg{} + p := &ParsedPkg{} p.Name = pkg.Name.Name obj, exists := pkg.Scope.Objects[objName] if !exists { @@ -59,11 +61,11 @@ func Parse(filePath string, objName string) (*parsedPkg, error) { } spec, ok := obj.Decl.(*ast.TypeSpec) if !ok { - return nil, ErrUnexpectedType{"*ast.TypeSpec", obj.Decl} + return nil, errUnexpectedType{"*ast.TypeSpec", obj.Decl} } iface, ok := spec.Type.(*ast.InterfaceType) if !ok { - return nil, ErrUnexpectedType{"*ast.InterfaceType", spec.Type} + return nil, errUnexpectedType{"*ast.InterfaceType", spec.Type} } p.Functions, err = parseInterface(iface) @@ -90,11 +92,11 @@ func parseInterface(iface *ast.InterfaceType) ([]function, error) { case *ast.Ident: spec, ok := f.Obj.Decl.(*ast.TypeSpec) if !ok { - return nil, ErrUnexpectedType{"*ast.TypeSpec", f.Obj.Decl} + return nil, errUnexpectedType{"*ast.TypeSpec", f.Obj.Decl} } iface, ok := spec.Type.(*ast.InterfaceType) if !ok { - return nil, ErrUnexpectedType{"*ast.TypeSpec", spec.Type} + return nil, errUnexpectedType{"*ast.TypeSpec", spec.Type} } funcs, err := parseInterface(iface) if err != nil { @@ -103,7 +105,7 @@ func parseInterface(iface *ast.InterfaceType) ([]function, error) { } functions = append(functions, funcs...) default: - return nil, ErrUnexpectedType{"*astFuncType or *ast.Ident", f} + return nil, errUnexpectedType{"*astFuncType or *ast.Ident", f} } } return functions, nil @@ -137,7 +139,7 @@ func parseArgs(fields []*ast.Field) ([]arg, error) { var args []arg for _, f := range fields { if len(f.Names) == 0 { - return nil, ErrBadReturn + return nil, errBadReturn } for _, name := range f.Names { var typeName string @@ -147,11 +149,11 @@ func parseArgs(fields []*ast.Field) ([]arg, error) { case *ast.StarExpr: i, ok := argType.X.(*ast.Ident) if !ok { - return nil, ErrUnexpectedType{"*ast.Ident", f.Type} + return nil, errUnexpectedType{"*ast.Ident", f.Type} } typeName = "*" + i.Name 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}) diff --git a/plugins/pluginrpc-gen/parser_test.go b/plugins/pluginrpc-gen/parser_test.go index 6c5665f..5a7579c 100644 --- a/plugins/pluginrpc-gen/parser_test.go +++ b/plugins/pluginrpc-gen/parser_test.go @@ -22,7 +22,7 @@ func TestParseEmptyInterface(t *testing.T) { func TestParseNonInterfaceType(t *testing.T) { _, err := Parse(testFixture, "wobble") - if _, ok := err.(ErrUnexpectedType); !ok { + if _, ok := err.(errUnexpectedType); !ok { t.Fatal("expected type error when parsing non-interface type") } } @@ -109,7 +109,7 @@ func TestParseWithMultipleFuncs(t *testing.T) { func TestParseWithUnamedReturn(t *testing.T) { _, 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) } }