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

Mockgen: Support generating mock for interfaces with generics function params #649

Open
DanielSolomon opened this issue May 29, 2022 · 2 comments
Milestone

Comments

@DanielSolomon
Copy link

In case I use a concrete type:

type Pagination struct {
	Limit int64
	Skip  int64
}

//go:generate mockgen -source=$GOFILE -destination=mocks/autogen.$GOFILE -package=mocks
type IMongoDbClient interface {
	GetMany(ctx context.Context, collectionName string, filter bson.M, pagination optional.Optional[Pagination]) error
}

Optional is defined in another package as:

type Optional[T any] struct {
	value   T
	present bool
}

When running go generate ./..., the following error is raised:

2022/05/19 10:01:58 Loading input failed: failed parsing source file api.go: api.go:43:97: missing ',' in parameter list
lib/go/mongo/client/api.go:41: running "mockgen": exit status 1

Also tried using the workaround (mentioned in #621):

type OptionalPagination = optional.Optional[Pagination]

Result in:

Loading input failed: failed parsing source file api.go: api.go:41:44: expected ';', found '['
lib/go/mongo/client/api.go:43: running "mockgen": exit status 1

Originally posted by @DanielSolomon in #621 (comment)

@codyoss codyoss added this to the v1.7.0 milestone Jul 8, 2022
@daolis
Copy link

daolis commented Sep 15, 2022

Generic type is generated e.g. like this..

(*runtime.Poller[github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage.AccountsClientCreateResponse], error)

So in this case manually save the files console output to the file which should contain the generated code and removing the github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/ part works as a workaround... (of course when adding the file to git) :-P

@danlaine
Copy link

danlaine commented Nov 29, 2022

+1 for fixing this please 🙏

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants