gomock mockgen : unknown embedded interface

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 1

https://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.go

Error result:

Loading input failed: test2.go:4:2: unknown embedded interface A

Embedded 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())
        }

https://pkg.go.dev/github.com/golang/mock/mockgen/internal/tests/aux_imports_embedded_interface#section-readme

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.go
type 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 files

mockgen 指令参数说明

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

参考资料

https://stackoverflow.com/questions/55999405/how-can-i-mock-specific-embedded-method-inside-interface

https://pkg.go.dev/github.com/golang/mock/gomock

https://github.com/golang/mock#running-mockgen

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,125评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,293评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,054评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,077评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,096评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,062评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,988评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,817评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,266评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,486评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,646评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,375评论 5 342
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,974评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,621评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,642评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,538评论 2 352

推荐阅读更多精彩内容