-
Notifications
You must be signed in to change notification settings - Fork 606
Generate mocks from archives/*types.Type data #424
Description
Requested feature
Mockgen supports creating github.com/golang/mock/mockgen/model.Interface models from reflect.Type data using
Line 295 in 6d816de
| func InterfaceFromInterfaceType(it reflect.Type) (*Interface, error) { |
But there is no analogue for creating model.Interface's for mock generation from go/types.Type data.
Why the feature is needed
The go compiler writes serialized type information for the public interface of each package, which can be read using golang.org/x/tools/go/gcexportdata to get a types.Package.
This blog post gives a good indication of what the export data is good for: https://jayconrod.com/posts/112/export-data--the-secret-of-go-s-fast-builds.
We implemented a utility tool that takes an archive (that was generated by a previous build step from bazel), reads the type data, converts it to a gomock model.Interface and uses it for codegen. Using this instead of source/reflect mode presented significant efficiency and speed win for us over using gomock in reflect mode.
(Optional) Proposed solution
Although the internal tool works fine for us, there's some rough edges and some opportunity to share some of our benefits with the community. There's two things I have in mind for improvement:
- We maintain separate code for converting
types.Typetomodel.Interface. This is pretty much analogous to whats implemented infunc InterfaceFromInterfaceType(it reflect.Type) (*model.Interface, error)on https://github.com/golang/mock/blob/master/mockgen/model/model.go#L293-L470.reflect.Typeandtypes.Typeare similar (archives contain all the same typedata as the binary they later get compiled into), but unfortunately don't have an overarching interface. Would there be any interest in having support for convertingtypes.Typetomodel.Interfacein golang/mock? - golang/mock doesn't have any go api to take a
model.Packageand run codegen. Our current tool uses a bit of a workaround to do codegeneration: run gomock with-exec_onlypointing to a stub binary, the stub binary reads the archive file, converts thetypes.Packageinto amodel.Packageand similar to a reflect binary encodes themodel.Packagewithencoding/gobto a file for mockgen to read. In addition to its existing cli api for doing mockgen on a gob encodedmodel.Package, would it be possible to add a public go api to do codegen based on amodel.Packageobject?