Skip to content

Commit

Permalink
Merge pull request #750 from DustinJSilk/replace-generic
Browse files Browse the repository at this point in the history
Add ability to replace type constraints
  • Loading branch information
LandonTClipp committed Feb 16, 2024
2 parents f0789fb + 601d297 commit 8b86cf2
Show file tree
Hide file tree
Showing 12 changed files with 453 additions and 8 deletions.
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.String
# 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
57 changes: 57 additions & 0 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,63 @@ 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]=github.com/vektra/mockery/v2/baz.Baz
```

For example:

```go
type InternalBaz[T any] struct{}

func (*InternalBaz[T]) Foo() T {}

// Becomes
type InternalBaz[T baz.Baz] struct{}

func (*InternalBaz[T]) Foo() T {}
```

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]=github.com/vektra/mockery/v2/baz.Baz
```

For example:

```go
type InternalBaz[T any] struct{}

func (*InternalBaz[T]) Foo() T {}

// Becomes
type InternalBaz struct{}

func (*InternalBaz) Foo() 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]=github.com/vektra/mockery/v2/baz.*Baz
```

For example:

```go
type InternalBaz[T any] struct{}

func (*InternalBaz[T]) Foo() T {}

// Becomes
type InternalBaz struct{}

func (*InternalBaz) Foo() *baz.Baz {}
```

`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.

4 changes: 4 additions & 0 deletions pkg/fixtures/constraints/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ type Signed interface {
type Integer interface {
~int
}

type String interface {
~string
}
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
}

0 comments on commit 8b86cf2

Please sign in to comment.