@@ -178,8 +178,13 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.
178178
179179 g .P ("// This is a compile-time assertion to ensure that this generated file" )
180180 g .P ("// is compatible with the grpc package it is being compiled against." )
181- g .P ("// Requires gRPC-Go v1.64.0 or later." )
182- g .P ("const _ = " , grpcPackage .Ident ("SupportPackageIsVersion9" ))
181+ if * useGenericStreams {
182+ g .P ("// Requires gRPC-Go v1.64.0 or later." )
183+ g .P ("const _ = " , grpcPackage .Ident ("SupportPackageIsVersion9" ))
184+ } else {
185+ g .P ("// Requires gRPC-Go v1.62.0 or later." )
186+ g .P ("const _ = " , grpcPackage .Ident ("SupportPackageIsVersion8" )) // When changing, update version number above.
187+ }
183188 g .P ()
184189 for _ , service := range file .Services {
185190 genService (gen , file , g , service )
@@ -328,7 +333,11 @@ func clientSignature(g *protogen.GeneratedFile, method *protogen.Method) string
328333 if ! method .Desc .IsStreamingClient () && ! method .Desc .IsStreamingServer () {
329334 s += "*" + g .QualifiedGoIdent (method .Output .GoIdent )
330335 } else {
331- s += clientStreamInterface (g , method )
336+ if * useGenericStreams {
337+ s += clientStreamInterface (g , method )
338+ } else {
339+ s += method .Parent .GoName + "_" + method .GoName + "Client"
340+ }
332341 }
333342 s += ", error)"
334343 return s
@@ -364,8 +373,11 @@ func genClientMethod(_ *protogen.Plugin, _ *protogen.File, g *protogen.Generated
364373 return
365374 }
366375
367- typeParam := g .QualifiedGoIdent (method .Input .GoIdent ) + ", " + g .QualifiedGoIdent (method .Output .GoIdent )
368- streamImpl := g .QualifiedGoIdent (grpcPackage .Ident ("GenericClientStream" )) + "[" + typeParam + "]"
376+ streamImpl := unexport (service .GoName ) + method .GoName + "Client"
377+ if * useGenericStreams {
378+ typeParam := g .QualifiedGoIdent (method .Input .GoIdent ) + ", " + g .QualifiedGoIdent (method .Output .GoIdent )
379+ streamImpl = g .QualifiedGoIdent (grpcPackage .Ident ("GenericClientStream" )) + "[" + typeParam + "]"
380+ }
369381
370382 serviceDescVar := service .GoName + "_ServiceDesc"
371383 g .P ("stream, err := c.cc.NewStream(ctx, &" , serviceDescVar , ".Streams[" , index , `], ` , fmSymbol , `, cOpts...)` )
@@ -380,9 +392,61 @@ func genClientMethod(_ *protogen.Plugin, _ *protogen.File, g *protogen.Generated
380392 g .P ()
381393
382394 // Auxiliary types aliases, for backwards compatibility.
383- g .P ("// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name." )
384- g .P ("type " , service .GoName , "_" , method .GoName , "Client = " , clientStreamInterface (g , method ))
395+ if * useGenericStreams {
396+ g .P ("// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name." )
397+ g .P ("type " , service .GoName , "_" , method .GoName , "Client = " , clientStreamInterface (g , method ))
398+ g .P ()
399+ return
400+ }
401+
402+ // Stream auxiliary types and methods, if we're not taking advantage of the
403+ // pre-implemented generic types and their methods.
404+ genSend := method .Desc .IsStreamingClient ()
405+ genRecv := method .Desc .IsStreamingServer ()
406+ genCloseAndRecv := ! method .Desc .IsStreamingServer ()
407+
408+ g .P ("type " , service .GoName , "_" , method .GoName , "Client interface {" )
409+ if genSend {
410+ g .P ("Send(*" , method .Input .GoIdent , ") error" )
411+ }
412+ if genRecv {
413+ g .P ("Recv() (*" , method .Output .GoIdent , ", error)" )
414+ }
415+ if genCloseAndRecv {
416+ g .P ("CloseAndRecv() (*" , method .Output .GoIdent , ", error)" )
417+ }
418+ g .P (grpcPackage .Ident ("ClientStream" ))
419+ g .P ("}" )
385420 g .P ()
421+
422+ g .P ("type " , streamImpl , " struct {" )
423+ g .P (grpcPackage .Ident ("ClientStream" ))
424+ g .P ("}" )
425+ g .P ()
426+
427+ if genSend {
428+ g .P ("func (x *" , streamImpl , ") Send(m *" , method .Input .GoIdent , ") error {" )
429+ g .P ("return x.ClientStream.SendMsg(m)" )
430+ g .P ("}" )
431+ g .P ()
432+ }
433+ if genRecv {
434+ g .P ("func (x *" , streamImpl , ") Recv() (*" , method .Output .GoIdent , ", error) {" )
435+ g .P ("m := new(" , method .Output .GoIdent , ")" )
436+ g .P ("if err := x.ClientStream.RecvMsg(m); err != nil { return nil, err }" )
437+ g .P ("return m, nil" )
438+ g .P ("}" )
439+ g .P ()
440+ }
441+ if genCloseAndRecv {
442+ g .P ("func (x *" , streamImpl , ") CloseAndRecv() (*" , method .Output .GoIdent , ", error) {" )
443+ g .P ("if err := x.ClientStream.CloseSend(); err != nil { return nil, err }" )
444+ g .P ("m := new(" , method .Output .GoIdent , ")" )
445+ g .P ("if err := x.ClientStream.RecvMsg(m); err != nil { return nil, err }" )
446+ g .P ("return m, nil" )
447+ g .P ("}" )
448+ g .P ()
449+ }
386450}
387451
388452func serverSignature (g * protogen.GeneratedFile , method * protogen.Method ) string {
@@ -396,7 +460,11 @@ func serverSignature(g *protogen.GeneratedFile, method *protogen.Method) string
396460 reqArgs = append (reqArgs , "*" + g .QualifiedGoIdent (method .Input .GoIdent ))
397461 }
398462 if method .Desc .IsStreamingClient () || method .Desc .IsStreamingServer () {
399- reqArgs = append (reqArgs , serverStreamInterface (g , method ))
463+ if * useGenericStreams {
464+ reqArgs = append (reqArgs , serverStreamInterface (g , method ))
465+ } else {
466+ reqArgs = append (reqArgs , method .Parent .GoName + "_" + method .GoName + "Server" )
467+ }
400468 }
401469 return method .GoName + "(" + strings .Join (reqArgs , ", " ) + ") " + ret
402470}
@@ -477,8 +545,11 @@ func genServerMethod(_ *protogen.Plugin, _ *protogen.File, g *protogen.Generated
477545 return hname
478546 }
479547
480- typeParam := g .QualifiedGoIdent (method .Input .GoIdent ) + ", " + g .QualifiedGoIdent (method .Output .GoIdent )
481- streamImpl := g .QualifiedGoIdent (grpcPackage .Ident ("GenericServerStream" )) + "[" + typeParam + "]"
548+ streamImpl := unexport (service .GoName ) + method .GoName + "Server"
549+ if * useGenericStreams {
550+ typeParam := g .QualifiedGoIdent (method .Input .GoIdent ) + ", " + g .QualifiedGoIdent (method .Output .GoIdent )
551+ streamImpl = g .QualifiedGoIdent (grpcPackage .Ident ("GenericServerStream" )) + "[" + typeParam + "]"
552+ }
482553
483554 g .P ("func " , hnameFuncNameFormatter (hname ), "(srv interface{}, stream " , grpcPackage .Ident ("ServerStream" ), ") error {" )
484555 if ! method .Desc .IsStreamingClient () {
@@ -492,9 +563,59 @@ func genServerMethod(_ *protogen.Plugin, _ *protogen.File, g *protogen.Generated
492563 g .P ()
493564
494565 // Auxiliary types aliases, for backwards compatibility.
495- g .P ("// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name." )
496- g .P ("type " , service .GoName , "_" , method .GoName , "Server = " , serverStreamInterface (g , method ))
566+ if * useGenericStreams {
567+ g .P ("// This type alias is provided for backwards compatibility with existing code that references the prior non-generic stream type by name." )
568+ g .P ("type " , service .GoName , "_" , method .GoName , "Server = " , serverStreamInterface (g , method ))
569+ g .P ()
570+ return hname
571+ }
572+
573+ // Stream auxiliary types and methods, if we're not taking advantage of the
574+ // pre-implemented generic types and their methods.
575+ genSend := method .Desc .IsStreamingServer ()
576+ genSendAndClose := ! method .Desc .IsStreamingServer ()
577+ genRecv := method .Desc .IsStreamingClient ()
578+
579+ g .P ("type " , service .GoName , "_" , method .GoName , "Server interface {" )
580+ if genSend {
581+ g .P ("Send(*" , method .Output .GoIdent , ") error" )
582+ }
583+ if genSendAndClose {
584+ g .P ("SendAndClose(*" , method .Output .GoIdent , ") error" )
585+ }
586+ if genRecv {
587+ g .P ("Recv() (*" , method .Input .GoIdent , ", error)" )
588+ }
589+ g .P (grpcPackage .Ident ("ServerStream" ))
590+ g .P ("}" )
591+ g .P ()
592+
593+ g .P ("type " , streamImpl , " struct {" )
594+ g .P (grpcPackage .Ident ("ServerStream" ))
595+ g .P ("}" )
497596 g .P ()
597+
598+ if genSend {
599+ g .P ("func (x *" , streamImpl , ") Send(m *" , method .Output .GoIdent , ") error {" )
600+ g .P ("return x.ServerStream.SendMsg(m)" )
601+ g .P ("}" )
602+ g .P ()
603+ }
604+ if genSendAndClose {
605+ g .P ("func (x *" , streamImpl , ") SendAndClose(m *" , method .Output .GoIdent , ") error {" )
606+ g .P ("return x.ServerStream.SendMsg(m)" )
607+ g .P ("}" )
608+ g .P ()
609+ }
610+ if genRecv {
611+ g .P ("func (x *" , streamImpl , ") Recv() (*" , method .Input .GoIdent , ", error) {" )
612+ g .P ("m := new(" , method .Input .GoIdent , ")" )
613+ g .P ("if err := x.ServerStream.RecvMsg(m); err != nil { return nil, err }" )
614+ g .P ("return m, nil" )
615+ g .P ("}" )
616+ g .P ()
617+ }
618+
498619 return hname
499620}
500621
0 commit comments