Browse code

Lint package pkg/plugins/pluginrpc-gen

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2015/09/04 21:55:56
Showing 4 changed files
... ...
@@ -6,12 +6,15 @@ type wobble struct {
6 6
 	Inception *wobble
7 7
 }
8 8
 
9
+// Fooer is an empty interface used for tests.
9 10
 type Fooer interface{}
10 11
 
12
+// Fooer2 is an interface used for tests.
11 13
 type Fooer2 interface {
12 14
 	Foo()
13 15
 }
14 16
 
17
+// Fooer3 is an interface used for tests.
15 18
 type Fooer3 interface {
16 19
 	Foo()
17 20
 	Bar(a string)
... ...
@@ -21,14 +24,17 @@ type Fooer3 interface {
21 21
 	Wiggle() (w wobble)
22 22
 }
23 23
 
24
+// Fooer4 is an interface used for tests.
24 25
 type Fooer4 interface {
25 26
 	Foo() error
26 27
 }
27 28
 
29
+// Bar is an interface used for tests.
28 30
 type Bar interface {
29 31
 	Boo(a string, b string) (s string, err error)
30 32
 }
31 33
 
34
+// Fooer5 is an interface used for tests.
32 35
 type Fooer5 interface {
33 36
 	Foo()
34 37
 	Bar
... ...
@@ -72,7 +72,7 @@ func main() {
72 72
 		InterfaceType string
73 73
 		RPCName       string
74 74
 		BuildTags     map[string]struct{}
75
-		*parsedPkg
75
+		*ParsedPkg
76 76
 	}{toLower(*typeName), *rpcName, flBuildTags.GetValues(), pkg}
77 77
 	var buf bytes.Buffer
78 78
 
... ...
@@ -9,18 +9,20 @@ import (
9 9
 	"reflect"
10 10
 )
11 11
 
12
-var ErrBadReturn = errors.New("found return arg with no name: all args must be named")
12
+var errBadReturn = errors.New("found return arg with no name: all args must be named")
13 13
 
14
-type ErrUnexpectedType struct {
14
+type errUnexpectedType struct {
15 15
 	expected string
16 16
 	actual   interface{}
17 17
 }
18 18
 
19
-func (e ErrUnexpectedType) Error() string {
19
+func (e errUnexpectedType) Error() string {
20 20
 	return fmt.Sprintf("got wrong type expecting %s, got: %v", e.expected, reflect.TypeOf(e.actual))
21 21
 }
22 22
 
23
-type parsedPkg struct {
23
+// ParsedPkg holds information about a package that has been parsed,
24
+// its name and the list of functions.
25
+type ParsedPkg struct {
24 26
 	Name      string
25 27
 	Functions []function
26 28
 }
... ...
@@ -41,14 +43,14 @@ func (a *arg) String() string {
41 41
 	return a.Name + " " + a.ArgType
42 42
 }
43 43
 
44
-// Parses the given file for an interface definition with the given name
45
-func Parse(filePath string, objName string) (*parsedPkg, error) {
44
+// Parse parses the given file for an interface definition with the given name.
45
+func Parse(filePath string, objName string) (*ParsedPkg, error) {
46 46
 	fs := token.NewFileSet()
47 47
 	pkg, err := parser.ParseFile(fs, filePath, nil, parser.AllErrors)
48 48
 	if err != nil {
49 49
 		return nil, err
50 50
 	}
51
-	p := &parsedPkg{}
51
+	p := &ParsedPkg{}
52 52
 	p.Name = pkg.Name.Name
53 53
 	obj, exists := pkg.Scope.Objects[objName]
54 54
 	if !exists {
... ...
@@ -59,11 +61,11 @@ func Parse(filePath string, objName string) (*parsedPkg, error) {
59 59
 	}
60 60
 	spec, ok := obj.Decl.(*ast.TypeSpec)
61 61
 	if !ok {
62
-		return nil, ErrUnexpectedType{"*ast.TypeSpec", obj.Decl}
62
+		return nil, errUnexpectedType{"*ast.TypeSpec", obj.Decl}
63 63
 	}
64 64
 	iface, ok := spec.Type.(*ast.InterfaceType)
65 65
 	if !ok {
66
-		return nil, ErrUnexpectedType{"*ast.InterfaceType", spec.Type}
66
+		return nil, errUnexpectedType{"*ast.InterfaceType", spec.Type}
67 67
 	}
68 68
 
69 69
 	p.Functions, err = parseInterface(iface)
... ...
@@ -90,11 +92,11 @@ func parseInterface(iface *ast.InterfaceType) ([]function, error) {
90 90
 		case *ast.Ident:
91 91
 			spec, ok := f.Obj.Decl.(*ast.TypeSpec)
92 92
 			if !ok {
93
-				return nil, ErrUnexpectedType{"*ast.TypeSpec", f.Obj.Decl}
93
+				return nil, errUnexpectedType{"*ast.TypeSpec", f.Obj.Decl}
94 94
 			}
95 95
 			iface, ok := spec.Type.(*ast.InterfaceType)
96 96
 			if !ok {
97
-				return nil, ErrUnexpectedType{"*ast.TypeSpec", spec.Type}
97
+				return nil, errUnexpectedType{"*ast.TypeSpec", spec.Type}
98 98
 			}
99 99
 			funcs, err := parseInterface(iface)
100 100
 			if err != nil {
... ...
@@ -103,7 +105,7 @@ func parseInterface(iface *ast.InterfaceType) ([]function, error) {
103 103
 			}
104 104
 			functions = append(functions, funcs...)
105 105
 		default:
106
-			return nil, ErrUnexpectedType{"*astFuncType or *ast.Ident", f}
106
+			return nil, errUnexpectedType{"*astFuncType or *ast.Ident", f}
107 107
 		}
108 108
 	}
109 109
 	return functions, nil
... ...
@@ -137,7 +139,7 @@ func parseArgs(fields []*ast.Field) ([]arg, error) {
137 137
 	var args []arg
138 138
 	for _, f := range fields {
139 139
 		if len(f.Names) == 0 {
140
-			return nil, ErrBadReturn
140
+			return nil, errBadReturn
141 141
 		}
142 142
 		for _, name := range f.Names {
143 143
 			var typeName string
... ...
@@ -147,11 +149,11 @@ func parseArgs(fields []*ast.Field) ([]arg, error) {
147 147
 			case *ast.StarExpr:
148 148
 				i, ok := argType.X.(*ast.Ident)
149 149
 				if !ok {
150
-					return nil, ErrUnexpectedType{"*ast.Ident", f.Type}
150
+					return nil, errUnexpectedType{"*ast.Ident", f.Type}
151 151
 				}
152 152
 				typeName = "*" + i.Name
153 153
 			default:
154
-				return nil, ErrUnexpectedType{"*ast.Ident or *ast.StarExpr", f.Type}
154
+				return nil, errUnexpectedType{"*ast.Ident or *ast.StarExpr", f.Type}
155 155
 			}
156 156
 
157 157
 			args = append(args, arg{name.Name, typeName})
... ...
@@ -22,7 +22,7 @@ func TestParseEmptyInterface(t *testing.T) {
22 22
 
23 23
 func TestParseNonInterfaceType(t *testing.T) {
24 24
 	_, err := Parse(testFixture, "wobble")
25
-	if _, ok := err.(ErrUnexpectedType); !ok {
25
+	if _, ok := err.(errUnexpectedType); !ok {
26 26
 		t.Fatal("expected type error when parsing non-interface type")
27 27
 	}
28 28
 }
... ...
@@ -109,7 +109,7 @@ func TestParseWithMultipleFuncs(t *testing.T) {
109 109
 
110 110
 func TestParseWithUnamedReturn(t *testing.T) {
111 111
 	_, err := Parse(testFixture, "Fooer4")
112
-	if !strings.HasSuffix(err.Error(), ErrBadReturn.Error()) {
112
+	if !strings.HasSuffix(err.Error(), errBadReturn.Error()) {
113 113
 		t.Fatalf("expected ErrBadReturn, got %v", err)
114 114
 	}
115 115
 }