Compare commits

..

No commits in common. "master" and "v4.0.0" have entirely different histories.

8 changed files with 49 additions and 223 deletions

10
go.mod
View File

@ -5,14 +5,14 @@ go 1.19
require (
github.com/fatih/structtag v1.2.0
go.unistack.org/micro-proto/v4 v4.0.1
golang.org/x/tools v0.11.0
google.golang.org/protobuf v1.31.0
golang.org/x/tools v0.1.12
google.golang.org/protobuf v1.28.1
)
require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.6.9 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/sys v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

20
go.sum
View File

@ -37,9 +37,8 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/gnostic v0.6.9 h1:ZK/5VhkoX835RikCHpSUJV9a+S3e1zLh59YnyWeBW+0=
github.com/google/gnostic v0.6.9/go.mod h1:Nm8234We1lq6iB9OmlgNv3nH91XLLVZHCDayfA3xq+E=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
@ -76,8 +75,8 @@ golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@ -93,7 +92,6 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
@ -102,8 +100,8 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
@ -114,8 +112,8 @@ golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8=
golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@ -145,8 +143,8 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -38,15 +38,14 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genC
}
for _, service := range file.Services {
g.generateServiceEndpoints(gfile, service, component)
if genClient {
g.generateServiceClient(gfile, file, service)
g.generateServiceClient(gfile, service)
g.generateServiceClientMethods(gfile, service, component)
}
if genServer {
g.generateServiceServer(gfile, file, service)
generateServiceServer(gfile, service)
g.generateServiceServerMethods(gfile, service)
g.generateServiceRegister(gfile, file, service, component)
g.generateServiceRegister(gfile, service, component)
}
}
}

View File

@ -90,9 +90,6 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
continue
case "micro":
err = g.microGenerate(component, plugin, genClient, genServer)
if err == nil {
err = g.writeErrors(plugin)
}
case "http":
err = g.httpGenerate(component, plugin, genClient, genServer)
case "grpc", "drpc", "rpc":

View File

@ -40,6 +40,7 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, gen
// generate services
for _, service := range file.Services {
g.generateServiceName(gfile, service)
g.generateServiceEndpoints(gfile, service)
if genClient {
g.generateServiceClientInterface(gfile, service)
g.generateServiceClientStreamInterface(gfile, service)

6
rpc.go
View File

@ -37,13 +37,13 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genCl
}
for _, service := range file.Services {
if genClient {
g.generateServiceClient(gfile, file, service)
g.generateServiceClient(gfile, service)
g.generateServiceClientMethods(gfile, service, component)
}
if genServer {
g.generateServiceServer(gfile, file, service)
generateServiceServer(gfile, service)
g.generateServiceServerMethods(gfile, service)
g.generateServiceRegister(gfile, file, service, component)
g.generateServiceRegister(gfile, service, component)
}
if component == "grpc" && g.reflection {
g.generateServiceDesc(gfile, file, service)

219
util.go
View File

@ -31,8 +31,8 @@ func unexport(s string) string {
return strings.ToLower(s[:1]) + s[1:]
}
func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) {
serviceName := getServiceName(service)
func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName
// if rule, ok := getMicroApiService(service); ok {
// gfile.P("// client wrappers ", strings.Join(rule.ClientWrappers, ", "))
// }
@ -41,18 +41,14 @@ func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *p
gfile.P("name string")
gfile.P("}")
if g.standalone {
gfile.P("func New", serviceName, "Client(name string, c ", microClientPackage.Ident("Client"), ") ", file.GoImportPath.Ident(serviceName), "Client {")
} else {
gfile.P("func New", serviceName, "Client(name string, c ", microClientPackage.Ident("Client"), ") ", serviceName, "Client {")
}
gfile.P("func New", serviceName, "Client(name string, c ", microClientPackage.Ident("Client"), ") ", serviceName, "Client {")
gfile.P("return &", unexport(serviceName), "Client{c: c, name: name}")
gfile.P("}")
gfile.P()
}
func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protogen.Service, component string) {
serviceName := getServiceName(service)
serviceName := service.GoName
for _, method := range service.Methods {
methodName := fmt.Sprintf("%s.%s", serviceName, method.GoName)
if component == "drpc" {
@ -323,20 +319,16 @@ func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile,
}
}
func (g *Generator) generateServiceServer(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) {
serviceName := getServiceName(service)
func generateServiceServer(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName
gfile.P("type ", unexport(serviceName), "Server struct {")
if g.standalone {
gfile.P(file.GoImportPath.Ident(serviceName), "Server")
} else {
gfile.P(serviceName, "Server")
}
gfile.P(serviceName, "Server")
gfile.P("}")
gfile.P()
}
func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := getServiceName(service)
serviceName := service.GoName
for _, method := range service.Methods {
generateServerFuncSignature(gfile, serviceName, method, true)
if rule, ok := getMicroApiMethod(method); ok {
@ -485,13 +477,9 @@ func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile,
}
}
func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service, component string) {
serviceName := getServiceName(service)
if g.standalone {
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", file.GoImportPath.Ident(serviceName), "Server, opts ...", microOptionsPackage.Ident("Option"), ") error {")
} else {
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microOptionsPackage.Ident("Option"), ") error {")
}
func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Service, component string) {
serviceName := service.GoName
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {")
gfile.P("type ", unexport(serviceName), " interface {")
for _, method := range service.Methods {
generateServerSignature(gfile, serviceName, method, true)
@ -501,15 +489,11 @@ func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, file
gfile.P(unexport(serviceName))
gfile.P("}")
gfile.P("h := &", unexport(serviceName), "Server{sh}")
gfile.P("var nopts []", microOptionsPackage.Ident("Option"))
gfile.P("var nopts []", microServerPackage.Ident("HandlerOption"))
if component == "http" {
// if g.standalone {
// gfile.P("nopts = append(nopts, ", microServerHttpPackage.Ident("HandlerEndpoints"), "(", file.GoImportPath.Ident(serviceName), "ServerEndpoints))")
// } else {
gfile.P("nopts = append(nopts, ", microServerHttpPackage.Ident("HandlerEndpoints"), "(", serviceName, "ServerEndpoints))")
// }
}
gfile.P("return s.Handle(&", serviceName, "{h}, append(nopts, opts...)...)")
gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))")
gfile.P("}")
}
@ -568,11 +552,11 @@ func (g *Generator) generateClientFuncSignature(gfile *protogen.GeneratedFile, s
if !method.Desc.IsStreamingClient() {
args = append(args, "req *", gfile.QualifiedGoIdent(method.Input.GoIdent), ", ")
}
args = append(args, "opts ...", microOptionsPackage.Ident("Option"), ") (")
args = append(args, "opts ...", microClientPackage.Ident("CallOption"), ") (")
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
args = append(args, "*", gfile.QualifiedGoIdent(method.Output.GoIdent))
} else {
args = append(args, gfile.QualifiedGoIdent(protogen.GoIdent{GoName: serviceName + "_" + method.GoName + "Client", GoImportPath: method.Output.GoIdent.GoImportPath}))
args = append(args, serviceName, "_", method.GoName, "Client")
}
args = append(args, ", error) {")
gfile.P(args...)
@ -586,7 +570,7 @@ func generateClientSignature(gfile *protogen.GeneratedFile, serviceName string,
if !method.Desc.IsStreamingClient() {
args = append(args, "req *", gfile.QualifiedGoIdent(method.Input.GoIdent), ", ")
}
args = append(args, "opts ...", microOptionsPackage.Ident("Option"), ") (")
args = append(args, "opts ...", microClientPackage.Ident("CallOption"), ") (")
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
args = append(args, "*", gfile.QualifiedGoIdent(method.Output.GoIdent))
} else {
@ -597,7 +581,7 @@ func generateClientSignature(gfile *protogen.GeneratedFile, serviceName string,
}
func (g *Generator) generateServiceClientInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := getServiceName(service)
serviceName := service.GoName
gfile.P("type ", serviceName, "Client interface {")
for _, method := range service.Methods {
generateClientSignature(gfile, serviceName, method)
@ -607,7 +591,7 @@ func (g *Generator) generateServiceClientInterface(gfile *protogen.GeneratedFile
}
func (g *Generator) generateServiceServerInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := getServiceName(service)
serviceName := service.GoName
gfile.P("type ", serviceName, "Server interface {")
for _, method := range service.Methods {
generateServerSignature(gfile, serviceName, method, false)
@ -617,7 +601,7 @@ func (g *Generator) generateServiceServerInterface(gfile *protogen.GeneratedFile
}
func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := getServiceName(service)
serviceName := service.GoName
for _, method := range service.Methods {
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
continue
@ -629,7 +613,7 @@ func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.Generat
gfile.P("RecvMsg(msg interface{}) error")
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
gfile.P("CloseAndRecv() (*", gfile.QualifiedGoIdent(method.Output.GoIdent), ", error)")
gfile.P("CloseSend() error")
gfile.P("CloseSend() () error")
}
gfile.P("Close() error")
if method.Desc.IsStreamingClient() {
@ -645,7 +629,7 @@ func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.Generat
}
func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := getServiceName(service)
serviceName := service.GoName
for _, method := range service.Methods {
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
continue
@ -657,7 +641,7 @@ func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.Generat
gfile.P("RecvMsg(msg interface{}) error")
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
gfile.P("SendAndClose(msg *", gfile.QualifiedGoIdent(method.Output.GoIdent), ") error")
// gfile.P("CloseSend() error")
gfile.P("CloseSend() error")
}
gfile.P("Close() error")
if method.Desc.IsStreamingClient() {
@ -792,35 +776,6 @@ func (g *Generator) getGoIdentByXref(xref string) (protogen.GoIdent, error) {
return protogen.GoIdent{}, fmt.Errorf("not found")
}
func (g *Generator) getMessageByXref(xref string) (*protogen.Message, error) {
idx := strings.LastIndex(xref, ".")
pkg := xref[:idx]
msg := xref[idx+1:]
for _, file := range g.plugin.Files {
if strings.Compare(pkg, *(file.Proto.Package)) != 0 {
continue
}
if pmsg, err := getProtoMessage(file.Messages, msg); err == nil {
return pmsg, nil
}
}
return nil, fmt.Errorf("not found")
}
func getProtoMessage(messages []*protogen.Message, msg string) (*protogen.Message, error) {
for _, message := range messages {
if strings.Compare(msg, message.GoIdent.GoName) == 0 {
return message, nil
}
if len(message.Messages) > 0 {
if pmsg, err := getProtoMessage(message.Messages, msg); err == nil {
return pmsg, nil
}
}
}
return nil, fmt.Errorf("not found")
}
func getGoIdentByMessage(messages []*protogen.Message, msg string) (protogen.GoIdent, error) {
for _, message := range messages {
if strings.Compare(msg, message.GoIdent.GoName) == 0 {
@ -836,7 +791,7 @@ func getGoIdentByMessage(messages []*protogen.Message, msg string) (protogen.GoI
}
func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service) {
serviceName := getServiceName(service)
serviceName := service.GoName
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"), ",")
@ -878,17 +833,14 @@ func (g *Generator) generateServiceDesc(gfile *protogen.GeneratedFile, file *pro
}
func (g *Generator) generateServiceName(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := getServiceName(service)
serviceName := service.GoName
gfile.P("var (")
gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`)
gfile.P(")")
}
func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service, component string) {
if component != "http" {
return
}
serviceName := getServiceName(service)
func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName
gfile.P("var (")
gfile.P(serviceName, "ServerEndpoints = []", microServerHttpPackage.Ident("EndpointMetadata"), "{")
@ -913,122 +865,3 @@ func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, serv
gfile.P("}")
gfile.P(")")
}
func getServiceName(s *protogen.Service) string {
if strings.HasSuffix(s.GoName, "Service") {
return s.GoName
}
return s.GoName + "Service"
}
func (g *Generator) writeErrors(plugin *protogen.Plugin) error {
errorsMap := make(map[string]struct{})
for _, file := range plugin.Files {
for _, service := range file.Services {
for _, method := range service.Methods {
if method.Desc.Options() != nil {
if proto.HasExtension(method.Desc.Options(), v2.E_Openapiv2Operation) {
opts := proto.GetExtension(method.Desc.Options(), v2.E_Openapiv2Operation)
if opts != nil {
r := opts.(*v2.Operation)
if r.Responses == nil {
continue
}
for _, rsp := range r.Responses.ResponseCode {
if schema := rsp.Value.GetJsonReference(); schema != nil {
xref := schema.XRef
if xref[0] == '.' {
xref = xref[1:]
}
errorsMap[xref] = struct{}{}
}
}
}
}
if proto.HasExtension(method.Desc.Options(), v3.E_Openapiv3Operation) {
opts := proto.GetExtension(method.Desc.Options(), v3.E_Openapiv3Operation)
if opts != nil {
r := opts.(*v3.Operation)
if r.Responses == nil {
continue
}
resps := r.Responses.ResponseOrReference
if r.Responses.GetDefault() != nil {
resps = append(resps, &v3.NamedResponseOrReference{Name: "default", Value: r.Responses.GetDefault()})
}
for _, rsp := range resps {
if schema := rsp.Value.GetReference(); schema != nil {
xref := schema.XRef
if xref[0] == '.' {
xref = xref[1:]
}
errorsMap[xref] = struct{}{}
}
}
}
}
}
}
}
}
var gfile *protogen.GeneratedFile
var importPath protogen.GoImportPath
if len(errorsMap) > 0 {
var packageName string
for _, file := range plugin.Files {
if !file.Generate {
continue
}
if len(file.Services) == 0 {
continue
}
packageName = string(file.GoPackageName)
importPath = file.GoImportPath
break
}
if g.standalone {
importPath = "."
}
gfile = plugin.NewGeneratedFile("micro_errors.pb.go", importPath)
gfile.P("// Code generated by protoc-gen-go-micro. DO NOT EDIT.")
gfile.P("// protoc-gen-go-micro version: " + versionComment)
gfile.P()
gfile.P("package ", packageName)
gfile.P()
gfile.Import(protojsonPackage)
gfile.P("var (")
gfile.P("marshaler = ", protojsonPackage.Ident("MarshalOptions"), "{}")
gfile.P(")")
}
for xref := range errorsMap {
msg, err := g.getMessageByXref(xref)
if err != nil {
return err
}
for _, field := range msg.Fields {
if field.GoName == "Error" {
return fmt.Errorf("failed generate Error() string interface for %s message %s already have Error field", field.Location.SourceFile, msg.Desc.Name())
}
}
gfile.P(`func (m *`, msg.GoIdent.GoName, `) Error() string {`)
gfile.P(`buf, _ := marshaler.Marshal(m)`)
gfile.P("return string(buf)")
gfile.P(`}`)
// log.Printf("xref %#+v %v\n", msg.GoIdent.GoName, err)
}
return nil
}

View File

@ -16,12 +16,10 @@ var (
microServerPackage = protogen.GoImportPath("go.unistack.org/micro/v4/server")
microClientHttpPackage = protogen.GoImportPath("go.unistack.org/micro-client-http/v4")
microServerHttpPackage = protogen.GoImportPath("go.unistack.org/micro-server-http/v4")
microCodecPackage = protogen.GoImportPath("go.unistack.org/micro-proto/v4/codec")
microCodecPackage = protogen.GoImportPath("go.unistack.org/micro/v4/codec")
microErrorsPackage = protogen.GoImportPath("go.unistack.org/micro/v4/errors")
microOptionsPackage = protogen.GoImportPath("go.unistack.org/micro/v4/options")
grpcPackage = protogen.GoImportPath("google.golang.org/grpc")
protojsonPackage = protogen.GoImportPath("google.golang.org/protobuf/encoding/protojson")
timePackage = protogen.GoImportPath("time")
deprecationComment = "// Deprecated: Do not use."
versionComment = "v4.0.2"
versionComment = "v4.0.0"
)