|
|
@@ -10,42 +10,28 @@ import (
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
|
|
|
- descpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
|
|
|
"github.com/golang/protobuf/v2/protogen"
|
|
|
)
|
|
|
|
|
|
-type fileInfo struct {
|
|
|
- *protogen.File
|
|
|
- locationMap map[string][]*descpb.SourceCodeInfo_Location
|
|
|
-}
|
|
|
-
|
|
|
// GenerateFile generates a _grpc.pb.go file containing gRPC service definitions.
|
|
|
-func GenerateFile(gen *protogen.Plugin, f *protogen.File) {
|
|
|
- if len(f.Services) == 0 {
|
|
|
+func GenerateFile(gen *protogen.Plugin, file *protogen.File) {
|
|
|
+ if len(file.Services) == 0 {
|
|
|
return
|
|
|
}
|
|
|
- filename := f.GeneratedFilenamePrefix + "_grpc.pb.go"
|
|
|
- g := gen.NewGeneratedFile(filename, f.GoImportPath)
|
|
|
+ filename := file.GeneratedFilenamePrefix + "_grpc.pb.go"
|
|
|
+ g := gen.NewGeneratedFile(filename, file.GoImportPath)
|
|
|
g.P("// Code generated by protoc-gen-go-grpc. DO NOT EDIT.")
|
|
|
g.P()
|
|
|
- g.P("package ", f.GoPackageName)
|
|
|
+ g.P("package ", file.GoPackageName)
|
|
|
g.P()
|
|
|
- GenerateFileContent(gen, f, g)
|
|
|
+ GenerateFileContent(gen, file, g)
|
|
|
}
|
|
|
|
|
|
// GenerateFileContent generates the gRPC service definitions, excluding the package statement.
|
|
|
-func GenerateFileContent(gen *protogen.Plugin, f *protogen.File, g *protogen.GeneratedFile) {
|
|
|
- if len(f.Services) == 0 {
|
|
|
+func GenerateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile) {
|
|
|
+ if len(file.Services) == 0 {
|
|
|
return
|
|
|
}
|
|
|
- file := &fileInfo{
|
|
|
- File: f,
|
|
|
- locationMap: make(map[string][]*descpb.SourceCodeInfo_Location),
|
|
|
- }
|
|
|
- for _, loc := range file.Proto.GetSourceCodeInfo().GetLocation() {
|
|
|
- key := pathKey(loc.Path)
|
|
|
- file.locationMap[key] = append(file.locationMap[key], loc)
|
|
|
- }
|
|
|
|
|
|
// TODO: Remove this. We don't need to include these references any more.
|
|
|
g.P("// Reference imports to suppress errors if they are not otherwise used.")
|
|
|
@@ -62,7 +48,7 @@ func GenerateFileContent(gen *protogen.Plugin, f *protogen.File, g *protogen.Gen
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func genService(gen *protogen.Plugin, file *fileInfo, g *protogen.GeneratedFile, service *protogen.Service) {
|
|
|
+func genService(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, service *protogen.Service) {
|
|
|
clientName := service.GoName + "Client"
|
|
|
|
|
|
g.P("// ", clientName, " is the client API for ", service.GoName, " service.")
|
|
|
@@ -77,7 +63,7 @@ func genService(gen *protogen.Plugin, file *fileInfo, g *protogen.GeneratedFile,
|
|
|
g.Annotate(clientName, service.Location)
|
|
|
g.P("type ", clientName, " interface {")
|
|
|
for _, method := range service.Methods {
|
|
|
- genComment(g, file, method.Location)
|
|
|
+ g.PrintLeadingComments(method.Location)
|
|
|
g.Annotate(clientName+"."+method.GoName, method.Location)
|
|
|
g.P(clientSignature(g, method))
|
|
|
}
|
|
|
@@ -123,7 +109,7 @@ func genService(gen *protogen.Plugin, file *fileInfo, g *protogen.GeneratedFile,
|
|
|
g.Annotate(serverType, service.Location)
|
|
|
g.P("type ", serverType, " interface {")
|
|
|
for _, method := range service.Methods {
|
|
|
- genComment(g, file, method.Location)
|
|
|
+ g.PrintLeadingComments(method.Location)
|
|
|
g.Annotate(serverType+"."+method.GoName, method.Location)
|
|
|
g.P(serverSignature(g, method))
|
|
|
}
|
|
|
@@ -199,7 +185,7 @@ func clientSignature(g *protogen.GeneratedFile, method *protogen.Method) string
|
|
|
return s
|
|
|
}
|
|
|
|
|
|
-func genClientMethod(gen *protogen.Plugin, file *fileInfo, g *protogen.GeneratedFile, method *protogen.Method, index int) {
|
|
|
+func genClientMethod(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, method *protogen.Method, index int) {
|
|
|
service := method.ParentService
|
|
|
sname := fmt.Sprintf("/%s/%s", service.Desc.FullName(), method.Desc.Name())
|
|
|
|
|
|
@@ -294,7 +280,7 @@ func serverSignature(g *protogen.GeneratedFile, method *protogen.Method) string
|
|
|
return method.GoName + "(" + strings.Join(reqArgs, ", ") + ") " + ret
|
|
|
}
|
|
|
|
|
|
-func genServerMethod(gen *protogen.Plugin, file *fileInfo, g *protogen.GeneratedFile, method *protogen.Method) string {
|
|
|
+func genServerMethod(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, method *protogen.Method) string {
|
|
|
service := method.ParentService
|
|
|
hname := fmt.Sprintf("_%s_%s_Handler", service.GoName, method.GoName)
|
|
|
|
|
|
@@ -388,32 +374,6 @@ func ident(name string) protogen.GoIdent {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func genComment(g *protogen.GeneratedFile, file *fileInfo, loc protogen.Location) (hasComment bool) {
|
|
|
- for _, loc := range file.locationMap[pathKey(loc.Path)] {
|
|
|
- if loc.LeadingComments == nil {
|
|
|
- continue
|
|
|
- }
|
|
|
- for _, line := range strings.Split(strings.TrimSuffix(loc.GetLeadingComments(), "\n"), "\n") {
|
|
|
- hasComment = true
|
|
|
- g.P("//", line)
|
|
|
- }
|
|
|
- break
|
|
|
- }
|
|
|
- return hasComment
|
|
|
-}
|
|
|
-
|
|
|
const deprecationComment = "// Deprecated: Do not use."
|
|
|
|
|
|
-// pathKey converts a location path to a string suitable for use as a map key.
|
|
|
-func pathKey(path []int32) string {
|
|
|
- var buf []byte
|
|
|
- for i, x := range path {
|
|
|
- if i != 0 {
|
|
|
- buf = append(buf, ',')
|
|
|
- }
|
|
|
- buf = strconv.AppendInt(buf, int64(x), 10)
|
|
|
- }
|
|
|
- return string(buf)
|
|
|
-}
|
|
|
-
|
|
|
func unexport(s string) string { return strings.ToLower(s[:1]) + s[1:] }
|