Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Generate mocks from archives/*types.Type data #424

@robbertvanginkel

Description

@robbertvanginkel

Requested feature
Mockgen supports creating github.com/golang/mock/mockgen/model.Interface models from reflect.Type data using

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.Type to model.Interface. This is pretty much analogous to whats implemented in func InterfaceFromInterfaceType(it reflect.Type) (*model.Interface, error) on https://github.com/golang/mock/blob/master/mockgen/model/model.go#L293-L470. reflect.Type and types.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 converting types.Type to model.Interface in golang/mock?
  • golang/mock doesn't have any go api to take a 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 the types.Package into a model.Package and similar to a reflect binary encodes the model.Package with encoding/gob to a file for mockgen to read. In addition to its existing cli api for doing mockgen on a gob encoded model.Package, would it be possible to add a public go api to do codegen based on a model.Package object?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions