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
}
// 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

View file

@ -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

View file

@ -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})

View file

@ -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)
}
}