-
Notifications
You must be signed in to change notification settings - Fork 607
Generate mocks from archives/*types.Type data #424
Comments
This is a really interesting use case. I did not knew anyone used this package directly. I will need to research this space a little more and get back to you. |
I think at the moment there isn't a strong need/use for conversion from
This seems totally reasonable to me as the existing code for consuming a |
@robbertvanginkel interestingly enough, we end up doing the same for running mockgen with bazel. I believe the only reason why mockgen is not using As an archive contains all transitive typing info, we can safely delegate all the tricky parts to It's a net gain compared to the current implementation.
|
Requested feature
Mockgen supports creating
github.com/golang/mock/mockgen/model.Interface
models fromreflect.Type
data usingmock/mockgen/model/model.go
Line 295 in 6d816de
But there is no analogue for creating
model.Interface
's for mock generation fromgo/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 atypes.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:
types.Type
tomodel.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.Type
andtypes.Type
are 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.Type
tomodel.Interface
in golang/mock?model.Package
and run codegen. Our current tool uses a bit of a workaround to do codegeneration: run gomock with-exec_only
pointing to a stub binary, the stub binary reads the archive file, converts thetypes.Package
into amodel.Package
and similar to a reflect binary encodes themodel.Package
withencoding/gob
to 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.Package
object?The text was updated successfully, but these errors were encountered: