From 4bb725cc55aa51461d9c69b3822d912555c5e965 Mon Sep 17 00:00:00 2001 From: Christian Stewart Date: Tue, 10 Dec 2024 16:36:47 -0800 Subject: [PATCH] feat: generate server and client comments for methods Signed-off-by: Christian Stewart --- cmd/protoc-gen-go-starpc/.gitignore | 3 ++- cmd/protoc-gen-go-starpc/main.go | 20 ++++++++++++++++---- echo/echo_srpc.pb.go | 13 +++++++++++++ mock/mock_srpc.pb.go | 3 +++ 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/cmd/protoc-gen-go-starpc/.gitignore b/cmd/protoc-gen-go-starpc/.gitignore index b46f4a5e..c3b710e5 100644 --- a/cmd/protoc-gen-go-starpc/.gitignore +++ b/cmd/protoc-gen-go-starpc/.gitignore @@ -1 +1,2 @@ -protoc-gen-starpc \ No newline at end of file +protoc-gen-*-starpc +protoc-gen-go-starpc \ No newline at end of file diff --git a/cmd/protoc-gen-go-starpc/main.go b/cmd/protoc-gen-go-starpc/main.go index 65733b1f..4e859363 100644 --- a/cmd/protoc-gen-go-starpc/main.go +++ b/cmd/protoc-gen-go-starpc/main.go @@ -97,10 +97,6 @@ func (s *srpc) ServerServiceID(service *protogen.Service) string { return "SRPC" + service.GoName + "ServiceID" } -func (s *srpc) ServerImpl(service *protogen.Service) string { - return "srpc" + service.GoName + "Server" -} - func (s *srpc) ServerHandler(service *protogen.Service) string { return "SRPC" + service.GoName + "Handler" } @@ -137,9 +133,11 @@ func (s *srpc) ServerStreamImpl(method *protogen.Method) string { func (s *srpc) generateService(service *protogen.Service) { // Client interface s.P("type ", s.ClientIface(service), " interface {") + s.P("// SRPCClient returns the underlying SRPC client.") s.P("SRPCClient() ", s.Ident(SRPCPackage, "Client")) s.P() for _, method := range service.Methods { + s.P(s.generateMethodComment(method)) s.P(s.generateClientSignature(method)) } s.P("}") @@ -175,6 +173,7 @@ func (s *srpc) generateService(service *protogen.Service) { // Server interface s.P("type ", s.ServerIface(service), " interface {") for _, method := range service.Methods { + s.P(s.generateMethodComment(method)) s.P(s.generateServerSignature(method)) } s.P("}") @@ -409,6 +408,19 @@ func (s *srpc) generateClientMethod(p *protogen.Method) { // server methods // +func (s *srpc) generateMethodComment(method *protogen.Method) string { + comment := method.Comments.Leading.String() + if comment == "" { + return "" + } + // Ensure comment starts with // + if !strings.HasPrefix(comment, "//") { + comment = "// " + comment + } + // Remove trailing newline if present + return strings.TrimRight(comment, "\n") +} + func (s *srpc) generateServerSignature(method *protogen.Method) string { var reqArgs []string // if neither client nor server is streaming, expose ctx as a parameter. diff --git a/echo/echo_srpc.pb.go b/echo/echo_srpc.pb.go index d4aa2750..0f962fe4 100644 --- a/echo/echo_srpc.pb.go +++ b/echo/echo_srpc.pb.go @@ -13,13 +13,20 @@ import ( ) type SRPCEchoerClient interface { + // SRPCClient returns the underlying SRPC client. SRPCClient() srpc.Client + // Echo returns the given message. Echo(ctx context.Context, in *EchoMsg) (*EchoMsg, error) + // EchoServerStream is an example of a server -> client one-way stream. EchoServerStream(ctx context.Context, in *EchoMsg) (SRPCEchoer_EchoServerStreamClient, error) + // EchoClientStream is an example of client->server one-way stream. EchoClientStream(ctx context.Context) (SRPCEchoer_EchoClientStreamClient, error) + // EchoBidiStream is an example of a two-way stream. EchoBidiStream(ctx context.Context) (SRPCEchoer_EchoBidiStreamClient, error) + // RpcStream opens a nested rpc call stream. RpcStream(ctx context.Context) (SRPCEchoer_RpcStreamClient, error) + // DoNothing does nothing. DoNothing(ctx context.Context, in *emptypb.Empty) (*emptypb.Empty, error) } @@ -216,11 +223,17 @@ func (c *srpcEchoerClient) DoNothing(ctx context.Context, in *emptypb.Empty) (*e } type SRPCEchoerServer interface { + // Echo returns the given message. Echo(context.Context, *EchoMsg) (*EchoMsg, error) + // EchoServerStream is an example of a server -> client one-way stream. EchoServerStream(*EchoMsg, SRPCEchoer_EchoServerStreamStream) error + // EchoClientStream is an example of client->server one-way stream. EchoClientStream(SRPCEchoer_EchoClientStreamStream) (*EchoMsg, error) + // EchoBidiStream is an example of a two-way stream. EchoBidiStream(SRPCEchoer_EchoBidiStreamStream) error + // RpcStream opens a nested rpc call stream. RpcStream(SRPCEchoer_RpcStreamStream) error + // DoNothing does nothing. DoNothing(context.Context, *emptypb.Empty) (*emptypb.Empty, error) } diff --git a/mock/mock_srpc.pb.go b/mock/mock_srpc.pb.go index d4fa60a5..830367b1 100644 --- a/mock/mock_srpc.pb.go +++ b/mock/mock_srpc.pb.go @@ -11,8 +11,10 @@ import ( ) type SRPCMockClient interface { + // SRPCClient returns the underlying SRPC client. SRPCClient() srpc.Client + // MockRequest runs a mock unary request. MockRequest(ctx context.Context, in *MockMsg) (*MockMsg, error) } @@ -44,6 +46,7 @@ func (c *srpcMockClient) MockRequest(ctx context.Context, in *MockMsg) (*MockMsg } type SRPCMockServer interface { + // MockRequest runs a mock unary request. MockRequest(context.Context, *MockMsg) (*MockMsg, error) }