Skip to content

Commit

Permalink
feat: generate server and client comments for methods
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Stewart <[email protected]>
  • Loading branch information
paralin committed Dec 11, 2024
1 parent 235b1b3 commit 4bb725c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
3 changes: 2 additions & 1 deletion cmd/protoc-gen-go-starpc/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
protoc-gen-starpc
protoc-gen-*-starpc
protoc-gen-go-starpc
20 changes: 16 additions & 4 deletions cmd/protoc-gen-go-starpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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("}")
Expand Down Expand Up @@ -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("}")
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions echo/echo_srpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions mock/mock_srpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4bb725c

Please sign in to comment.