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

Commit b9b5dc7

Browse files
tra4lesssywhang
andauthored
support type-safe return values (#11)
migrate from #630 more: #622 #427 #634 #657 --------- Co-authored-by: Sung Yoon Whang <[email protected]>
1 parent c3c5278 commit b9b5dc7

File tree

14 files changed

+1573
-18
lines changed

14 files changed

+1573
-18
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ It supports the following flags:
113113

114114
- `-write_package_comment`: Writes package documentation comment (godoc) if true. (default true)
115115

116+
- `-typed`: Generate Type-safe 'Return', 'Do', 'DoAndReturn' function. (default false)
117+
116118
For an example of the use of `mockgen`, see the `sample/` directory. In simple
117119
cases, you will need only the `-source` flag.
118120

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package typed
2+
3+
//go:generate mockgen -typed -aux_files faux=faux/faux.go -destination bugreport_mock.go -package typed -source=bugreport.go Example
4+
5+
import (
6+
"log"
7+
8+
"go.uber.org/mock/mockgen/internal/tests/typed/faux"
9+
)
10+
11+
// Source is an interface w/ an embedded foreign interface
12+
type Source interface {
13+
faux.Foreign
14+
}
15+
16+
func CallForeignMethod(s Source) {
17+
log.Println(s.Method())
18+
}

mockgen/internal/tests/typed/bugreport_mock.go

Lines changed: 111 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package typed
2+
3+
import (
4+
"testing"
5+
6+
"go.uber.org/mock/gomock"
7+
)
8+
9+
// TestValidInterface assesses whether or not the generated mock is valid
10+
func TestValidInterface(t *testing.T) {
11+
ctrl := gomock.NewController(t)
12+
defer ctrl.Finish()
13+
14+
s := NewMockSource(ctrl)
15+
s.EXPECT().Method().Return("")
16+
17+
CallForeignMethod(s)
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package typed
2+
3+
import (
4+
"go.uber.org/mock/mockgen/internal/tests/typed/other"
5+
"golang.org/x/exp/constraints"
6+
)
7+
8+
//go:generate mockgen --source=external.go --destination=source/mock_external_test.go --package source -typed
9+
10+
type ExternalConstraint[I constraints.Integer, F constraints.Float] interface {
11+
One(string) string
12+
Two(I) string
13+
Three(I) F
14+
Four(I) Foo[I, F]
15+
Five(I) Baz[F]
16+
Six(I) *Baz[F]
17+
Seven(I) other.One[I]
18+
Eight(F) other.Two[I, F]
19+
Nine(Iface[I])
20+
Ten(*I)
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package faux
2+
3+
type Foreign interface {
4+
Method() Return
5+
Embedded
6+
error
7+
}
8+
9+
type Embedded interface{}
10+
11+
type Return interface{}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package typed
2+
3+
import "go.uber.org/mock/mockgen/internal/tests/typed/other"
4+
5+
//go:generate mockgen --source=generics.go --destination=source/mock_generics_test.go --package source -typed
6+
////go:generate mockgen --destination=reflect/mock_test.go --package reflect . Bar,Bar2
7+
8+
type Bar[T any, R any] interface {
9+
One(string) string
10+
Two(T) string
11+
Three(T) R
12+
Four(T) Foo[T, R]
13+
Five(T) Baz[T]
14+
Six(T) *Baz[T]
15+
Seven(T) other.One[T]
16+
Eight(T) other.Two[T, R]
17+
Nine(Iface[T])
18+
Ten(*T)
19+
Eleven() (*other.One[T], error)
20+
Twelve() (*other.Two[T, R], error)
21+
Thirteen() (Baz[StructType], error)
22+
Fourteen() (*Foo[StructType, StructType2], error)
23+
Fifteen() (Iface[StructType], error)
24+
Sixteen() (Baz[other.Three], error)
25+
Seventeen() (*Foo[other.Three, other.Four], error)
26+
Eighteen() (Iface[*other.Five], error)
27+
Nineteen() AliasType
28+
}
29+
30+
type Foo[T any, R any] struct{}
31+
32+
type Baz[T any] struct{}
33+
34+
type Iface[T any] interface{}
35+
36+
type StructType struct{}
37+
38+
type StructType2 struct{}
39+
40+
type AliasType Baz[other.Three]

mockgen/internal/tests/typed/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module go.uber.org/mock/mockgen/internal/tests/typed
2+
3+
go 1.18
4+
5+
replace go.uber.org/mock => ../../../..
6+
7+
require (
8+
go.uber.org/mock v0.0.0-00010101000000-000000000000
9+
golang.org/x/exp v0.0.0-20220609121020-a51bd0440498
10+
)

mockgen/internal/tests/typed/go.sum

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
2+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
3+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
4+
golang.org/x/exp v0.0.0-20220609121020-a51bd0440498 h1:TF0FvLUGEq/8wOt/9AV1nj6D4ViZGUIGCMQfCv7VRXY=
5+
golang.org/x/exp v0.0.0-20220609121020-a51bd0440498/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys=
6+
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
7+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
8+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
9+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
10+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
11+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
12+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
13+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
14+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
15+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
16+
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
17+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
18+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
19+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
20+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
21+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
22+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
23+
golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
24+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
25+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
26+
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package other
2+
3+
type One[T any] struct{}
4+
5+
type Two[T any, R any] struct{}
6+
7+
type Three struct{}
8+
9+
type Four struct{}
10+
11+
type Five interface{}

0 commit comments

Comments
 (0)