当前位置:网站首页>gomock mockgen : unknown embedded interface
gomock mockgen : unknown embedded interface
2022-06-27 19:18:00 【禅与计算机程序设计艺术】
Issue
mockgen -source=./driver/rocket_driver.go -destination ./driver/rocket_driver_mock.go -package driver
2022/06/01 21:39:44 Loading input failed: ./driver/rocket_driver.go:6:2: unknown embedded interface INavigatorDriver
make: *** [mockgen_rocket_driver] Error 1https://github.com/golang/mock/issues/178
Here are my test files:
github.com/anyuser/test/test1.go:
package test
type A interface {
Method1()
}github.com/anyuser/test/test2.go:
package test
type B interface {
A
}Here's generation:
mockgen -source test2.go -destination test2_mocks.go -package test -aux_files test=test1.goError result:
Loading input failed: test2.go:4:2: unknown embedded interface AEmbedded Interfaces in aux_files
Embedded interfaces in aux_files generate unknown embedded interface XXX errors. See below for example of the problem:
// source
import (
alias "some.org/package/imported"
)
type Source interface {
alias.Foreign
}
// some.org/package/imported
type Foreign interface {
Embedded
}
type Embedded interface {}Attempting to generate a mock will result in an unknown embedded interface Embedded. The issue is that the fileParser stores auxInterfaces underneath the package name explicitly specified in the aux_files flag.
In the parseInterface method, there is an incorrect assumption about an embedded interface always being in the source file.
case *ast.Ident:
// Embedded interface in this package.
ei := p.auxInterfaces[""][v.String()]
if ei == nil {
return nil, p.errorf(v.Pos(), "unknown embedded interface %s", v.String())
}Solution
不要把 rocket_driver.go 跟其他依赖的 interface 放到同一个 package 下面 ( mockgen 的默认假设? ) .
mockgen -source=./rocket/rocket_driver.go
-destination ./rocket/rocket_driver_mock.go
-package rocket
-aux_files rocket=service/basic_info_service.go,rocket=driver/navigator_driver.gotype IRocketFetcher interface {
service.BasicInfoService
driver.INavigatorDriver
}
type RocketFetcher struct {
}
func NewRocketFetcher() *RocketFetcher { return &RocketFetcher{} }源代码工程目录:
.
├── Makefile
├── README.md
├── alpha
│ ├── cql.go
│ ├── cql_compiler.go
│ ├── cql_compiler_test.go
│ └── cql_util.go
├── component
│ ├── component.go
│ └── component_test.go
├── constant
│ └── sql_key.go
├── datasource
│ └── data_source.go
├── driver
│ ├── navigator_driver.go
│ ├── navigator_driver_mock.go
│ ├── navigator_driver_mock_data.go
│ ├── sqlite_driver.go
│ └── sqlite_driver_test.go
├── go.mod
├── go.sum
├── lang
│ └── lang.go
├── onetable
│ ├── one_table.go
│ └── one_table_test.go
├── rocket
│ ├── basic_common.go
│ ├── basic_common_test.go
│ ├── index_common.go
│ ├── rocket_fetcher.go
│ ├── rocket_fetcher_mock.go
│ ├── rocket_fetcher_test.go
│ ├── rsd.go
│ └── rsd_test.go
├── service
│ ├── basic_info_service.go
│ ├── basic_info_service_mock.go
│ └── basic_info_service_test.go
├── stream
│ ├── parallel.go
│ ├── parallel_test.go
│ ├── ring.go
│ ├── ring_test.go
│ ├── stream.go
│ └── stream_test.go
├── stringx
│ ├── node.go
│ ├── node_fuzz_test.go
│ ├── node_test.go
│ ├── random.go
│ ├── random_test.go
│ ├── replacer.go
│ ├── replacer_fuzz_test.go
│ ├── replacer_test.go
│ ├── strings.go
│ ├── strings_test.go
│ ├── trie.go
│ └── trie_test.go
├── test_report.html
├── threading
│ ├── coroutinegroup.go
│ ├── coroutinegroup_test.go
│ ├── recover.go
│ ├── recover_test.go
│ ├── routines.go
│ ├── routines_test.go
│ ├── taskrunner.go
│ ├── taskrunner_test.go
│ ├── workergroup.go
│ └── workergroup_test.go
├── udfs
│ ├── indexation_convert.go
│ └── udf.go
└── utils
├── basic
│ ├── basic_common.go
│ ├── basic_constu.go
│ ├── basic_util.go
│ └── hotsoon_item_pack.go
├── chart
│ └── chart_common.go
├── co
│ ├── concurrent.go
│ └── concurrent_test.go
├── convert.go
├── copy
│ ├── copier.go
│ └── errors.go
├── dimu
│ └── dimu.go
├── indexu
│ ├── constu.go
│ ├── indexu.go
│ └── parse_request.go
├── modelu
│ ├── error.go
│ └── model.go
├── numberu
│ └── numberu.go
├── rocket_constu
│ └── constu.go
├── slicesu
│ └── slicesu.go
├── stringu
│ └── stringu.go
├── structu
│ └── structu.go
└── timeu
├── date_type.go
├── timeu.go
└── timeu_test.go
27 directories, 86 filesmockgen 指令参数说明
-source :
A file containing interfaces to be mocked.
-destination :
A file to which to write the resulting source code.
If you don't set this, the code is printed to standard output.
-package :
The package to use for the resulting mock class source code.
If you don't set this, the package name is mock_ concatenated with the package of the input file.
-imports :
A list of explicit imports that should be used in the resulting source code,
specified as a comma-separated list of elements of the form `foo=bar/baz` ,
where `bar/baz` is the package being imported ,
and `foo` is the identifier to use for the package in the generated source code.
-aux_files:
A list of additional files that should be consulted to resolve
e.g. embedded interfaces defined in a different file.
This is specified as a comma-separated list of elements of the form foo=bar/baz.go,
where bar/baz.go is the source file
and foo is the package name of that file used by the -source file.
-build_flags:
(reflect mode only) Flags passed verbatim to go build.
-mock_names:
A list of custom names for generated mocks. This is specified as a comma-separated list of elements of the form Repository=MockSensorRepository,Endpoint=MockSensorEndpoint, where Repository is the interface name and MockSensorRepository is the desired mock name (mock factory method and mock recorder will be named after the mock). If one of the interfaces has no custom name specified, then default naming convention will be used.
-copyright_file: Copyright file used to add copyright header to the resulting source code.Go语言中接口组合(接口中包含接口)
在Go语言中,可以在接口A中组合其它的一个或多个接口(如接口B、C),这种方式等价于在接口A中添加接口B、C中声明的方法。
//接口中可以组合其它接口,这种方式等效于在接口中添加其它接口的方法
type Reader interface {
read()
}
type Writer interface {
write()
}
//定义上述两个接口的实现类
type MyReadWrite struct{}
func (mrw *MyReadWrite) read() {
fmt.Println("MyReadWrite...read")
}
func (mrw *MyReadWrite) write() {
fmt.Println("MyReadWrite...write")
}
//定义一个接口,组合了上述两个接口
type ReadWriter interface {
Reader
Writer
}
//上述接口等价于:
type ReadWriterV2 interface {
read()
write()
}
//ReadWriter和ReadWriterV2两个接口是等效的,因此可以相互赋值
func interfaceTest0104() {
mrw := &MyReadWrite{}
//mrw对象实现了read()方法和write()方法,因此可以赋值给ReadWriter和ReadWriterV2
var rw1 ReadWriter = mrw
rw1.read()
rw1.write()
fmt.Println("------")
var rw2 ReadWriterV2 = mrw
rw2.read()
rw2.write()
//同时,ReadWriter和ReadWriterV2两个接口对象可以相互赋值
rw1 = rw2
rw2 = rw1
}参考资料
边栏推荐
- 关于企业数字化的展望(38/100)
- Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
- mime. Type file content
- 华为伙伴暨开发者大会2022开源时刻全纪录
- Leetcode 821. Minimum distance of characters (simple) - sequel
- Shuttle hides the return button of the AppBar
- SQL必需掌握的100个重要知识点:组合 WHERE 子句
- Codeforces Round #723 (Div. 2)
- Original translation | comparison of machine learning model service tools: kserve, Seldon core and bentoml
- 农产品期货怎么做怎么开户,期货开户手续费多少,找谁能优惠手续费?
猜你喜欢

Can Oracle's CTAs bring constraints and other attributes to the new table?

于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日

GFS distributed file system

行业案例|从零售之王看银行数字化转型的运营之道

GoLand永久激活

SQL server for circular usage

麒麟V10安装字体

100 important knowledge points that SQL must master: sorting and retrieving data

Oracle的CTAS能不能将约束等属性带到新表?

GFS分布式文件系统
随机推荐
SQL必需掌握的100个重要知识点:IN 操作符
This is the same as data collection. Can you define a parameter as last month or the previous day, and then use this parameter in SQL?
Codeforces Round #719 (Div. 3)
系统自带的karsonzhang/fastadmin-addons报错
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
JPA踩坑系列之save方法
KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"
Pycharm common functions - breakpoint debugging
Shuttle hides the return button of the AppBar
At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
SQL必需掌握的100个重要知识点:用通配符进行过滤
让马化腾失望了!Web3.0,毫无希望
100 important knowledge points that SQL must master: filtering data
安装gatewayworker之后启动start.php
动物养殖生产虚拟仿真教学系统|华锐互动
A set of system to reduce 10 times the traffic pressure in crowded areas
shell脚本控制服务的启动和关闭 - 具备详细案例
大促场景下,如何做好网关高可用防护
非常全面的DolphinScheduler(海豚调度)安装使用文档
"Good voice" has been singing for 10 years. How can the Chinese language in the starry sky sing well in HKEx?