Compare commits

...

9 Commits

Author SHA1 Message Date
091e964bd4 resurrect old standalone detection
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-05-06 17:45:22 +03:00
25bf4f9483 move backward for v3 => v4 gen
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-04-24 12:09:00 +03:00
3d92ee0003 standalone checks
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-03-01 13:36:44 +03:00
750c949723 fix codegen for streams
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-01-09 18:00:13 +03:00
4633928da5 update deps
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-12-13 16:13:51 +03:00
5ecd91182f Merge pull request 'graphql' (#91) from graphql into v3
Reviewed-on: #91
2024-06-11 20:46:55 +03:00
a8b52d800d Merge branch 'v3' into graphql 2024-06-11 20:46:45 +03:00
9c05b6b646 Merge pull request 'fix gen' (#89) from fixu into v3
Reviewed-on: #89
2023-12-26 00:56:08 +03:00
675ca27dbf fix gen
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-12-26 00:55:09 +03:00
16 changed files with 1473 additions and 104 deletions

View File

@@ -23,4 +23,4 @@ $> protoc --go_micro_out=debug=true,components="micro|http":. input.proto
* Install the **go** compiler and tools from https://golang.org/doc/install
* Install **protoc-gen-go**: `go install google.golang.org/protobuf/cmd/protoc-gen-go`
* Install **protoc-gen-go-micro**: `go install go.unistack.org/protoc-gen-go-micro/v3`
* Install **protoc-gen-go-micro**: `go install go.unistack.org/protoc-gen-go-micro/v4`

2
ast.go
View File

@@ -10,7 +10,7 @@ import (
"strings"
"github.com/fatih/structtag"
tag_options "go.unistack.org/micro-proto/v3/tag"
tag_options "go.unistack.org/micro-proto/v4/tag"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
)

View File

@@ -2,7 +2,7 @@ syntax = "proto3";
package example;
option go_package = "github.com/unistack-org/protoc-gen-go-micro/v3/example;examplepb";
option go_package = "github.com/unistack-org/protoc-gen-go-micro/v4/example;examplepb";
import "tag/tag.proto";
import "api/annotations.proto";

View File

@@ -1,3 +1,3 @@
package main
//go:generate sh -xc "protoc -I./example -I. -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v3) --go-micro_out=components=graphqls,graphql_file=./schema.graphql:./example example/example.proto"
//go:generate sh -xc "protoc -I./example -I. -I$(go list -f '{{ .Dir }}' -m go.unistack.org/micro-proto/v4) --go-micro_out=components=graphqls,graphql_file=./schema.graphql:./example example/example.proto"

37
go.mod
View File

@@ -1,24 +1,31 @@
module go.unistack.org/protoc-gen-go-micro/v3
module go.unistack.org/protoc-gen-go-micro/v4
go 1.20
go 1.22.0
toolchain go1.23.4
require (
github.com/danielvladco/go-proto-gql v0.10.0
github.com/fatih/structtag v1.2.0
github.com/jhump/protoreflect v1.7.1
github.com/vektah/gqlparser/v2 v2.5.1
go.unistack.org/micro-proto/v3 v3.4.1
golang.org/x/tools v0.11.0
google.golang.org/protobuf v1.28.1
github.com/jhump/protoreflect v1.17.0
github.com/vektah/gqlparser/v2 v2.5.20
go.unistack.org/micro-proto/v4 v4.1.0
golang.org/x/tools v0.28.0
google.golang.org/protobuf v1.35.2
)
require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/kr/text v0.2.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
github.com/bufbuild/protocompile v0.14.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic v0.7.0 // indirect
github.com/google/gnostic-models v0.6.9 // indirect
github.com/kr/pretty v0.3.1 // 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
)

1441
go.sum

File diff suppressed because it is too large Load Diff

View File

@@ -10,7 +10,7 @@ import (
"google.golang.org/protobuf/compiler/protogen"
descriptor "google.golang.org/protobuf/types/descriptorpb"
gqlpb "go.unistack.org/micro-proto/v3/graphql"
gqlpb "go.unistack.org/micro-proto/v4/graphql"
)
const (

View File

@@ -10,7 +10,7 @@ import (
descriptor "google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/pluginpb"
gqlpb "go.unistack.org/micro-proto/v3/graphql"
gqlpb "go.unistack.org/micro-proto/v4/graphql"
)
func GraphqlMethodOptions(opts proto.Message) *gqlpb.Rpc {

View File

@@ -5,7 +5,7 @@ import (
"os"
"github.com/vektah/gqlparser/v2/formatter"
generator "go.unistack.org/protoc-gen-go-micro/v3/graphql"
generator "go.unistack.org/protoc-gen-go-micro/v4/graphql"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"

View File

@@ -41,7 +41,7 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genC
g.generateServiceEndpoints(gfile, service, component)
if genClient {
g.generateServiceClient(gfile, file, service)
g.generateServiceClientMethods(gfile, service, component)
g.generateServiceClientMethods(gfile, file, service, component)
}
if genServer {
g.generateServiceServer(gfile, file, service)

View File

@@ -24,8 +24,8 @@ import (
"strings"
"sync"
"go.unistack.org/micro-proto/v3/api"
v3 "go.unistack.org/micro-proto/v3/openapiv3"
"go.unistack.org/micro-proto/v4/api"
v3 "go.unistack.org/micro-proto/v4/openapiv3"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"

View File

@@ -1,6 +1,6 @@
package main
import v3 "go.unistack.org/micro-proto/v3/openapiv3"
import v3 "go.unistack.org/micro-proto/v4/openapiv3"
func getMediaType(eopt interface{}) string {
ctype := "application/json"

2
rpc.go
View File

@@ -38,7 +38,7 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genCl
for _, service := range file.Services {
if genClient {
g.generateServiceClient(gfile, file, service)
g.generateServiceClientMethods(gfile, service, component)
g.generateServiceClientMethods(gfile, file, service, component)
}
if genServer {
g.generateServiceServer(gfile, file, service)

View File

@@ -3,5 +3,5 @@
package tools
import (
_ "go.unistack.org/micro-proto/v3/graphql"
_ "go.unistack.org/micro-proto/v4/graphql"
)

59
util.go
View File

@@ -8,9 +8,9 @@ import (
"strings"
"time"
api_options "go.unistack.org/micro-proto/v3/api"
v2 "go.unistack.org/micro-proto/v3/openapiv2"
v3 "go.unistack.org/micro-proto/v3/openapiv3"
api_options "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"
)
@@ -51,14 +51,14 @@ func (g *Generator) generateServiceClient(gfile *protogen.GeneratedFile, file *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
for _, method := range service.Methods {
methodName := fmt.Sprintf("%s.%s", serviceName, method.GoName)
if component == "drpc" {
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 proto.HasExtension(method.Desc.Options(), v2.E_Openapiv2Operation) {
@@ -262,7 +262,7 @@ func (g *Generator) generateServiceClientMethods(gfile *protogen.GeneratedFile,
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("msg := &", gfile.QualifiedGoIdent(method.Output.GoIdent), "{}")
gfile.P("err := s.RecvMsg(msg)")
@@ -432,7 +432,7 @@ func (g *Generator) generateServiceServerMethods(gfile *protogen.GeneratedFile,
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("err := s.SendMsg(msg)")
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("type ", unexport(serviceName), " interface {")
var generate bool
for _, method := range service.Methods {
generateServerSignature(gfile, serviceName, method, true)
if endpoints, _ := generateEndpoints(method); endpoints != nil {
generate = true
}
}
gfile.P("}")
gfile.P("type ", serviceName, " struct {")
gfile.P(unexport(serviceName))
gfile.P("}")
gfile.P("h := &", unexport(serviceName), "Server{sh}")
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))")
// }
if component == "http" && generate {
gfile.P("opts = append(opts, ", 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("}")
}
@@ -557,7 +558,7 @@ func generateServerSignature(gfile *protogen.GeneratedFile, serviceName string,
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{}{},
"func (c *",
unexport(serviceName),
@@ -572,7 +573,9 @@ func (g *Generator) generateClientFuncSignature(gfile *protogen.GeneratedFile, s
if !method.Desc.IsStreamingClient() && !method.Desc.IsStreamingServer() {
args = append(args, "*", gfile.QualifiedGoIdent(method.Output.GoIdent))
} else {
args = append(args, serviceName, "_", method.GoName, "Client")
// 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")
}
args = append(args, ", error) {")
gfile.P(args...)
@@ -627,7 +630,7 @@ func (g *Generator) generateServiceClientStreamInterface(gfile *protogen.Generat
gfile.P("Context() ", contextPackage.Ident("Context"))
gfile.P("SendMsg(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("CloseSend() error")
}
@@ -655,9 +658,9 @@ func (g *Generator) generateServiceServerStreamInterface(gfile *protogen.Generat
gfile.P("Context() ", contextPackage.Ident("Context"))
gfile.P("SendMsg(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("CloseSend() error")
// gfile.P("CloseSend() error")
}
gfile.P("Close() error")
if method.Desc.IsStreamingClient() {
@@ -888,14 +891,18 @@ func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, serv
if component != "http" {
return
}
serviceName := service.GoName
gfile.P("var (")
gfile.P(serviceName, "ServerEndpoints = []", microServerHttpPackage.Ident("EndpointMetadata"), "{")
var generate bool
serviceName := service.GoName
for _, method := range service.Methods {
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
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 {
epath, emethod, ebody := getEndpoint(ep)
gfile.P("{")
@@ -910,8 +917,10 @@ func (g *Generator) generateServiceEndpoints(gfile *protogen.GeneratedFile, serv
}
}
gfile.P("}")
gfile.P(")")
if generate {
gfile.P("}")
gfile.P(")")
}
}
func (g *Generator) writeErrors(plugin *protogen.Plugin) error {

View File

@@ -13,13 +13,13 @@ var (
gorillaMuxPackage = protogen.GoImportPath("github.com/gorilla/mux")
chiPackage = protogen.GoImportPath("github.com/go-chi/chi/v5")
chiMiddlewarePackage = protogen.GoImportPath("github.com/go-chi/chi/v5/middleware")
microMetadataPackage = protogen.GoImportPath("go.unistack.org/micro/v3/metadata")
microClientPackage = protogen.GoImportPath("go.unistack.org/micro/v3/client")
microServerPackage = protogen.GoImportPath("go.unistack.org/micro/v3/server")
microClientHttpPackage = protogen.GoImportPath("go.unistack.org/micro-client-http/v3")
microServerHttpPackage = protogen.GoImportPath("go.unistack.org/micro-server-http/v3")
microCodecPackage = protogen.GoImportPath("go.unistack.org/micro-proto/v3/codec")
microErrorsPackage = protogen.GoImportPath("go.unistack.org/micro/v3/errors")
microMetadataPackage = protogen.GoImportPath("go.unistack.org/micro/v4/metadata")
microClientPackage = protogen.GoImportPath("go.unistack.org/micro/v4/client")
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")
microErrorsPackage = protogen.GoImportPath("go.unistack.org/micro/v4/errors")
grpcPackage = protogen.GoImportPath("google.golang.org/grpc")
protojsonPackage = protogen.GoImportPath("google.golang.org/protobuf/encoding/protojson")
timePackage = protogen.GoImportPath("time")