@@ -47,61 +47,67 @@ func (s *RPCSuite) TestDeclConstructor() {
47
47
s .Equal (expectedConstructor , output )
48
48
}
49
49
50
- const expectedFuncNotGenerated = `func (s *FooServer) DoFoo(ctx context .Context, in *Foo) (result *Bar, err error) {
50
+ const expectedFuncNotGenerated = `func (s *FooServer) DoFoo(ctx xcontext .Context, in *Foo) (result *Bar, err error) {
51
51
result = new(Bar)
52
52
result = DoFoo(in)
53
53
return
54
54
}`
55
55
56
- const expectedFuncNotGeneratedAndNotNullable = `func (s *FooServer) DoFoo(ctx context.Context, in *Foo) (result *Bar, err error) {
56
+ const expectedFuncNotGeneratedCtx = `func (s *FooServer) DoFooCtx(ctx xcontext.Context, in *Foo) (result *Bar, err error) {
57
+ result = new(Bar)
58
+ result = DoFooCtx(ctx, in)
59
+ return
60
+ }`
61
+
62
+ const expectedFuncNotGeneratedAndNotNullable = `func (s *FooServer) DoFoo(ctx xcontext.Context, in *Foo) (result *Bar, err error) {
57
63
result = new(Bar)
58
64
aux := DoFoo(in)
59
65
result = &aux
60
66
return
61
67
}`
62
68
63
- const expectedFuncNotGeneratedAndNotNullableIn = `func (s *FooServer) DoFoo(ctx context .Context, in *Foo) (result *Bar, err error) {
69
+ const expectedFuncNotGeneratedAndNotNullableIn = `func (s *FooServer) DoFoo(ctx xcontext .Context, in *Foo) (result *Bar, err error) {
64
70
result = new(Bar)
65
71
result = DoFoo(*in)
66
72
return
67
73
}`
68
74
69
- const expectedFuncGenerated = `func (s *FooServer) DoFoo(ctx context .Context, in *FooRequest) (result *FooResponse, err error) {
75
+ const expectedFuncGenerated = `func (s *FooServer) DoFoo(ctx xcontext .Context, in *FooRequest) (result *FooResponse, err error) {
70
76
result = new(FooResponse)
71
77
result.Result1, result.Result2, result.Result3 = DoFoo(in.Arg1, in.Arg2, in.Arg3)
72
78
return
73
79
}`
74
80
75
- const expectedFuncGeneratedVariadic = `func (s *FooServer) DoFoo(ctx context .Context, in *FooRequest) (result *FooResponse, err error) {
81
+ const expectedFuncGeneratedVariadic = `func (s *FooServer) DoFoo(ctx xcontext .Context, in *FooRequest) (result *FooResponse, err error) {
76
82
result = new(FooResponse)
77
83
result.Result1, result.Result2, result.Result3 = DoFoo(in.Arg1, in.Arg2, in.Arg3...)
78
84
return
79
85
}`
80
86
81
- const expectedFuncGeneratedWithError = `func (s *FooServer) DoFoo(ctx context .Context, in *FooRequest) (result *FooResponse, err error) {
87
+ const expectedFuncGeneratedWithError = `func (s *FooServer) DoFoo(ctx xcontext .Context, in *FooRequest) (result *FooResponse, err error) {
82
88
result = new(FooResponse)
83
89
result.Result1, result.Result2, result.Result3, err = DoFoo(in.Arg1, in.Arg2, in.Arg3)
84
90
return
85
91
}`
86
92
87
- const expectedMethod = `func (s *FooServer) Fooer_DoFoo(ctx context .Context, in *FooRequest) (result *FooResponse, err error) {
93
+ const expectedMethod = `func (s *FooServer) Fooer_DoFoo(ctx xcontext .Context, in *FooRequest) (result *FooResponse, err error) {
88
94
result = new(FooResponse)
89
95
result.Result1, result.Result2, result.Result3, err = s.Fooer.DoFoo(in.Arg1, in.Arg2, in.Arg3)
90
96
return
91
97
}`
92
98
93
- const expectedMethodExternalInput = `func (s *FooServer) T_Foo(ctx context .Context, in *ast.BlockStmt) (result *T_FooResponse, err error) {
99
+ const expectedMethodExternalInput = `func (s *FooServer) T_Foo(ctx xcontext .Context, in *ast.BlockStmt) (result *T_FooResponse, err error) {
94
100
result = new(T_FooResponse)
95
101
_ = s.T.Foo(in)
96
102
return
97
103
}`
98
104
99
- const expectedFuncEmptyInAndOut = `func (s *FooServer) Empty(ctx context .Context, in *Empty) (result *Empty, err error) {
105
+ const expectedFuncEmptyInAndOut = `func (s *FooServer) Empty(ctx xcontext .Context, in *Empty) (result *Empty, err error) {
100
106
Empty()
101
107
return
102
108
}`
103
109
104
- const expectedFuncEmptyInAndOutWithError = `func (s *FooServer) Empty(ctx context .Context, in *Empty) (result *Empty, err error) {
110
+ const expectedFuncEmptyInAndOutWithError = `func (s *FooServer) Empty(ctx xcontext .Context, in *Empty) (result *Empty, err error) {
105
111
err = Empty()
106
112
return
107
113
}`
@@ -122,6 +128,17 @@ func (s *RPCSuite) TestDeclMethod() {
122
128
},
123
129
expectedFuncNotGenerated ,
124
130
},
131
+ {
132
+ "func not generated with ctx" ,
133
+ & protobuf.RPC {
134
+ Name : "DoFooCtx" ,
135
+ Method : "DoFooCtx" ,
136
+ HasCtx : true ,
137
+ Input : nullable (protobuf .NewNamed ("" , "Foo" )),
138
+ Output : nullable (protobuf .NewNamed ("" , "Bar" )),
139
+ },
140
+ expectedFuncNotGeneratedCtx ,
141
+ },
125
142
{
126
143
"func output not generated and not nullable" ,
127
144
& protobuf.RPC {
@@ -295,7 +312,7 @@ func (s *RPCSuite) TestDeclMethod() {
295
312
const expectedGeneratedFile = `package subpkg
296
313
297
314
import (
298
- "golang.org/x/net/context"
315
+ xcontext "golang.org/x/net/context"
299
316
)
300
317
301
318
type subpkgServiceServer struct {
@@ -304,22 +321,22 @@ type subpkgServiceServer struct {
304
321
func NewSubpkgServiceServer() *subpkgServiceServer {
305
322
return &subpkgServiceServer{}
306
323
}
307
- func (s *subpkgServiceServer) Generated(ctx context .Context, in *GeneratedRequest) (result *GeneratedResponse, err error) {
324
+ func (s *subpkgServiceServer) Generated(ctx xcontext .Context, in *GeneratedRequest) (result *GeneratedResponse, err error) {
308
325
result = new(GeneratedResponse)
309
326
result.Result1, err = Generated(in.Arg1)
310
327
return
311
328
}
312
- func (s *subpkgServiceServer) MyContainer_Name(ctx context .Context, in *MyContainer_NameRequest) (result *MyContainer_NameResponse, err error) {
329
+ func (s *subpkgServiceServer) MyContainer_Name(ctx xcontext .Context, in *MyContainer_NameRequest) (result *MyContainer_NameResponse, err error) {
313
330
result = new(MyContainer_NameResponse)
314
331
result.Result1 = s.MyContainer.Name()
315
332
return
316
333
}
317
- func (s *subpkgServiceServer) Point_GeneratedMethod(ctx context .Context, in *Point_GeneratedMethodRequest) (result *Point, err error) {
334
+ func (s *subpkgServiceServer) Point_GeneratedMethod(ctx xcontext .Context, in *Point_GeneratedMethodRequest) (result *Point, err error) {
318
335
result = new(Point)
319
336
result = s.Point.GeneratedMethod(in.Arg1)
320
337
return
321
338
}
322
- func (s *subpkgServiceServer) Point_GeneratedMethodOnPointer(ctx context .Context, in *Point_GeneratedMethodOnPointerRequest) (result *Point, err error) {
339
+ func (s *subpkgServiceServer) Point_GeneratedMethodOnPointer(ctx xcontext .Context, in *Point_GeneratedMethodOnPointerRequest) (result *Point, err error) {
323
340
result = new(Point)
324
341
result = s.Point.GeneratedMethodOnPointer(in.Arg1)
325
342
return
@@ -362,6 +379,7 @@ func TestConstructorName(t *testing.T) {
362
379
const testPkg = `package fake
363
380
364
381
import "go/ast"
382
+ import "context"
365
383
366
384
type Foo struct{}
367
385
type Bar struct {}
@@ -370,6 +388,10 @@ func DoFoo(in *Foo) *Bar {
370
388
return nil
371
389
}
372
390
391
+ func DoFooCtx(ctx context.Context, in *Foo) *Bar {
392
+ return nil
393
+ }
394
+
373
395
func MoreFoo(a int) *ast.BlockStmt {
374
396
return nil
375
397
}
0 commit comments