当前位置:网站首页>gomock mockgen : unknown embedded interface
gomock mockgen : unknown embedded interface
2022-06-27 22:01:00 【Zen and the art of computer programming】
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
Don't put the rocket_driver.go With other dependent interface Put it in the same package below ( mockgen Default assumptions for ? ) .
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{} }Source code project directory :
.
├── 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 Instruction parameter description
-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 Interface composition in language ( Interface contains interface )
stay Go In language , Can be in the interface A Combine one or more other interfaces in ( Such as interface B、C), This approach is equivalent to the interface A Add interface to B、C The method declared in .
// Other interfaces can be combined in the interface , This method is equivalent to adding methods of other interfaces to the interface
type Reader interface {
read()
}
type Writer interface {
write()
}
// Define the implementation classes of the above two interfaces
type MyReadWrite struct{}
func (mrw *MyReadWrite) read() {
fmt.Println("MyReadWrite...read")
}
func (mrw *MyReadWrite) write() {
fmt.Println("MyReadWrite...write")
}
// Define an interface , The above two interfaces are combined
type ReadWriter interface {
Reader
Writer
}
// The above interface is equivalent to :
type ReadWriterV2 interface {
read()
write()
}
//ReadWriter and ReadWriterV2 The two interfaces are equivalent , So we can assign values to each other
func interfaceTest0104() {
mrw := &MyReadWrite{}
//mrw Object implementation read() Methods and write() Method , So it can be assigned to ReadWriter and ReadWriterV2
var rw1 ReadWriter = mrw
rw1.read()
rw1.write()
fmt.Println("------")
var rw2 ReadWriterV2 = mrw
rw2.read()
rw2.write()
// meanwhile ,ReadWriter and ReadWriterV2 Two interface objects can assign values to each other
rw1 = rw2
rw2 = rw1
}Reference material
边栏推荐
- Common problems encountered by burp Suite
- 美团20k软件测试工程师的经验分享
- Method of reading file contents by Excel
- [LeetCode]508. 出现次数最多的子树元素和
- 豆沙绿保护你的双眼
- Simulink导出FMU模型文件方法
- 百万年薪独家专访,开发人员不修复bug怎么办?
- List of language weaknesses --cwe, a website worth learning
- Oracle migration MySQL unique index case insensitive don't be afraid
- Set code exercise
猜你喜欢

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

Go from introduction to actual combat - task cancellation (note)

AQS SOS AQS with me

Go from introduction to practice -- shared memory concurrency mechanism (notes)

Simulink导出FMU模型文件方法

I think I should start writing my own blog.

STM32CubeIDE1.9.0\STM32CubeMX 6.5 F429IGT6加LAN8720A,配置ETH+LWIP

The create database of gbase 8A takes a long time to query and is suspected to be stuck

Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear

win11桌面出现“了解此图片”如何删除
随机推荐
matlab查找某一行或者某一列在矩阵中的位置
Test automatique de Test logiciel - test d'interface de l'introduction à la maîtrise, apprendre un peu chaque jour
xpath
Interview question 3 of software test commonly used by large factories (with answers)
[LeetCode]100. Same tree
C language programming detailed version (learning note 1) I can't understand it after reading, and I can't help it.
Dynamic refresh mapper
Stm32f107+lan8720a use stm32subemx to configure network connection +tcp master-slave +udp app
[LeetCode]513. Find the value in the lower left corner of the tree
linux下安装oracle11g 静默安装教程
GBase 8a OLAP分析函数cume_dist的使用样例
Go from starting to Real - Interface (note)
豆沙绿保护你的双眼
Software test automation test -- interface test from entry to proficiency, learn a little every day
Simulink导出FMU模型文件方法
根据自定义excel标题模板快速excel导出
JVM memory structure when creating objects
Système de gestion - itclub (II)
Stm32cubeide1.9.0\stm32cubemx 6.5 f429igt6 plus lan8720a, configure eth+lwip
[leetcode] dynamic programming solution split integer i[silver fox]