Merge pull request 'append Service only in generated go files' (#87) from namingfix into master

Reviewed-on: #87
This commit is contained in:
Василий Толстов 2023-08-20 13:16:24 +03:00
commit 190a1903f4
2 changed files with 21 additions and 14 deletions

View File

@ -63,8 +63,8 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
g.reflection = *flagReflection g.reflection = *flagReflection
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
genClient := true var genClient bool
genServer := true var genServer bool
var genNone bool var genNone bool
if strings.Contains(g.components, "server") { if strings.Contains(g.components, "server") {

31
util.go
View File

@ -32,7 +32,7 @@ func unexport(s string) string {
} }
func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) { func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
// if rule, ok := getMicroApiService(service); ok { // if rule, ok := getMicroApiService(service); ok {
// gfile.P("// client wrappers ", strings.Join(rule.ClientWrappers, ", ")) // gfile.P("// client wrappers ", strings.Join(rule.ClientWrappers, ", "))
// } // }
@ -52,7 +52,7 @@ func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *p
} }
func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protogen.Service, component string) { func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protogen.Service, component string) {
serviceName := service.GoName serviceName := getServiceName(service)
for _, method := range service.Methods { for _, method := range service.Methods {
methodName := fmt.Sprintf("%s.%s", serviceName, method.GoName) methodName := fmt.Sprintf("%s.%s", serviceName, method.GoName)
if component == "drpc" { if component == "drpc" {
@ -324,7 +324,7 @@ func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile,
} }
func (g *Generator) generateServiceServer(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) { func (g *Generator) generateServiceServer(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
gfile.P("type ", unexport(serviceName), "Server struct {") gfile.P("type ", unexport(serviceName), "Server struct {")
if g.standalone { if g.standalone {
gfile.P(file.GoImportPath.Ident(serviceName), "Server") gfile.P(file.GoImportPath.Ident(serviceName), "Server")
@ -336,7 +336,7 @@ func (g *Generator) generateServiceServer(gfile *protogen.GeneratedFile, file *p
} }
func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
for _, method := range service.Methods { for _, method := range service.Methods {
generateServerFuncSignature(gfile, serviceName, method, true) generateServerFuncSignature(gfile, serviceName, method, true)
if rule, ok := getMicroApiMethod(method); ok { if rule, ok := getMicroApiMethod(method); ok {
@ -486,7 +486,7 @@ func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile,
} }
func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service, component string) { func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service, component string) {
serviceName := service.GoName serviceName := getServiceName(service)
if g.standalone { if g.standalone {
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", file.GoImportPath.Ident(serviceName), "Server, opts ...", microOptionsPackage.Ident("Option"), ") error {") gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", file.GoImportPath.Ident(serviceName), "Server, opts ...", microOptionsPackage.Ident("Option"), ") error {")
} else { } else {
@ -597,7 +597,7 @@ func generateClientSignature(gfile *protogen.GeneratedFile, serviceName string,
} }
func (g *Generator) generateServiceClientInterface(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceClientInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
gfile.P("type ", serviceName, "Client interface {") gfile.P("type ", serviceName, "Client interface {")
for _, method := range service.Methods { for _, method := range service.Methods {
generateClientSignature(gfile, serviceName, method) generateClientSignature(gfile, serviceName, method)
@ -607,7 +607,7 @@ func (g *Generator) generateServiceClientInterface(gfile *protogen.GeneratedFile
} }
func (g *Generator) generateServiceServerInterface(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceServerInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
gfile.P("type ", serviceName, "Server interface {") gfile.P("type ", serviceName, "Server interface {")
for _, method := range service.Methods { for _, method := range service.Methods {
generateServerSignature(gfile, serviceName, method, false) generateServerSignature(gfile, serviceName, method, false)
@ -617,7 +617,7 @@ func (g *Generator) generateServiceServerInterface(gfile *protogen.GeneratedFile
} }
func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
for _, method := range service.Methods { for _, method := range service.Methods {
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() { if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
continue continue
@ -645,7 +645,7 @@ func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.Generat
} }
func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
for _, method := range service.Methods { for _, method := range service.Methods {
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() { if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
continue continue
@ -807,7 +807,7 @@ func getGoIdentByMessage(messages []*protogen.Message, msg string) (protogen.GoI
} }
func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) { func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
gfile.P("// ", serviceName, "_ServiceDesc", " is the ", grpcPackage.Ident("ServiceDesc"), " for ", serviceName, " service.") gfile.P("// ", serviceName, "_ServiceDesc", " is the ", grpcPackage.Ident("ServiceDesc"), " for ", serviceName, " service.")
gfile.P("// It's only intended for direct use with ", grpcPackage.Ident("RegisterService"), ",") gfile.P("// It's only intended for direct use with ", grpcPackage.Ident("RegisterService"), ",")
@ -849,7 +849,7 @@ func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *pro
} }
func (g *Generator) generateServiceName(gfile *protogen.GeneratedFile, service *protogen.Service) { func (g *Generator) generateServiceName(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName serviceName := getServiceName(service)
gfile.P("var (") gfile.P("var (")
gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`) gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`)
gfile.P(")") gfile.P(")")
@ -859,7 +859,7 @@ func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, serv
if component != "http" { if component != "http" {
return return
} }
serviceName := service.GoName serviceName := getServiceName(service)
gfile.P("var (") gfile.P("var (")
gfile.P(serviceName, "ServerEndpoints = []", microServerHttpPackage.Ident("EndpointMetadata"), "{") gfile.P(serviceName, "ServerEndpoints = []", microServerHttpPackage.Ident("EndpointMetadata"), "{")
@ -884,3 +884,10 @@ func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, serv
gfile.P("}") gfile.P("}")
gfile.P(")") gfile.P(")")
} }
func getServiceName(s *protogen.Service) string {
if strings.HasSuffix(s.GoName, "Service") {
return s.GoName
}
return s.GoName + "Service"
}