1
- // Copyright 2015 The Go Authors. All rights reserved.
1
+ // Copyright 2015 The Go Authors. All rights reserved.
2
2
// Use of this source code is governed by a BSD-style
3
3
// license that can be found in the LICENSE file.
4
4
@@ -23,13 +23,15 @@ import (
23
23
// It is incremented whenever an incompatibility between the generated code and
24
24
// the grpc package is introduced; the generated code references
25
25
// a constant, grpc.SupportPackageIsVersionN (where N is generatedCodeVersion).
26
- const generatedCodeVersion = 4
26
+ const generatedCodeVersion = 6
27
27
28
28
// Paths for packages used by code generated in this file,
29
29
// relative to the import_prefix of the generator.Generator.
30
30
const (
31
31
contextPkgPath = "context"
32
32
grpcPkgPath = "google.golang.org/grpc"
33
+ codePkgPath = "google.golang.org/grpc/codes"
34
+ statusPkgPath = "google.golang.org/grpc/status"
33
35
)
34
36
35
37
func init () {
@@ -86,7 +88,7 @@ func (g *grpc) Generate(file *generator.FileDescriptor) {
86
88
87
89
g .P ("// Reference imports to suppress errors if they are not otherwise used." )
88
90
g .P ("var _ " , contextPkg , ".Context" )
89
- g .P ("var _ " , grpcPkg , ".ClientConn " )
91
+ g .P ("var _ " , grpcPkg , ".ClientConnInterface " )
90
92
g .P ()
91
93
92
94
// Assert version compatibility.
@@ -140,22 +142,26 @@ func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.Servi
140
142
g .P ("type " , servName , "Client interface {" )
141
143
for i , method := range service .Method {
142
144
g .gen .PrintComments (fmt .Sprintf ("%s,2,%d" , path , i )) // 2 means method in a service.
145
+ if method .GetOptions ().GetDeprecated () {
146
+ g .P ("//" )
147
+ g .P (deprecationComment )
148
+ }
143
149
g .P (g .generateClientSignature (servName , method ))
144
150
}
145
151
g .P ("}" )
146
152
g .P ()
147
153
148
154
// Client structure.
149
155
g .P ("type " , unexport (servName ), "Client struct {" )
150
- g .P ("cc * " , grpcPkg , ".ClientConn " )
156
+ g .P ("cc " , grpcPkg , ".ClientConnInterface " )
151
157
g .P ("}" )
152
158
g .P ()
153
159
154
160
// NewClient factory.
155
161
if deprecated {
156
162
g .P (deprecationComment )
157
163
}
158
- g .P ("func New" , servName , "Client (cc * " , grpcPkg , ".ClientConn ) " , servName , "Client {" )
164
+ g .P ("func New" , servName , "Client (cc " , grpcPkg , ".ClientConnInterface ) " , servName , "Client {" )
159
165
g .P ("return &" , unexport (servName ), "Client{cc}" )
160
166
g .P ("}" )
161
167
g .P ()
@@ -187,11 +193,21 @@ func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.Servi
187
193
g .P ("type " , serverType , " interface {" )
188
194
for i , method := range service .Method {
189
195
g .gen .PrintComments (fmt .Sprintf ("%s,2,%d" , path , i )) // 2 means method in a service.
196
+ if method .GetOptions ().GetDeprecated () {
197
+ g .P ("//" )
198
+ g .P (deprecationComment )
199
+ }
190
200
g .P (g .generateServerSignature (servName , method ))
191
201
}
192
202
g .P ("}" )
193
203
g .P ()
194
204
205
+ // Server Unimplemented struct for forward compatibility.
206
+ if deprecated {
207
+ g .P (deprecationComment )
208
+ }
209
+ g .generateUnimplementedServer (servName , service )
210
+
195
211
// Server registration.
196
212
if deprecated {
197
213
g .P (deprecationComment )
@@ -245,6 +261,35 @@ func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.Servi
245
261
g .P ()
246
262
}
247
263
264
+ // generateUnimplementedServer creates the unimplemented server struct
265
+ func (g * grpc ) generateUnimplementedServer (servName string , service * pb.ServiceDescriptorProto ) {
266
+ serverType := servName + "Server"
267
+ g .P ("// Unimplemented" , serverType , " can be embedded to have forward compatible implementations." )
268
+ g .P ("type Unimplemented" , serverType , " struct {" )
269
+ g .P ("}" )
270
+ g .P ()
271
+ // Unimplemented<service_name>Server's concrete methods
272
+ for _ , method := range service .Method {
273
+ g .generateServerMethodConcrete (servName , method )
274
+ }
275
+ g .P ()
276
+ }
277
+
278
+ // generateServerMethodConcrete returns unimplemented methods which ensure forward compatibility
279
+ func (g * grpc ) generateServerMethodConcrete (servName string , method * pb.MethodDescriptorProto ) {
280
+ header := g .generateServerSignatureWithParamNames (servName , method )
281
+ g .P ("func (*Unimplemented" , servName , "Server) " , header , " {" )
282
+ var nilArg string
283
+ if ! method .GetServerStreaming () && ! method .GetClientStreaming () {
284
+ nilArg = "nil, "
285
+ }
286
+ methName := generator .CamelCase (method .GetName ())
287
+ statusPkg := string (g .gen .AddImport (statusPkgPath ))
288
+ codePkg := string (g .gen .AddImport (codePkgPath ))
289
+ g .P ("return " , nilArg , statusPkg , `.Errorf(` , codePkg , `.Unimplemented, "method ` , methName , ` not implemented")` )
290
+ g .P ("}" )
291
+ }
292
+
248
293
// generateClientSignature returns the client-side signature for a method.
249
294
func (g * grpc ) generateClientSignature (servName string , method * pb.MethodDescriptorProto ) string {
250
295
origMethName := method .GetName ()
@@ -344,6 +389,30 @@ func (g *grpc) generateClientMethod(servName, fullServName, serviceDescVar strin
344
389
}
345
390
}
346
391
392
+ // generateServerSignatureWithParamNames returns the server-side signature for a method with parameter names.
393
+ func (g * grpc ) generateServerSignatureWithParamNames (servName string , method * pb.MethodDescriptorProto ) string {
394
+ origMethName := method .GetName ()
395
+ methName := generator .CamelCase (origMethName )
396
+ if reservedClientName [methName ] {
397
+ methName += "_"
398
+ }
399
+
400
+ var reqArgs []string
401
+ ret := "error"
402
+ if ! method .GetServerStreaming () && ! method .GetClientStreaming () {
403
+ reqArgs = append (reqArgs , "ctx " + contextPkg + ".Context" )
404
+ ret = "(*" + g .typeName (method .GetOutputType ()) + ", error)"
405
+ }
406
+ if ! method .GetClientStreaming () {
407
+ reqArgs = append (reqArgs , "req *" + g .typeName (method .GetInputType ()))
408
+ }
409
+ if method .GetServerStreaming () || method .GetClientStreaming () {
410
+ reqArgs = append (reqArgs , "srv " + servName + "_" + generator .CamelCase (origMethName )+ "Server" )
411
+ }
412
+
413
+ return methName + "(" + strings .Join (reqArgs , ", " ) + ") " + ret
414
+ }
415
+
347
416
// generateServerSignature returns the server-side signature for a method.
348
417
func (g * grpc ) generateServerSignature (servName string , method * pb.MethodDescriptorProto ) string {
349
418
origMethName := method .GetName ()
0 commit comments