Skip to content

Release v0.5.1 breaks mockgen for typeref'd generic types #243

@shawnburke

Description

@shawnburke

Actual behavior

NOTE: I think you can skip most the below by just creating a generic type with a typeref:

type MyGenericType[Foo any, Bar any] interface {
}

type MyAliasedType = MyGenericType[string, int]

type MyService interface {

// not sure which of these forms is the problem
  MyFunc(ctx context.Context) (MyGenericType[string, int], error) 
MyOtherFunc(ctx context.Context) (MyAliasedType, error) 
}

Then mockgen MyService and try to build the resulting file

============
Given a proto like this:

[myservice.proto]


service MyService {
  rpc Dispatch (stream DispatchRequest) returns (stream DispatchMessage);  
}

message DispatchRequest {
  string id = 1;
}

message DispatchMessage {
  string msg = 1;
}

Then generating that:

apt install -y protobuf-compiler
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
export GENERATED_DIR=./proto_out
protoc -I=proto --go_out=$(GENERATED_DIR) --go-grpc_out=$(GENERATED_DIR)

It will generate Go like:

type MyServer_DispatchClient = grpc.BidiStreamingClient[DispatchRequest, DispatchMessage]

Then mockgen:

mockgen .proto_out  MyServiceClient >mocks.go

Will generate code like this:

func (m *MockMyServiceClient) Dispatch(ctx context.Context, opts ...grpc.CallOption) (mypackage.myservice_DispatchClient[mypackage.DispatchRequest, mypackage.DispatchMessage], error) {

And the problem here is that myservice_DispatchClient is not a generic type, it's type alias is a realized type based on a generic type.

Expected behavior A clear and concise description of what you expected to
happen.

Should generate code like:

func (m *MockMyServiceClient) Dispatch(ctx context.Context, opts ...grpc.CallOption) (grpc.BidiStreamingClient[axon.DispatchRequest, axon.DispatchMessage], error) {

OR better yet

func (m *MockMyServiceClient) Dispatch(ctx context.Context, opts ...grpc.CallOption) (mypackage.MyService_DispatchClient, error) {

To Reproduce Steps to reproduce the behavior

Either build out the proto above or:

  1. Clone https://github.com/cortexapps/axon
  2. Replace mockgen@v0.5.0 with mockgen@v0.5.1
  3. cd sdks/go
  4. rm -rf mock_axon && make test

Additional Information

  • gomock mode (reflect or source): source
  • gomock version or git ref: v0.5.0
  • golang version: 1.22.9

Triage Notes for the Maintainers

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions