Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a service regsitry when generate source code #740

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
178 changes: 95 additions & 83 deletions gogoproto/gogo.pb.go

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

5 changes: 5 additions & 0 deletions gogoproto/gogo.proto
Expand Up @@ -142,3 +142,8 @@ extend google.protobuf.FieldOptions {
optional bool wktpointer = 65012;

}

extend google.protobuf.ServiceOptions {
optional bool genregistry = 168001;
}

10 changes: 10 additions & 0 deletions gogoproto/helper.go
Expand Up @@ -413,3 +413,13 @@ func HasSizecache(file *google_protobuf.FileDescriptorProto, message *google_pro
func HasUnkeyed(file *google_protobuf.FileDescriptorProto, message *google_protobuf.DescriptorProto) bool {
return proto.GetBoolExtension(message.Options, E_GoprotoUnkeyed, proto.GetBoolExtension(file.Options, E_GoprotoUnkeyedAll, true))
}

func IsServiceGenRegistry(service *google_protobuf.ServiceDescriptorProto) bool {
if service == nil {
return false
}
if service.Options == nil {
return false
}
return proto.GetBoolExtension(service.Options, E_Genregistry, false)
}
17 changes: 16 additions & 1 deletion protoc-gen-gogo/grpc/grpc.go
Expand Up @@ -39,6 +39,7 @@ import (
"strconv"
"strings"

"github.com/gogo/protobuf/gogoproto"
pb "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
)
Expand Down Expand Up @@ -151,7 +152,7 @@ func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.Servi
}
servName := generator.CamelCase(origServName)
deprecated := service.GetOptions().GetDeprecated()

genRegistry := gogoproto.IsServiceGenRegistry(service)
g.P()
g.P(fmt.Sprintf(`// %sClient is the client API for %s service.
//
Expand Down Expand Up @@ -273,6 +274,20 @@ func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.Servi
g.P("},")
g.P("Metadata: \"", file.GetName(), "\",")
g.P("}")

if genRegistry {
registry := servName + "ServiceRegistry"
g.P()
g.P("type _", registry, " struct {")
g.P("ServiceDesc *", grpcPkg, ".ServiceDesc")
g.P("ImplService ", serverType)
g.P()
g.P("}")
g.P("var ", registry, " = _", registry, "{")
g.P("ServiceDesc: ", serviceDescVar, ",")
g.P("}")
g.P()
}
g.P()
}

Expand Down