Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
dd62c380f2 | |||
e4f2867419 | |||
c5f68efd4c | |||
140c5fb90a | |||
b4231422b3 | |||
dd872a03b3 | |||
0e902b1022 | |||
81bbbf55e6 | |||
c6caa0d3ac |
@@ -1,4 +1,4 @@
|
|||||||
# `protoc-gen-micro`
|
# `protoc-gen-go-micro`
|
||||||
protobuf plugin to generate helper code for micro framework
|
protobuf plugin to generate helper code for micro framework
|
||||||
|
|
||||||
A generic **code**/script/data generator based on [Protobuf](https://developers.google.com/protocol-buffers/).
|
A generic **code**/script/data generator based on [Protobuf](https://developers.google.com/protocol-buffers/).
|
||||||
@@ -23,4 +23,4 @@ $> protoc --micro_out=debug=true,components="micro|http":. input.proto
|
|||||||
|
|
||||||
* Install the **go** compiler and tools from https://golang.org/doc/install
|
* Install the **go** compiler and tools from https://golang.org/doc/install
|
||||||
* Install **protoc-gen-go**: `go get google.golang.org/protobuf/cmd/protoc-gen-go`
|
* Install **protoc-gen-go**: `go get google.golang.org/protobuf/cmd/protoc-gen-go`
|
||||||
* Install **protoc-gen-micro**: `go get github.com/unistack-org/protoc-gen-micro/v3`
|
* Install **protoc-gen-go-micro**: `go get github.com/unistack-org/protoc-gen-go-micro/v3`
|
||||||
|
5
ast.go
5
ast.go
@@ -44,7 +44,10 @@ func (g *Generator) astGenerate(plugin *protogen.Plugin) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
mp = make(map[string]map[string]*structtag.Tags)
|
mp = make(map[string]map[string]*structtag.Tags)
|
||||||
}
|
}
|
||||||
nmp := make(map[string]*structtag.Tags)
|
nmp, ok := mp[message.GoIdent.GoName]
|
||||||
|
if !ok {
|
||||||
|
nmp = make(map[string]*structtag.Tags)
|
||||||
|
}
|
||||||
tags, err := structtag.Parse(opts.(string))
|
tags, err := structtag.Parse(opts.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
4
chi.go
4
chi.go
@@ -30,7 +30,9 @@ func (g *Generator) chiGenerate(component string, plugin *protogen.Plugin) error
|
|||||||
}
|
}
|
||||||
gfile := plugin.NewGeneratedFile(gname, path)
|
gfile := plugin.NewGeneratedFile(gname, path)
|
||||||
|
|
||||||
gfile.P("// Code generated by protoc-gen-micro")
|
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 ", file.GoPackageName)
|
gfile.P("package ", file.GoPackageName)
|
||||||
gfile.P()
|
gfile.P()
|
||||||
|
|
||||||
|
@@ -2,7 +2,7 @@ syntax = "proto3";
|
|||||||
|
|
||||||
package example;
|
package example;
|
||||||
|
|
||||||
option go_package = "github.com/unistack-org/protoc-gen-micro/v3/example;examplepb";
|
option go_package = "github.com/unistack-org/protoc-gen-go-micro/v3/example;examplepb";
|
||||||
|
|
||||||
import "tag/tag.proto";
|
import "tag/tag.proto";
|
||||||
import "api/annotations.proto";
|
import "api/annotations.proto";
|
||||||
|
8
go.mod
8
go.mod
@@ -1,12 +1,10 @@
|
|||||||
module github.com/unistack-org/protoc-gen-micro/v3
|
module github.com/unistack-org/protoc-gen-go-micro/v3
|
||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/fatih/structtag v1.2.0
|
github.com/fatih/structtag v1.2.0
|
||||||
github.com/unistack-org/micro-proto v0.0.2
|
github.com/unistack-org/micro-proto v0.0.8
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
google.golang.org/protobuf v1.26.0
|
google.golang.org/protobuf v1.27.1
|
||||||
)
|
)
|
||||||
|
|
||||||
//replace github.com/unistack-org/micro-proto => ../micro-proto
|
|
||||||
|
8
go.sum
8
go.sum
@@ -26,8 +26,8 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
|
|||||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
github.com/google/go-cmp v0.5.5/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/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||||
github.com/unistack-org/micro-proto v0.0.2 h1:mL1ZPRGPCWJOiMBiJrkk8Y513rPrJmhJWxyLceckAXU=
|
github.com/unistack-org/micro-proto v0.0.8 h1:g4UZGQGeYGI3CFJtjuEm47aouYPviG8SDhSifl0831w=
|
||||||
github.com/unistack-org/micro-proto v0.0.2/go.mod h1:GYO53DWmeldRIo90cAdQx8bLr/WJMxW62W4ja74p1Ac=
|
github.com/unistack-org/micro-proto v0.0.8/go.mod h1:GYO53DWmeldRIo90cAdQx8bLr/WJMxW62W4ja74p1Ac=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
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/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=
|
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||||
@@ -69,7 +69,7 @@ google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2
|
|||||||
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/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/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
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-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||||
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
|
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
|
||||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
google.golang.org/protobuf v1.27.1/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-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
@@ -30,7 +30,9 @@ func (g *Generator) gorillaGenerate(component string, plugin *protogen.Plugin) e
|
|||||||
}
|
}
|
||||||
gfile := plugin.NewGeneratedFile(gname, path)
|
gfile := plugin.NewGeneratedFile(gname, path)
|
||||||
|
|
||||||
gfile.P("// Code generated by protoc-gen-micro")
|
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 ", file.GoPackageName)
|
gfile.P("package ", file.GoPackageName)
|
||||||
gfile.P()
|
gfile.P()
|
||||||
|
|
||||||
|
4
http.go
4
http.go
@@ -20,8 +20,10 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin, genC
|
|||||||
}
|
}
|
||||||
gfile := plugin.NewGeneratedFile(gname, path)
|
gfile := plugin.NewGeneratedFile(gname, path)
|
||||||
|
|
||||||
gfile.P("// Code generated by protoc-gen-micro")
|
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("// source: ", file.Proto.GetName())
|
||||||
|
gfile.P()
|
||||||
gfile.P("package ", file.GoPackageName)
|
gfile.P("package ", file.GoPackageName)
|
||||||
gfile.P()
|
gfile.P()
|
||||||
|
|
||||||
|
4
micro.go
4
micro.go
@@ -21,8 +21,10 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin, gen
|
|||||||
}
|
}
|
||||||
gfile := plugin.NewGeneratedFile(gname, path)
|
gfile := plugin.NewGeneratedFile(gname, path)
|
||||||
|
|
||||||
gfile.P("// Code generated by protoc-gen-micro")
|
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("// source: ", file.Proto.GetName())
|
||||||
|
gfile.P()
|
||||||
gfile.P("package ", file.GoPackageName)
|
gfile.P("package ", file.GoPackageName)
|
||||||
gfile.P()
|
gfile.P()
|
||||||
|
|
||||||
|
4
rpc.go
4
rpc.go
@@ -20,8 +20,10 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin, genCl
|
|||||||
}
|
}
|
||||||
gfile := plugin.NewGeneratedFile(gname, path)
|
gfile := plugin.NewGeneratedFile(gname, path)
|
||||||
|
|
||||||
gfile.P("// Code generated by protoc-gen-micro")
|
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("// source: ", file.Proto.GetName())
|
||||||
|
gfile.P()
|
||||||
gfile.P("package ", file.GoPackageName)
|
gfile.P("package ", file.GoPackageName)
|
||||||
gfile.P()
|
gfile.P()
|
||||||
|
|
||||||
|
27
util.go
27
util.go
@@ -55,14 +55,14 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
|
|||||||
opts := proto.GetExtension(method.Desc.Options(), openapiv2_options.E_Openapiv2Operation)
|
opts := proto.GetExtension(method.Desc.Options(), openapiv2_options.E_Openapiv2Operation)
|
||||||
if opts != nil {
|
if opts != nil {
|
||||||
r := opts.(*openapiv2_options.Operation)
|
r := opts.(*openapiv2_options.Operation)
|
||||||
gfile.P("errmap := make(map[string]interface{}, ", len(r.Responses), ")")
|
gfile.P("errmap := make(map[string]interface{}, ", len(r.Responses.ResponseCode), ")")
|
||||||
for code, response := range r.Responses {
|
for _, rsp := range r.Responses.ResponseCode {
|
||||||
if response.Schema != nil && response.Schema.JsonSchema != nil {
|
if schema := rsp.Value.GetJsonReference(); schema != nil {
|
||||||
ref := response.Schema.JsonSchema.Ref
|
ref := schema.XRef
|
||||||
if strings.HasPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".") {
|
if strings.HasPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".") {
|
||||||
ref = strings.TrimPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".")
|
ref = strings.TrimPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".")
|
||||||
}
|
}
|
||||||
gfile.P(`errmap["`, code, `"] = &`, ref, "{}")
|
gfile.P(`errmap["`, rsp.Name, `"] = &`, ref, "{}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -284,8 +284,8 @@ func generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Se
|
|||||||
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"))
|
gfile.P("var nopts []", microServerPackage.Ident("HandlerOption"))
|
||||||
gfile.P("for _, endpoint := range New", serviceName, "Endpoints() {")
|
gfile.P("for _, endpoint := range ", serviceName, "Endpoints {")
|
||||||
gfile.P("nopts = append(nopts, ", microApiPackage.Ident("WithEndpoint"), "(endpoint))")
|
gfile.P("nopts = append(nopts, ", microApiPackage.Ident("WithEndpoint"), "(&endpoint))")
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))")
|
gfile.P("return s.Handle(s.NewHandler(&", serviceName, "{h}, append(nopts, opts...)...))")
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
@@ -448,8 +448,10 @@ func generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service
|
|||||||
|
|
||||||
func generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
func generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
||||||
serviceName := service.GoName
|
serviceName := service.GoName
|
||||||
gfile.P("func New", serviceName, "Endpoints() []*", microApiPackage.Ident("Endpoint"), " {")
|
gfile.P("var (")
|
||||||
gfile.P("return []*", microApiPackage.Ident("Endpoint"), "{")
|
gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`)
|
||||||
|
gfile.P()
|
||||||
|
gfile.P(serviceName, "Endpoints", "=", "[]", microApiPackage.Ident("Endpoint"), "{")
|
||||||
for _, method := range service.Methods {
|
for _, method := range service.Methods {
|
||||||
if method.Desc.Options() == nil {
|
if method.Desc.Options() == nil {
|
||||||
continue
|
continue
|
||||||
@@ -457,13 +459,18 @@ func generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.S
|
|||||||
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
|
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
|
||||||
endpoints, streaming := generateEndpoints(method)
|
endpoints, streaming := generateEndpoints(method)
|
||||||
for _, endpoint := range endpoints {
|
for _, endpoint := range endpoints {
|
||||||
gfile.P("&", microApiPackage.Ident("Endpoint"), "{")
|
gfile.P("{")
|
||||||
generateEndpoint(gfile, serviceName, method.GoName, endpoint, streaming)
|
generateEndpoint(gfile, serviceName, method.GoName, endpoint, streaming)
|
||||||
gfile.P("},")
|
gfile.P("},")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
|
gfile.P(")")
|
||||||
|
gfile.P()
|
||||||
|
|
||||||
|
gfile.P("func New", serviceName, "Endpoints()", "[]", microApiPackage.Ident("Endpoint"), "{")
|
||||||
|
gfile.P("return ", serviceName, "Endpoints")
|
||||||
gfile.P("}")
|
gfile.P("}")
|
||||||
gfile.P()
|
gfile.P()
|
||||||
}
|
}
|
||||||
|
@@ -17,4 +17,5 @@ var (
|
|||||||
microClientHttpPackage = protogen.GoImportPath("github.com/unistack-org/micro-client-http/v3")
|
microClientHttpPackage = protogen.GoImportPath("github.com/unistack-org/micro-client-http/v3")
|
||||||
timePackage = protogen.GoImportPath("time")
|
timePackage = protogen.GoImportPath("time")
|
||||||
deprecationComment = "// Deprecated: Do not use."
|
deprecationComment = "// Deprecated: Do not use."
|
||||||
|
versionComment = "v3.4.2"
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user