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

support embedded error interface in source mode #445

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package faux
type Foreign interface {
Method() Return
Embedded
error
}

type Embedded interface{}
Expand Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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