Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to replace type constraints #750

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ packages:
config:
with-expecter: True
unroll-variadic: False
# Replace generic params with a new constraint and a new fixed value
ReplaceGeneric:
config:
replace-type:
- github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGeneric[-TImport]=github.com/vektra/mockery/v2/pkg/fixtures/redefined_type_b.B
- github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGeneric[TConstraint]=github.com/vektra/mockery/v2/pkg/fixtures/constraints.Integer
# Replace a generic param with the parent type
ReplaceGenericSelf:
config:
replace-type:
- github.com/vektra/mockery/v2/pkg/fixtures.ReplaceGenericSelf[-T]=github.com/vektra/mockery/v2/pkg/fixtures.*ReplaceGenericSelf
github.com/vektra/mockery/v2/pkg/fixtures/recursive_generation:
config:
recursive: True
Expand Down
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tasks:
- "**/*.go"
cmds:
- go fmt ./...

mocks:
desc: generate new mocks from scratch
deps: [mocks.remove, mocks.generate]
Expand Down
18 changes: 18 additions & 0 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ func (_m *Handler) HandleMessage(m pubsub.Message) error {
}
```

Generic type constraints can also be replaced by targeting the changed parameter with the square bracket notation on the left hand side.

```shell
mockery --replace-type github.com/vektra/mockery/v2/baz/internal/foo.InternalBaz[T]=baz:github.com/vektra/mockery/v2/baz.Baz
```

If a type constraint needs to be removed and replaced with a type, target the constraint with square brackets and include a '-' in front to have it removed.

```shell
mockery --replace-type github.com/vektra/mockery/v2/baz/internal/foo.InternalBaz[-T]=baz:github.com/vektra/mockery/v2/baz.Baz
```

When replacing a generic constraint, you can replace the type with a pointer by adding a '*' before the output type name.

```shell
mockery --replace-type github.com/vektra/mockery/v2/baz/internal/foo.InternalBaz[-T]=baz:github.com/vektra/mockery/v2/baz.*Baz
```

DustinJSilk marked this conversation as resolved.
Show resolved Hide resolved
`packages` configuration
------------------------
:octicons-tag-24: v2.21.0
Expand Down
173 changes: 173 additions & 0 deletions mocks/github.com/vektra/mockery/v2/pkg/fixtures/ReplaceGeneric.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions mocks/github.com/vektra/mockery/v2/pkg/fixtures/Variadic.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions pkg/fixtures/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,17 @@ type GetInt interface{ Get() int }
type GetGeneric[T constraints.Integer] interface{ Get() T }

type EmbeddedGet[T constraints.Signed] interface{ GetGeneric[T] }

type ReplaceGeneric[
TImport any,
TConstraint constraints.Signed,
TKeep any,
] interface {
A(t1 TImport) TKeep
B() TImport
C() TConstraint
}

type ReplaceGenericSelf[T any] interface {
A() T
}