Compare commits

..

9 Commits

Author SHA1 Message Date
f69088bd27 fallback to generate client and server
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-03-30 18:30:58 +03:00
be4eac21df fix example
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-03-26 16:04:08 +03:00
fcd8dd1f7c minor changes for generator
* allow to split client and server generation
* allow to override options for handler

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-03-26 15:00:55 +03:00
c7ec840ac0 remove some runtime allocaions
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-03-23 16:56:11 +03:00
bce3839877 fix http client generation without openapiv2 annotation
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-03-19 13:00:00 +03:00
Renovate Bot
4d2af78ba7 Update module google.golang.org/protobuf to v1.26.0 2021-03-19 03:24:49 +00:00
298f123492 extract variables to dedicated file
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-02-28 23:42:00 +03:00
254448bf74 add client request timeout option support
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-02-28 01:42:29 +03:00
7b349ba1ca update micro-proto
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-02-28 00:38:57 +03:00
8 changed files with 181 additions and 61 deletions

7
go.mod
View File

@@ -3,8 +3,9 @@ module github.com/unistack-org/protoc-gen-micro/v3
go 1.16
require (
github.com/google/go-cmp v0.5.4 // indirect
github.com/unistack-org/micro-proto v0.0.1
github.com/unistack-org/micro-proto v0.0.2-0.20210227213711-77c7563bd01e
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.25.0
google.golang.org/protobuf v1.26.0
)
//replace github.com/unistack-org/micro-proto => ../micro-proto

14
go.sum
View File

@@ -14,18 +14,18 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM=
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/unistack-org/micro-proto v0.0.1 h1:1d9O8d3/ZITQPwIQCvZ+2OCDjVP0iaX+gv5klbwkE/I=
github.com/unistack-org/micro-proto v0.0.1/go.mod h1:GYO53DWmeldRIo90cAdQx8bLr/WJMxW62W4ja74p1Ac=
github.com/unistack-org/micro-proto v0.0.2-0.20210227213711-77c7563bd01e h1:hQJ3V0QggeFdU5967wO5v6oWnaK42wUnG4UU4zWcyu4=
github.com/unistack-org/micro-proto v0.0.2-0.20210227213711-77c7563bd01e/go.mod h1:GYO53DWmeldRIo90cAdQx8bLr/WJMxW62W4ja74p1Ac=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
@@ -65,7 +65,9 @@ google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzi
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

26
http.go
View File

@@ -4,7 +4,7 @@ import (
"google.golang.org/protobuf/compiler/protogen"
)
func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin) error {
func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genClient bool, genServer bool) error {
for _, file := range plugin.Files {
if !file.Generate {
continue
@@ -27,16 +27,24 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin) erro
gfile.Import(contextPackage)
gfile.Import(microApiPackage)
gfile.Import(microClientPackage)
gfile.Import(microClientHttpPackage)
gfile.Import(microServerPackage)
if genClient {
gfile.Import(microClientPackage)
gfile.Import(microClientHttpPackage)
}
if genServer {
gfile.Import(microServerPackage)
}
for _, service := range file.Services {
generateServiceClient(gfile, service)
generateServiceClientMethods(gfile, service, true)
generateServiceServer(gfile, service)
generateServiceServerMethods(gfile, service)
generateServiceRegister(gfile, service)
if genClient {
generateServiceClient(gfile, service)
generateServiceClientMethods(gfile, service, true)
}
if genServer {
generateServiceServer(gfile, service)
generateServiceServerMethods(gfile, service)
generateServiceRegister(gfile, service)
}
}
}

26
main.go
View File

@@ -12,7 +12,7 @@ import (
var (
flagDebug = flag.Bool("debug", false, "")
flagStandalone = flag.Bool("standalone", false, "")
flagComponents = flag.String("components", "micro|rpc", "")
flagComponents = flag.String("components", "micro|rpc|http|client|server", "")
)
func main() {
@@ -39,15 +39,33 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
g.components = *flagComponents
plugin.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
var genClient bool
var genServer bool
if strings.Contains(g.components, "server") {
genServer = true
}
if strings.Contains(g.components, "client") {
genClient = true
}
if strings.Contains(g.components, "rpc") || strings.Contains(g.components, "http") {
if !genServer && !genClient {
genServer = true
genClient = true
}
}
// Protoc passes a slice of File structs for us to process
for _, component := range strings.Split(g.components, "|") {
switch component {
case "server", "client":
continue
case "micro":
err = g.microGenerate(component, plugin)
err = g.microGenerate(component, plugin, genClient, genServer)
case "http":
err = g.httpGenerate(component, plugin)
err = g.httpGenerate(component, plugin, genClient, genServer)
case "grpc", "rpc":
err = g.rpcGenerate("rpc", plugin)
err = g.rpcGenerate("rpc", plugin, genClient, genServer)
case "gorilla":
err = g.gorillaGenerate(component, plugin)
case "chi":

View File

@@ -4,7 +4,7 @@ import (
"google.golang.org/protobuf/compiler/protogen"
)
func (g *Generator) microGenerate(component string, plugin *protogen.Plugin) error {
func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, genClient bool, genServer bool) error {
for _, file := range plugin.Files {
if !file.Generate {
continue
@@ -28,15 +28,20 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin) err
gfile.Import(contextPackage)
gfile.Import(microApiPackage)
gfile.Import(microClientPackage)
if genClient {
gfile.Import(microClientPackage)
}
// generate services
for _, service := range file.Services {
generateServiceEndpoints(gfile, service)
generateServiceClientInterface(gfile, service)
generateServiceClientStreamInterface(gfile, service)
generateServiceServerInterface(gfile, service)
generateServiceServerStreamInterface(gfile, service)
if genClient {
generateServiceClientInterface(gfile, service)
generateServiceClientStreamInterface(gfile, service)
}
if genServer {
generateServiceServerInterface(gfile, service)
generateServiceServerStreamInterface(gfile, service)
}
}
}

25
rpc.go
View File

@@ -4,7 +4,7 @@ import (
"google.golang.org/protobuf/compiler/protogen"
)
func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin) error {
func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genClient bool, genServer bool) error {
for _, file := range plugin.Files {
if !file.Generate {
continue
@@ -27,15 +27,22 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin) error
gfile.Import(contextPackage)
gfile.Import(microApiPackage)
gfile.Import(microClientPackage)
gfile.Import(microServerPackage)
if genClient {
gfile.Import(microClientPackage)
}
if genServer {
gfile.Import(microServerPackage)
}
for _, service := range file.Services {
generateServiceClient(gfile, service)
generateServiceClientMethods(gfile, service, false)
generateServiceServer(gfile, service)
generateServiceServerMethods(gfile, service)
generateServiceRegister(gfile, service)
if genClient {
generateServiceClient(gfile, service)
generateServiceClientMethods(gfile, service, false)
}
if genServer {
generateServiceServer(gfile, service)
generateServiceServerMethods(gfile, service)
generateServiceRegister(gfile, service)
}
}
}

105
util.go
View File

@@ -11,19 +11,17 @@ import (
)
var (
reflectPackage = protogen.GoImportPath("reflect")
stringsPackage = protogen.GoImportPath("strings")
fmtPackage = protogen.GoImportPath("fmt")
contextPackage = protogen.GoImportPath("context")
httpPackage = protogen.GoImportPath("net/http")
gorillaMuxPackage = protogen.GoImportPath("github.com/gorilla/mux")
chiPackage = protogen.GoImportPath("github.com/go-chi/chi/v4")
chiMiddlewarePackage = protogen.GoImportPath("github.com/go-chi/chi/v4/middleware")
microApiPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/api")
microClientPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/client")
microServerPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/server")
microClientHttpPackage = protogen.GoImportPath("github.com/unistack-org/micro-client-http/v3")
deprecationComment = "// Deprecated: Do not use."
httpMethodMap = map[string]string{
"GET": "MethodGet",
"HEAD": "MethodHead",
"POST": "MethodPost",
"PUT": "MethodPut",
"PATCH": "MethodPatch",
"DELETE": "MethodDelete",
"CONNECT": "MethodConnect",
"OPTIONS": "MethodOptions",
"TRACE": "MethodTrace",
}
)
func unexport(s string) string {
@@ -32,6 +30,9 @@ func unexport(s string) string {
func generateServiceClient(gfile *protogen.GeneratedFile, service *protogen.Service) {
serviceName := service.GoName
//if rule, ok := getMicroApiService(service); ok {
// gfile.P("// client wrappers ", strings.Join(rule.ClientWrappers, ", "))
// }
gfile.P("type ", unexport(serviceName), "Client struct {")
gfile.P("c ", microClientPackage.Ident("Client"))
gfile.P("name string")
@@ -68,18 +69,29 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
gfile.P("opts = append(opts,")
gfile.P(microClientHttpPackage.Ident("ErrorMap"), "(errmap),")
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
endpoints, _ := generateEndpoints(method)
path, method, body := getEndpoint(endpoints[0])
gfile.P(")")
}
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
gfile.P("opts = append(opts,")
endpoints, _ := generateEndpoints(method)
path, method, body := getEndpoint(endpoints[0])
if vmethod, ok := httpMethodMap[method]; ok {
gfile.P(microClientHttpPackage.Ident("Method"), `(`, httpPackage.Ident(vmethod), `),`)
} else {
gfile.P(microClientHttpPackage.Ident("Method"), `("`, method, `"),`)
gfile.P(microClientHttpPackage.Ident("Path"), `("`, path, `"),`)
}
gfile.P(microClientHttpPackage.Ident("Path"), `("`, path, `"),`)
if body != "" {
gfile.P(microClientHttpPackage.Ident("Body"), `("`, body, `"),`)
}
gfile.P(")")
}
}
if rule, ok := getMicroApiMethod(method); ok {
if rule.Timeout > 0 {
gfile.P("opts = append(opts, ", microClientPackage.Ident("WithRequestTimeout"), "(", timePackage.Ident("Second"), "*", rule.Timeout, "))")
}
}
if !method.Desc.IsStreamingServer() && !method.Desc.IsStreamingClient() {
gfile.P("rsp := &", gfile.QualifiedGoIdent(method.Output.GoIdent), "{}")
@@ -177,7 +189,13 @@ func generateServiceServerMethods(gfile *protogen.GeneratedFile, service *protog
serviceName := service.GoName
for _, method := range service.Methods {
generateServerFuncSignature(gfile, serviceName, method, true)
if rule, ok := getMicroApiMethod(method); ok {
if rule.Timeout > 0 {
gfile.P("var cancel ", contextPackage.Ident("CancelFunc"))
gfile.P("ctx, cancel = ", contextPackage.Ident("WithTimeout"), "(ctx, ", timePackage.Ident("Second"), "*", rule.Timeout, ")")
gfile.P("defer cancel()")
}
}
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
if !method.Desc.IsStreamingClient() {
gfile.P("msg := &", gfile.QualifiedGoIdent(method.Input.GoIdent), "{}")
@@ -265,10 +283,11 @@ func generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Se
gfile.P(unexport(serviceName))
gfile.P("}")
gfile.P("h := &", unexport(serviceName), "Server{sh}")
gfile.P("var nopts []", microServerPackage.Ident("HandlerOption"))
gfile.P("for _, endpoint := range New", serviceName, "Endpoints() {")
gfile.P("opts = append(opts, ", microApiPackage.Ident("WithEndpoint"), "(endpoint))")
gfile.P("nopts = append(nopts, ", microApiPackage.Ident("WithEndpoint"), "(endpoint))")
gfile.P("}")
gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, opts...))")
gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))")
gfile.P("}")
}
@@ -470,6 +489,42 @@ func generateEndpoints(method *protogen.Method) ([]*api_options.HttpRule, bool)
return rules, method.Desc.IsStreamingServer() || method.Desc.IsStreamingClient()
}
func getMicroApiMethod(method *protogen.Method) (*api_options.MicroMethod, bool) {
if method.Desc.Options() == nil {
return nil, false
}
if !proto.HasExtension(method.Desc.Options(), api_options.E_MicroMethod) {
return nil, false
}
r := proto.GetExtension(method.Desc.Options(), api_options.E_MicroMethod)
if r == nil {
return nil, false
}
rule := r.(*api_options.MicroMethod)
return rule, true
}
func getMicroApiService(service *protogen.Service) (*api_options.MicroService, bool) {
if service.Desc.Options() == nil {
return nil, false
}
if !proto.HasExtension(service.Desc.Options(), api_options.E_MicroService) {
return nil, false
}
r := proto.GetExtension(service.Desc.Options(), api_options.E_MicroService)
if r == nil {
return nil, false
}
rule := r.(*api_options.MicroService)
return rule, true
}
func getEndpoint(rule *api_options.HttpRule) (string, string, string) {
var meth string
var path string
@@ -505,8 +560,12 @@ func generateEndpoint(gfile *protogen.GeneratedFile, serviceName string, methodN
path, meth, body := getEndpoint(rule)
gfile.P("Name:", fmt.Sprintf(`"%s.%s",`, serviceName, methodName))
gfile.P("Path:", fmt.Sprintf(`[]string{"%s"},`, path))
//if vmethod, ok := httpMethodMap[meth]; ok {
// gfile.P("Method:", `[]string{`, httpPackage.Ident(vmethod), `},`)
//} else {
gfile.P("Method:", fmt.Sprintf(`[]string{"%s"},`, meth))
if len(rule.GetGet()) == 0 {
// }
if len(rule.GetGet()) == 0 && body != "" {
gfile.P("Body:", fmt.Sprintf(`"%s",`, body))
}
if streaming {

20
variables.go Normal file
View File

@@ -0,0 +1,20 @@
package main
import "google.golang.org/protobuf/compiler/protogen"
var (
reflectPackage = protogen.GoImportPath("reflect")
stringsPackage = protogen.GoImportPath("strings")
fmtPackage = protogen.GoImportPath("fmt")
contextPackage = protogen.GoImportPath("context")
httpPackage = protogen.GoImportPath("net/http")
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")
microApiPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/api")
microClientPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/client")
microServerPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/server")
microClientHttpPackage = protogen.GoImportPath("github.com/unistack-org/micro-client-http/v3")
timePackage = protogen.GoImportPath("time")
deprecationComment = "// Deprecated: Do not use."
)