golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.25k stars 608 forks source link

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

Open DanielSolomon opened 2 years ago

DanielSolomon commented 2 years ago

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 https://github.com/golang/mock/issues/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 https://github.com/golang/mock/issues/621#issuecomment-1131310245

daolis commented 1 year ago

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 commented 1 year ago

+1 for fixing this please :pray: