• Home
  • Raw
  • Download

Lines Matching full:g

72 func (g *grpc) Name() string {
85 func (g *grpc) Init(gen *generator.Generator) {
86 g.gen = gen
91 func (g *grpc) objectNamed(name string) generator.Object {
92 g.gen.RecordTypeUse(name)
93 return g.gen.ObjectNamed(name)
97 func (g *grpc) typeName(str string) string {
98 return g.gen.TypeName(g.objectNamed(str))
101 // P forwards to g.gen.P.
102 func (g *grpc) P(args ...interface{}) { g.gen.P(args...) }
105 func (g *grpc) Generate(file *generator.FileDescriptor) {
110 contextPkg = string(g.gen.AddImport(contextPkgPath))
111 grpcPkg = string(g.gen.AddImport(grpcPkgPath))
113 g.P("// Reference imports to suppress errors if they are not otherwise used.")
114 g.P("var _ ", contextPkg, ".Context")
115 g.P("var _ ", grpcPkg, ".ClientConnInterface")
116 g.P()
119 g.P("// This is a compile-time assertion to ensure that this generated file")
120 g.P("// is compatible with the grpc package it is being compiled against.")
121 g.P("const _ = ", grpcPkg, ".SupportPackageIsVersion", generatedCodeVersion)
122 g.P()
125 g.generateService(file, service, i)
130 func (g *grpc) GenerateImports(file *generator.FileDescriptor) {
145 func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.ServiceDescriptorProto, …
156 g.P()
157 g.P(fmt.Sprintf(`// %sClient is the client API for %s service.
163 g.P("//")
164 g.P(deprecationComment)
166 g.P("type ", servName, "Client interface {")
168 g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service.
170 g.P("//")
171 g.P(deprecationComment)
173 g.P(g.generateClientSignature(servName, method))
175 g.P("}")
176 g.P()
179 g.P("type ", unexport(servName), "Client struct {")
180 g.P("cc ", grpcPkg, ".ClientConnInterface")
181 g.P("}")
182 g.P()
186 g.P(deprecationComment)
188 g.P("func New", servName, "Client (cc ", grpcPkg, ".ClientConnInterface) ", servName, "Client {")
189 g.P("return &", unexport(servName), "Client{cc}")
190 g.P("}")
191 g.P()
207 g.generateClientMethod(servName, fullServName, serviceDescVar, method, descExpr)
212 g.P("// ", serverType, " is the server API for ", servName, " service.")
214 g.P("//")
215 g.P(deprecationComment)
217 g.P("type ", serverType, " interface {")
219 g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service.
221 g.P("//")
222 g.P(deprecationComment)
224 g.P(g.generateServerSignature(servName, method))
226 g.P("}")
227 g.P()
231 g.P(deprecationComment)
233 g.generateUnimplementedServer(servName, service)
237 g.P(deprecationComment)
239 g.P("func Register", servName, "Server(s *", grpcPkg, ".Server, srv ", serverType, ") {")
240 g.P("s.RegisterService(&", serviceDescVar, `, srv)`)
241 g.P("}")
242 g.P()
247 hname := g.generateServerMethod(servName, fullServName, method)
252 g.P("var ", serviceDescVar, " = ", grpcPkg, ".ServiceDesc {")
253 g.P("ServiceName: ", strconv.Quote(fullServName), ",")
254 g.P("HandlerType: (*", serverType, ")(nil),")
255 g.P("Methods: []", grpcPkg, ".MethodDesc{")
260 g.P("{")
261 g.P("MethodName: ", strconv.Quote(method.GetName()), ",")
262 g.P("Handler: ", handlerNames[i], ",")
263 g.P("},")
265 g.P("},")
266 g.P("Streams: []", grpcPkg, ".StreamDesc{")
271 g.P("{")
272 g.P("StreamName: ", strconv.Quote(method.GetName()), ",")
273 g.P("Handler: ", handlerNames[i], ",")
275 g.P("ServerStreams: true,")
278 g.P("ClientStreams: true,")
280 g.P("},")
282 g.P("},")
283 g.P("Metadata: \"", file.GetName(), "\",")
284 g.P("}")
285 g.P()
289 func (g *grpc) generateUnimplementedServer(servName string, service *pb.ServiceDescriptorProto) {
291 g.P("// Unimplemented", serverType, " can be embedded to have forward compatible implementations.")
292 g.P("type Unimplemented", serverType, " struct {")
293 g.P("}")
294 g.P()
297 g.generateServerMethodConcrete(servName, method)
299 g.P()
303 func (g *grpc) generateServerMethodConcrete(servName string, method *pb.MethodDescriptorProto) {
304 header := g.generateServerSignatureWithParamNames(servName, method)
305 g.P("func (*Unimplemented", servName, "Server) ", header, " {")
311 statusPkg := string(g.gen.AddImport(statusPkgPath))
312 codePkg := string(g.gen.AddImport(codePkgPath))
313g.P("return ", nilArg, statusPkg, `.Errorf(`, codePkg, `.Unimplemented, "method `, methName, ` not…
314 g.P("}")
318 func (g *grpc) generateClientSignature(servName string, method *pb.MethodDescriptorProto) string {
324 reqArg := ", in *" + g.typeName(method.GetInputType())
328 respName := "*" + g.typeName(method.GetOutputType())
335 func (g *grpc) generateClientMethod(servName, fullServName, serviceDescVar string, method *pb.Metho…
338 inType := g.typeName(method.GetInputType())
339 outType := g.typeName(method.GetOutputType())
342 g.P(deprecationComment)
344 g.P("func (c *", unexport(servName), "Client) ", g.generateClientSignature(servName, method), "{")
346 g.P("out := new(", outType, ")")
348 g.P(`err := c.cc.Invoke(ctx, "`, sname, `", in, out, opts...)`)
349 g.P("if err != nil { return nil, err }")
350 g.P("return out, nil")
351 g.P("}")
352 g.P()
356 g.P("stream, err := c.cc.NewStream(ctx, ", descExpr, `, "`, sname, `", opts...)`)
357 g.P("if err != nil { return nil, err }")
358 g.P("x := &", streamType, "{stream}")
360 g.P("if err := x.ClientStream.SendMsg(in); err != nil { return nil, err }")
361 g.P("if err := x.ClientStream.CloseSend(); err != nil { return nil, err }")
363 g.P("return x, nil")
364 g.P("}")
365 g.P()
372 g.P("type ", servName, "_", methName, "Client interface {")
374 g.P("Send(*", inType, ") error")
377 g.P("Recv() (*", outType, ", error)")
380 g.P("CloseAndRecv() (*", outType, ", error)")
382 g.P(grpcPkg, ".ClientStream")
383 g.P("}")
384 g.P()
386 g.P("type ", streamType, " struct {")
387 g.P(grpcPkg, ".ClientStream")
388 g.P("}")
389 g.P()
392 g.P("func (x *", streamType, ") Send(m *", inType, ") error {")
393 g.P("return x.ClientStream.SendMsg(m)")
394 g.P("}")
395 g.P()
398 g.P("func (x *", streamType, ") Recv() (*", outType, ", error) {")
399 g.P("m := new(", outType, ")")
400 g.P("if err := x.ClientStream.RecvMsg(m); err != nil { return nil, err }")
401 g.P("return m, nil")
402 g.P("}")
403 g.P()
406 g.P("func (x *", streamType, ") CloseAndRecv() (*", outType, ", error) {")
407 g.P("if err := x.ClientStream.CloseSend(); err != nil { return nil, err }")
408 g.P("m := new(", outType, ")")
409 g.P("if err := x.ClientStream.RecvMsg(m); err != nil { return nil, err }")
410 g.P("return m, nil")
411 g.P("}")
412 g.P()
417 func (g *grpc) generateServerSignatureWithParamNames(servName string, method *pb.MethodDescriptorPr…
428 ret = "(*" + g.typeName(method.GetOutputType()) + ", error)"
431 reqArgs = append(reqArgs, "req *"+g.typeName(method.GetInputType()))
441 func (g *grpc) generateServerSignature(servName string, method *pb.MethodDescriptorProto) string {
452 ret = "(*" + g.typeName(method.GetOutputType()) + ", error)"
455 reqArgs = append(reqArgs, "*"+g.typeName(method.GetInputType()))
464 func (g *grpc) generateServerMethod(servName, fullServName string, method *pb.MethodDescriptorProto…
467 inType := g.typeName(method.GetInputType())
468 outType := g.typeName(method.GetOutputType())
471g.P("func ", hname, "(srv interface{}, ctx ", contextPkg, ".Context, dec func(interface{}) error, …
472 g.P("in := new(", inType, ")")
473 g.P("if err := dec(in); err != nil { return nil, err }")
474 g.P("if interceptor == nil { return srv.(", servName, "Server).", methName, "(ctx, in) }")
475 g.P("info := &", grpcPkg, ".UnaryServerInfo{")
476 g.P("Server: srv,")
477 g.P("FullMethod: ", strconv.Quote(fmt.Sprintf("/%s/%s", fullServName, methName)), ",")
478 g.P("}")
479 g.P("handler := func(ctx ", contextPkg, ".Context, req interface{}) (interface{}, error) {")
480 g.P("return srv.(", servName, "Server).", methName, "(ctx, req.(*", inType, "))")
481 g.P("}")
482 g.P("return interceptor(ctx, in, info, handler)")
483 g.P("}")
484 g.P()
488 g.P("func ", hname, "(srv interface{}, stream ", grpcPkg, ".ServerStream) error {")
490 g.P("m := new(", inType, ")")
491 g.P("if err := stream.RecvMsg(m); err != nil { return err }")
492 g.P("return srv.(", servName, "Server).", methName, "(m, &", streamType, "{stream})")
494 g.P("return srv.(", servName, "Server).", methName, "(&", streamType, "{stream})")
496 g.P("}")
497 g.P()
504 g.P("type ", servName, "_", methName, "Server interface {")
506 g.P("Send(*", outType, ") error")
509 g.P("SendAndClose(*", outType, ") error")
512 g.P("Recv() (*", inType, ", error)")
514 g.P(grpcPkg, ".ServerStream")
515 g.P("}")
516 g.P()
518 g.P("type ", streamType, " struct {")
519 g.P(grpcPkg, ".ServerStream")
520 g.P("}")
521 g.P()
524 g.P("func (x *", streamType, ") Send(m *", outType, ") error {")
525 g.P("return x.ServerStream.SendMsg(m)")
526 g.P("}")
527 g.P()
530 g.P("func (x *", streamType, ") SendAndClose(m *", outType, ") error {")
531 g.P("return x.ServerStream.SendMsg(m)")
532 g.P("}")
533 g.P()
536 g.P("func (x *", streamType, ") Recv() (*", inType, ", error) {")
537 g.P("m := new(", inType, ")")
538 g.P("if err := x.ServerStream.RecvMsg(m); err != nil { return nil, err }")
539 g.P("return m, nil")
540 g.P("}")
541 g.P()