vektra / mockery

A mock code autogenerator for Go
https://vektra.github.io/mockery/
BSD 3-Clause "New" or "Revised" License
5.8k stars 395 forks source link

Add ability to replace type constraints #750

Closed DustinJSilk closed 4 months ago

DustinJSilk commented 5 months ago

Description

Adds a config option that can replace generic types with a concrete type.

Type of change

Version of Golang used when building/testing:

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

Checklist

LandonTClipp commented 5 months ago

Excellent! I'll give it a proper review in a few days. Thanks for the contribution.

codecov[bot] commented 5 months ago

Codecov Report

Attention: 82 lines in your changes are missing coverage. Please review.

Comparison is base (3b25f39) 42.45067% compared to head (601d297) 42.65889%. Report is 4 commits behind head on master.

Files Patch % Lines
...m/vektra/mockery/v2/pkg/fixtures/ReplaceGeneric.go 53.01205% 30 Missing and 9 partials :warning:
pkg/generator.go 43.47826% 20 Missing and 6 partials :warning:
...ktra/mockery/v2/pkg/fixtures/ReplaceGenericSelf.go 60.60606% 10 Missing and 3 partials :warning:
...hub.com/vektra/mockery/v2/pkg/fixtures/Variadic.go 0.00000% 2 Missing :warning:
...ktra/mockery/v2/pkg/fixtures/VariadicReturnFunc.go 0.00000% 1 Missing and 1 partial :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #750 +/- ## =================================================== + Coverage 42.45067% 42.65889% +0.20821% =================================================== Files 61 63 +2 Lines 4815 4972 +157 =================================================== + Hits 2044 2121 +77 - Misses 2591 2653 +62 - Partials 180 198 +18 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

DustinJSilk commented 4 months ago

Hey @LandonTClipp These are the 2 new formats I'd propose for replacing generic types using the replace-type option, please let me know what you think of the 2 examples below:

Replace a type parameter: module.com/foo.Foo[T]=module.com/baz.Baz

type Foo[T any] struct {}
func (*Foo) Get() T {}
type Foo[T baz.Baz] struct {}
func (*Foo) Get() T {}

Remove a type parameter and set each instance of it: module.com/foo.Foo[-T]=module.com/baz.Baz

type Foo[T any] struct {}
func (*Foo) Get() T {}
type Foo struct {}
func (*Foo) Get() baz.Baz {}
DustinJSilk commented 4 months ago

Hi @LandonTClipp

I've updated the API to the suggestion above which I'm using successfully for my usecase and hopefully it provides more flexibility for others.

I'm happy to change the API if you have any ideas.

LandonTClipp commented 4 months ago

Hi @DustinJSilk, apologies for the lack of responses the last few weeks. I have been on the hunt for a new job and I haven't been able to devote much time or energy to this project, but luckily my search is coming to a close (hopefully, knock on wood).

As far as your proposals, I think I'm on board with it. The only thing I'm unhappy about (which has nothing to do with your proposal or implementation) is that I don't enjoy these complex, custom parameter strings. I think in hindsight, I would have preferred replace-types to simply be a list of maps with individual parameters, instead of having to parse this string here. That ship has already sailed... it's something I'll probably deprecate once a better implementation is available.

LandonTClipp commented 4 months ago

The tests are failing due to an unrelated bug that I will fix in a separate PR.

LandonTClipp commented 4 months ago

Good work @DustinJSilk, feature has been released.

DustinJSilk commented 4 months ago

Amazing thanks @LandonTClipp