Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
750c949723 | |||
4633928da5 | |||
5ecd91182f | |||
a8b52d800d | |||
9c05b6b646 | |||
675ca27dbf |
33
go.mod
33
go.mod
@ -1,24 +1,31 @@
|
|||||||
module go.unistack.org/protoc-gen-go-micro/v3
|
module go.unistack.org/protoc-gen-go-micro/v3
|
||||||
|
|
||||||
go 1.20
|
go 1.22.0
|
||||||
|
|
||||||
|
toolchain go1.23.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/danielvladco/go-proto-gql v0.10.0
|
|
||||||
github.com/fatih/structtag v1.2.0
|
github.com/fatih/structtag v1.2.0
|
||||||
github.com/jhump/protoreflect v1.7.1
|
github.com/jhump/protoreflect v1.17.0
|
||||||
github.com/vektah/gqlparser/v2 v2.5.1
|
github.com/vektah/gqlparser/v2 v2.5.20
|
||||||
go.unistack.org/micro-proto/v3 v3.4.1
|
go.unistack.org/micro-proto/v3 v3.4.1
|
||||||
golang.org/x/tools v0.11.0
|
golang.org/x/tools v0.28.0
|
||||||
google.golang.org/protobuf v1.28.1
|
google.golang.org/protobuf v1.35.2
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/golang/protobuf v1.5.3 // indirect
|
github.com/bufbuild/protocompile v0.14.1 // indirect
|
||||||
github.com/google/gnostic v0.6.9 // indirect
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||||
github.com/google/go-cmp v0.5.9 // indirect
|
github.com/golang/protobuf v1.5.4 // indirect
|
||||||
github.com/kr/text v0.2.0 // indirect
|
github.com/google/gnostic v0.7.0 // indirect
|
||||||
golang.org/x/mod v0.12.0 // indirect
|
github.com/google/gnostic-models v0.6.9 // indirect
|
||||||
golang.org/x/sys v0.10.0 // indirect
|
github.com/kr/pretty v0.3.1 // indirect
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
|
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||||
|
github.com/rogpeppe/go-internal v1.13.1 // indirect
|
||||||
|
golang.org/x/mod v0.22.0 // indirect
|
||||||
|
golang.org/x/sync v0.10.0 // indirect
|
||||||
|
golang.org/x/text v0.21.0 // indirect
|
||||||
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 // indirect
|
||||||
|
google.golang.org/grpc v1.68.1 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
2
http.go
2
http.go
@ -41,7 +41,7 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genC
|
|||||||
g.generateServiceEndpoints(gfile, service, component)
|
g.generateServiceEndpoints(gfile, service, component)
|
||||||
if genClient {
|
if genClient {
|
||||||
g.generateServiceClient(gfile, file, service)
|
g.generateServiceClient(gfile, file, service)
|
||||||
g.generateServiceClientMethods(gfile, service, component)
|
g.generateServiceClientMethods(gfile, file, service, component)
|
||||||
}
|
}
|
||||||
if genServer {
|
if genServer {
|
||||||
g.generateServiceServer(gfile, file, service)
|
g.generateServiceServer(gfile, file, service)
|
||||||
|
2
rpc.go
2
rpc.go
@ -38,7 +38,7 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genCl
|
|||||||
for _, service := range file.Services {
|
for _, service := range file.Services {
|
||||||
if genClient {
|
if genClient {
|
||||||
g.generateServiceClient(gfile, file, service)
|
g.generateServiceClient(gfile, file, service)
|
||||||
g.generateServiceClientMethods(gfile, service, component)
|
g.generateServiceClientMethods(gfile, file, service, component)
|
||||||
}
|
}
|
||||||
if genServer {
|
if genServer {
|
||||||
g.generateServiceServer(gfile, file, service)
|
g.generateServiceServer(gfile, file, service)
|
||||||
|
52
util.go
52
util.go
@ -51,14 +51,14 @@ func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *p
|
|||||||
gfile.P()
|
gfile.P()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protogen.Service, component string) {
|
func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile, file *protogen.File, service *protogen.Service, component string) {
|
||||||
serviceName := service.GoName
|
serviceName := service.GoName
|
||||||
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" {
|
||||||
methodName = fmt.Sprintf("%s.%s", method.Parent.Desc.FullName(), method.Desc.Name())
|
methodName = fmt.Sprintf("%s.%s", method.Parent.Desc.FullName(), method.Desc.Name())
|
||||||
}
|
}
|
||||||
g.generateClientFuncSignature(gfile, serviceName, method)
|
g.generateClientFuncSignature(gfile, serviceName, file, method)
|
||||||
|
|
||||||
if component == "http" && method.Desc.Options() != nil {
|
if component == "http" && method.Desc.Options() != nil {
|
||||||
if proto.HasExtension(method.Desc.Options(), v2.E_Openapiv2Operation) {
|
if proto.HasExtension(method.Desc.Options(), v2.E_Openapiv2Operation) {
|
||||||
@ -262,7 +262,7 @@ func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile,
|
|||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
}
|
}
|
||||||
|
|
||||||
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
if method.Desc.IsStreamingClient() /*&& !method.Desc.IsStreamingServer()*/ {
|
||||||
gfile.P("func (s *", unexport(serviceName), "Client", method.GoName, ") CloseAndRecv() (*", gfile.QualifiedGoIdent(method.Output.GoIdent), ", error) {")
|
gfile.P("func (s *", unexport(serviceName), "Client", method.GoName, ") CloseAndRecv() (*", gfile.QualifiedGoIdent(method.Output.GoIdent), ", error) {")
|
||||||
gfile.P("msg := &", gfile.QualifiedGoIdent(method.Output.GoIdent), "{}")
|
gfile.P("msg := &", gfile.QualifiedGoIdent(method.Output.GoIdent), "{}")
|
||||||
gfile.P("err := s.RecvMsg(msg)")
|
gfile.P("err := s.RecvMsg(msg)")
|
||||||
@ -432,7 +432,7 @@ func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile,
|
|||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
}
|
}
|
||||||
|
|
||||||
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
if /*method.Desc.IsStreamingClient() && !*/ method.Desc.IsStreamingServer() {
|
||||||
gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) SendAndClose(msg *", gfile.QualifiedGoIdent(method.Output.GoIdent), ") error {")
|
gfile.P("func (s *", unexport(serviceName), method.GoName, "Stream) SendAndClose(msg *", gfile.QualifiedGoIdent(method.Output.GoIdent), ") error {")
|
||||||
gfile.P("err := s.SendMsg(msg)")
|
gfile.P("err := s.SendMsg(msg)")
|
||||||
gfile.P("if err == nil {")
|
gfile.P("if err == nil {")
|
||||||
@ -493,23 +493,24 @@ func (g *Generator) generateServiceRegister(gfile *protogen.GeneratedFile, file
|
|||||||
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {")
|
gfile.P("func Register", serviceName, "Server(s ", microServerPackage.Ident("Server"), ", sh ", serviceName, "Server, opts ...", microServerPackage.Ident("HandlerOption"), ") error {")
|
||||||
}
|
}
|
||||||
gfile.P("type ", unexport(serviceName), " interface {")
|
gfile.P("type ", unexport(serviceName), " interface {")
|
||||||
|
var generate bool
|
||||||
for _, method := range service.Methods {
|
for _, method := range service.Methods {
|
||||||
generateServerSignature(gfile, serviceName, method, true)
|
generateServerSignature(gfile, serviceName, method, true)
|
||||||
|
if endpoints, _ := generateEndpoints(method); endpoints != nil {
|
||||||
|
generate = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
gfile.P("type ", serviceName, " struct {")
|
gfile.P("type ", serviceName, " struct {")
|
||||||
gfile.P(unexport(serviceName))
|
gfile.P(unexport(serviceName))
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
gfile.P("h := &", unexport(serviceName), "Server{sh}")
|
gfile.P("h := &", unexport(serviceName), "Server{sh}")
|
||||||
gfile.P("var nopts []", microServerPackage.Ident("HandlerOption"))
|
|
||||||
if component == "http" {
|
if component == "http" && generate {
|
||||||
// if g.standalone {
|
gfile.P("opts = append(opts, ", microServerHttpPackage.Ident("HandlerEndpoints"), "(", serviceName, "ServerEndpoints))")
|
||||||
// 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(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))")
|
|
||||||
|
gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, opts...))")
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -557,7 +558,7 @@ func generateServerSignature(gfile *protogen.GeneratedFile, serviceName string,
|
|||||||
gfile.P(args...)
|
gfile.P(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Generator) generateClientFuncSignature(gfile *protogen.GeneratedFile, serviceName string, method *protogen.Method) {
|
func (g *Generator) generateClientFuncSignature(gfile *protogen.GeneratedFile, serviceName string, file *protogen.File, method *protogen.Method) {
|
||||||
args := append([]interface{}{},
|
args := append([]interface{}{},
|
||||||
"func (c *",
|
"func (c *",
|
||||||
unexport(serviceName),
|
unexport(serviceName),
|
||||||
@ -572,7 +573,8 @@ func (g *Generator) generateClientFuncSignature(gfile *protogen.GeneratedFile, s
|
|||||||
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
||||||
args = append(args, "*", gfile.QualifiedGoIdent(method.Output.GoIdent))
|
args = append(args, "*", gfile.QualifiedGoIdent(method.Output.GoIdent))
|
||||||
} else {
|
} else {
|
||||||
args = append(args, serviceName, "_", method.GoName, "Client")
|
// TODO
|
||||||
|
args = append(args, gfile.QualifiedGoIdent(protogen.GoIdent{GoName: serviceName + "_" + method.GoName + "Client", GoImportPath: file.GoImportPath}))
|
||||||
}
|
}
|
||||||
args = append(args, ", error) {")
|
args = append(args, ", error) {")
|
||||||
gfile.P(args...)
|
gfile.P(args...)
|
||||||
@ -627,7 +629,7 @@ func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.Generat
|
|||||||
gfile.P("Context() ", contextPackage.Ident("Context"))
|
gfile.P("Context() ", contextPackage.Ident("Context"))
|
||||||
gfile.P("SendMsg(msg interface{}) error")
|
gfile.P("SendMsg(msg interface{}) error")
|
||||||
gfile.P("RecvMsg(msg interface{}) error")
|
gfile.P("RecvMsg(msg interface{}) error")
|
||||||
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
if method.Desc.IsStreamingClient() /*&& !method.Desc.IsStreamingServer()*/ {
|
||||||
gfile.P("CloseAndRecv() (*", gfile.QualifiedGoIdent(method.Output.GoIdent), ", error)")
|
gfile.P("CloseAndRecv() (*", gfile.QualifiedGoIdent(method.Output.GoIdent), ", error)")
|
||||||
gfile.P("CloseSend() error")
|
gfile.P("CloseSend() error")
|
||||||
}
|
}
|
||||||
@ -655,9 +657,9 @@ func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.Generat
|
|||||||
gfile.P("Context() ", contextPackage.Ident("Context"))
|
gfile.P("Context() ", contextPackage.Ident("Context"))
|
||||||
gfile.P("SendMsg(msg interface{}) error")
|
gfile.P("SendMsg(msg interface{}) error")
|
||||||
gfile.P("RecvMsg(msg interface{}) error")
|
gfile.P("RecvMsg(msg interface{}) error")
|
||||||
if method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
|
if /*method.Desc.IsStreamingClient() && !*/ method.Desc.IsStreamingServer() {
|
||||||
gfile.P("SendAndClose(msg *", gfile.QualifiedGoIdent(method.Output.GoIdent), ") error")
|
gfile.P("SendAndClose(msg *", gfile.QualifiedGoIdent(method.Output.GoIdent), ") error")
|
||||||
gfile.P("CloseSend() error")
|
// gfile.P("CloseSend() error")
|
||||||
}
|
}
|
||||||
gfile.P("Close() error")
|
gfile.P("Close() error")
|
||||||
if method.Desc.IsStreamingClient() {
|
if method.Desc.IsStreamingClient() {
|
||||||
@ -888,14 +890,18 @@ func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, serv
|
|||||||
if component != "http" {
|
if component != "http" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
serviceName := service.GoName
|
|
||||||
|
|
||||||
gfile.P("var (")
|
var generate bool
|
||||||
gfile.P(serviceName, "ServerEndpoints = []", microServerHttpPackage.Ident("EndpointMetadata"), "{")
|
serviceName := service.GoName
|
||||||
|
|
||||||
for _, method := range service.Methods {
|
for _, method := range service.Methods {
|
||||||
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
|
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
|
||||||
if endpoints, streaming := generateEndpoints(method); endpoints != nil {
|
if endpoints, streaming := generateEndpoints(method); endpoints != nil {
|
||||||
|
if !generate {
|
||||||
|
gfile.P("var (")
|
||||||
|
gfile.P(serviceName, "ServerEndpoints = []", microServerHttpPackage.Ident("EndpointMetadata"), "{")
|
||||||
|
generate = true
|
||||||
|
}
|
||||||
for _, ep := range endpoints {
|
for _, ep := range endpoints {
|
||||||
epath, emethod, ebody := getEndpoint(ep)
|
epath, emethod, ebody := getEndpoint(ep)
|
||||||
gfile.P("{")
|
gfile.P("{")
|
||||||
@ -910,8 +916,10 @@ func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, serv
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gfile.P("}")
|
if generate {
|
||||||
gfile.P(")")
|
gfile.P("}")
|
||||||
|
gfile.P(")")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Generator) writeErrors(plugin *protogen.Plugin) error {
|
func (g *Generator) writeErrors(plugin *protogen.Plugin) error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user