Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
support embedded error interface in source mode (#445)
Browse files Browse the repository at this point in the history
Fixes: #446
  • Loading branch information
wencan committed Jul 10, 2020
1 parent 44e6f1e commit 5b2ea10
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -3,6 +3,7 @@ package faux
type Foreign interface {
Method() Return
Embedded
error
}

type Embedded interface{}
Expand Down
Expand Up @@ -27,6 +27,7 @@ import (
type Source interface {
ersatz.Embedded
faux.Foreign
error
}

func CallForeignMethod(s Source) {
Expand Down
14 changes: 14 additions & 0 deletions mockgen/internal/tests/import_embedded_interface/bugreport_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions mockgen/model/model.go
Expand Up @@ -468,3 +468,19 @@ func impPath(imp string) string {
}
return imp
}

// ErrorInterface represent built-in error interface.
var ErrorInterface = Interface{
Name: "error",
Methods: []*Method{
{
Name: "Error",
Out: []*Parameter{
{
Name: "",
Type: PredeclaredType("string"),
},
},
},
},
}
20 changes: 15 additions & 5 deletions mockgen/parse.go
Expand Up @@ -272,14 +272,24 @@ func (p *fileParser) parseInterface(name, pkg string, it *ast.InterfaceType) (*m
// Embedded interface in this package.
ei := p.auxInterfaces[pkg][v.String()]
if ei == nil {
if ei = p.importedInterfaces[pkg][v.String()]; ei == nil {
ei = p.importedInterfaces[pkg][v.String()]
}

var eintf *model.Interface
if ei != nil {
var err error
eintf, err = p.parseInterface(v.String(), pkg, ei)
if err != nil {
return nil, err
}
} else {
// This is built-in error interface.
if v.String() == model.ErrorInterface.Name {
eintf = &model.ErrorInterface
} else {
return nil, p.errorf(v.Pos(), "unknown embedded interface %s", v.String())
}
}
eintf, err := p.parseInterface(v.String(), pkg, ei)
if err != nil {
return nil, err
}
// Copy the methods.
// TODO: apply shadowing rules.
intf.Methods = append(intf.Methods, eintf.Methods...)
Expand Down

0 comments on commit 5b2ea10

Please sign in to comment.