fix: using standalone && auto tag set && fielaligment (#103)

This commit is contained in:
Denis
2026-03-01 18:13:34 +03:00
committed by GitHub
parent 2f67df5079
commit 545af44de7
13 changed files with 268 additions and 134 deletions

84
util.go
View File

@@ -8,9 +8,10 @@ import (
"strings"
"time"
api_options "go.unistack.org/micro-proto/v4/api"
apioptions "go.unistack.org/micro-proto/v4/api"
v2 "go.unistack.org/micro-proto/v4/openapiv2"
v3 "go.unistack.org/micro-proto/v4/openapiv3"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
)
@@ -31,6 +32,33 @@ func unexport(s string) string {
return strings.ToLower(s[:1]) + s[1:]
}
func (g *Generator) newGeneratedFile(plugin *protogen.Plugin, file *protogen.File, component string, genClient bool, genServer bool) *protogen.GeneratedFile {
gname := file.GeneratedFilenamePrefix + "_micro_" + component + ".pb.go"
path := file.GoImportPath
if g.standalone {
path = protogen.GoImportPath(string(file.GoImportPath) + "/" + component)
}
gFile := plugin.NewGeneratedFile(gname, path)
gFile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.")
gFile.P("// protoc-gen-go-micro version: " + versionComment)
gFile.P("// source: ", file.Proto.GetName())
gFile.P()
gFile.P("package ", file.GoPackageName)
gFile.P()
gFile.Import(contextPackage)
if genClient {
gFile.Import(microClientPackage)
}
if genServer {
gFile.Import(microServerPackage)
}
return gFile
}
func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) {
serviceName := service.GoName
// if rule, ok := getMicroApiService(service); ok {
@@ -42,7 +70,7 @@ func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *p
gfile.P("}")
if g.standalone {
gfile.P("func New", serviceName, "Client(name string, c ", microClientPackage.Ident("Client"), ") ", file.GoImportPath.Ident(serviceName), "Client {")
gfile.P("func New", serviceName, "Client(name string, c ", microClientPackage.Ident("Client"), ") ", gfile.QualifiedGoIdent(file.GoImportPath.Ident(serviceName+"Client")), " {")
} else {
gfile.P("func New", serviceName, "Client(name string, c ", microClientPackage.Ident("Client"), ") ", serviceName, "Client {")
}
@@ -141,7 +169,7 @@ func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile,
}
labelMethod:
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
if proto.HasExtension(method.Desc.Options(), apioptions.E_Http) {
gfile.P("opts = append(opts,")
endpoints, _ := generateEndpoints(method)
path, method, body := getEndpoint(endpoints[0])
@@ -327,7 +355,7 @@ func (g *Generator) generateServiceServer(gfile *protogen.GeneratedFile, file *p
serviceName := service.GoName
gfile.P("type ", unexport(serviceName), "Server struct {")
if g.standalone {
gfile.P(file.GoImportPath.Ident(serviceName), "Server")
gfile.P(gfile.QualifiedGoIdent(file.GoImportPath.Ident(serviceName + "Server")))
} else {
gfile.P(serviceName, "Server")
}
@@ -488,7 +516,7 @@ func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile,
func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service, component string) {
serviceName := service.GoName
if g.standalone {
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", file.GoImportPath.Ident(serviceName), "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {")
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", gfile.QualifiedGoIdent(file.GoImportPath.Ident(serviceName+"Server")), ", opts ...", microServerPackage.Ident("HandlerOption"), ") error {")
} else {
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {")
}
@@ -573,9 +601,11 @@ func (g *Generator) generateClientFuncSignature(gfile *protogen.GeneratedFile, s
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
args = append(args, "*", gfile.QualifiedGoIdent(method.Output.GoIdent))
} else {
// TODO
// args = append(args, gfile.QualifiedGoIdent(protogen.GoIdent{GoName: serviceName + "_" + method.GoName + "Client", GoImportPath: method.Output.GoIdent.GoImportPath}))
args = append(args, serviceName+"_"+method.GoName+"Client")
if g.standalone {
args = append(args, gfile.QualifiedGoIdent(file.GoImportPath.Ident(serviceName+"_"+method.GoName+"Client")))
} else {
args = append(args, serviceName+"_"+method.GoName+"Client")
}
}
args = append(args, ", error) {")
gfile.P(args...)
@@ -674,64 +704,64 @@ func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.Generat
}
}
func generateEndpoints(method *protogen.Method) ([]*api_options.HttpRule, bool) {
func generateEndpoints(method *protogen.Method) ([]*apioptions.HttpRule, bool) {
if method.Desc.Options() == nil {
return nil, false
}
if !proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
if !proto.HasExtension(method.Desc.Options(), apioptions.E_Http) {
return nil, false
}
r := proto.GetExtension(method.Desc.Options(), api_options.E_Http)
r := proto.GetExtension(method.Desc.Options(), apioptions.E_Http)
if r == nil {
return nil, false
}
rule := r.(*api_options.HttpRule)
rules := []*api_options.HttpRule{rule}
rule := r.(*apioptions.HttpRule)
rules := []*apioptions.HttpRule{rule}
rules = append(rules, rule.GetAdditionalBindings()...)
return rules, method.Desc.IsStreamingServer() || method.Desc.IsStreamingClient()
}
func getMicroApiMethod(method *protogen.Method) (*api_options.MicroMethod, bool) {
func getMicroApiMethod(method *protogen.Method) (*apioptions.MicroMethod, bool) {
if method.Desc.Options() == nil {
return nil, false
}
if !proto.HasExtension(method.Desc.Options(), api_options.E_MicroMethod) {
if !proto.HasExtension(method.Desc.Options(), apioptions.E_MicroMethod) {
return nil, false
}
r := proto.GetExtension(method.Desc.Options(), api_options.E_MicroMethod)
r := proto.GetExtension(method.Desc.Options(), apioptions.E_MicroMethod)
if r == nil {
return nil, false
}
rule := r.(*api_options.MicroMethod)
rule := r.(*apioptions.MicroMethod)
return rule, true
}
func getMicroApiService(service *protogen.Service) (*api_options.MicroService, bool) {
func getMicroApiService(service *protogen.Service) (*apioptions.MicroService, bool) {
if service.Desc.Options() == nil {
return nil, false
}
if !proto.HasExtension(service.Desc.Options(), api_options.E_MicroService) {
if !proto.HasExtension(service.Desc.Options(), apioptions.E_MicroService) {
return nil, false
}
r := proto.GetExtension(service.Desc.Options(), api_options.E_MicroService)
r := proto.GetExtension(service.Desc.Options(), apioptions.E_MicroService)
if r == nil {
return nil, false
}
rule := r.(*api_options.MicroService)
rule := r.(*apioptions.MicroService)
return rule, true
}
func getEndpoint(rule *api_options.HttpRule) (string, string, string) {
func getEndpoint(rule *apioptions.HttpRule) (string, string, string) {
var meth string
var path string
var body string
@@ -762,7 +792,7 @@ func getEndpoint(rule *api_options.HttpRule) (string, string, string) {
return path, meth, body
}
func generateEndpoint(gfile *protogen.GeneratedFile, serviceName string, methodName string, rule *api_options.HttpRule, streaming bool) {
func generateEndpoint(gfile *protogen.GeneratedFile, serviceName string, methodName string, rule *apioptions.HttpRule, streaming bool) {
path, meth, body := getEndpoint(rule)
gfile.P("Name:", fmt.Sprintf(`"%s.%s",`, serviceName, methodName))
gfile.P("Path:", fmt.Sprintf(`[]string{"%s"},`, path))
@@ -846,7 +876,11 @@ func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *pro
gfile.P("// and not to be introspected or modified (even as a copy)")
gfile.P("var ", serviceName, "_ServiceDesc", " = ", grpcPackage.Ident("ServiceDesc"), " {")
gfile.P("ServiceName: ", strconv.Quote(string(service.Desc.FullName())), ",")
gfile.P("HandlerType: (*", serviceName, "Server)(nil),")
if g.standalone {
gfile.P("HandlerType: (*", gfile.QualifiedGoIdent(file.GoImportPath.Ident(serviceName+"Server")), ")(nil),")
} else {
gfile.P("HandlerType: (*", serviceName, "Server)(nil),")
}
gfile.P("Methods: []", grpcPackage.Ident("MethodDesc"), "{")
for _, method := range service.Methods {
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
@@ -896,7 +930,7 @@ func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, serv
serviceName := service.GoName
for _, method := range service.Methods {
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
if proto.HasExtension(method.Desc.Options(), apioptions.E_Http) {
if endpoints, streaming := generateEndpoints(method); endpoints != nil {
if !generate {
gfile.P("var (")