Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
5d13b84c7a | |||
ee4d83458f | |||
dd62c380f2 | |||
e4f2867419 | |||
c5f68efd4c | |||
140c5fb90a | |||
b4231422b3 | |||
dd872a03b3 | |||
0e902b1022 | |||
81bbbf55e6 | |||
c6caa0d3ac | |||
5ecc4986dd | |||
9ed1ca9a89 | |||
805b52cf8d | |||
d41fa1a64f | |||
f69088bd27 | |||
be4eac21df | |||
fcd8dd1f7c |
@@ -1,4 +1,4 @@
|
||||
# `protoc-gen-micro`
|
||||
# `protoc-gen-go-micro`
|
||||
protobuf plugin to generate helper code for micro framework
|
||||
|
||||
A generic **code**/script/data generator based on [Protobuf](https://developers.google.com/protocol-buffers/).
|
||||
@@ -15,11 +15,12 @@ $> protoc --micro_out=debug=true,components="micro|http":. input.proto
|
||||
|
||||
| Option | Default Value | Accepted Values | Description
|
||||
|-----------------------|---------------|---------------------------|-----------------------
|
||||
| `tag_path` | `.` | `any local path` | path contains generated protobuf code that needs to be tagged
|
||||
| `debug` | *false* | `true` or `false` | if *true*, `protoc` will generate a more verbose output
|
||||
| `components` | `micro` | `micro rpc http chi gorilla` | some values cant coexists like gorilla/chi or rpc/http, values must be concatinated with pipe symbol
|
||||
| `components` | `micro` | `micro rpc http chi gorilla client server` | some values can't coexists like gorilla/chi or rpc/http, values must be concatinated with pipe symbol
|
||||
|
||||
## 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-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`
|
||||
|
170
ast.go
Normal file
170
ast.go
Normal file
@@ -0,0 +1,170 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/format"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/structtag"
|
||||
tag_options "github.com/unistack-org/micro-proto/tag"
|
||||
"google.golang.org/protobuf/compiler/protogen"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var astFields = make(map[string]map[string]map[string]*structtag.Tags) // map proto file with proto message ast struct
|
||||
|
||||
func (g *Generator) astFill(file *protogen.File, message *protogen.Message) error {
|
||||
for _, field := range message.Fields {
|
||||
if field.Desc.Options() == nil {
|
||||
continue
|
||||
}
|
||||
if !proto.HasExtension(field.Desc.Options(), tag_options.E_Tags) {
|
||||
continue
|
||||
}
|
||||
|
||||
opts := proto.GetExtension(field.Desc.Options(), tag_options.E_Tags)
|
||||
if opts != nil {
|
||||
fpath := filepath.Join(g.tagPath, file.GeneratedFilenamePrefix+".pb.go")
|
||||
mp, ok := astFields[fpath]
|
||||
if !ok {
|
||||
mp = make(map[string]map[string]*structtag.Tags)
|
||||
}
|
||||
nmp, ok := mp[message.GoIdent.GoName]
|
||||
if !ok {
|
||||
nmp = make(map[string]*structtag.Tags)
|
||||
}
|
||||
tags, err := structtag.Parse(opts.(string))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
nmp[field.GoName] = tags
|
||||
mp[message.GoIdent.GoName] = nmp
|
||||
astFields[fpath] = mp
|
||||
}
|
||||
}
|
||||
for _, nmessage := range message.Messages {
|
||||
if err := g.astFill(file, nmessage); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *Generator) astGenerate(plugin *protogen.Plugin) error {
|
||||
if g.tagPath == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, file := range plugin.Files {
|
||||
if !file.Generate {
|
||||
continue
|
||||
}
|
||||
for _, message := range file.Messages {
|
||||
if err := g.astFill(file, message); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for file, mp := range astFields {
|
||||
fset := token.NewFileSet()
|
||||
pf, err := parser.ParseFile(fset, file, nil, parser.AllErrors|parser.ParseComments)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r := retag{}
|
||||
f := func(n ast.Node) ast.Visitor {
|
||||
if r.err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if v, ok := n.(*ast.TypeSpec); ok {
|
||||
r.fields = mp[v.Name.Name]
|
||||
return r
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
ast.Walk(structVisitor{f}, pf)
|
||||
|
||||
if r.err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fp, err := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC, os.FileMode(0644))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = format.Node(fp, fset, pf); err != nil {
|
||||
fp.Close()
|
||||
return err
|
||||
}
|
||||
if err = fp.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type retag struct {
|
||||
err error
|
||||
fields map[string]*structtag.Tags
|
||||
}
|
||||
|
||||
func (v retag) Visit(n ast.Node) ast.Visitor {
|
||||
if v.err != nil {
|
||||
return nil
|
||||
}
|
||||
if f, ok := n.(*ast.Field); ok {
|
||||
if len(f.Names) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
newTags := v.fields[f.Names[0].String()]
|
||||
if newTags == nil {
|
||||
return nil
|
||||
}
|
||||
if f.Tag == nil {
|
||||
f.Tag = &ast.BasicLit{
|
||||
Kind: token.STRING,
|
||||
}
|
||||
}
|
||||
|
||||
oldTags, err := structtag.Parse(strings.Trim(f.Tag.Value, "`"))
|
||||
if err != nil {
|
||||
v.err = err
|
||||
return nil
|
||||
}
|
||||
for _, t := range newTags.Tags() {
|
||||
oldTags.Set(t)
|
||||
}
|
||||
|
||||
f.Tag.Value = "`" + oldTags.String() + "`"
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
type structVisitor struct {
|
||||
visitor func(n ast.Node) ast.Visitor
|
||||
}
|
||||
|
||||
func (v structVisitor) Visit(n ast.Node) ast.Visitor {
|
||||
if tp, ok := n.(*ast.TypeSpec); ok {
|
||||
if _, ok := tp.Type.(*ast.StructType); ok {
|
||||
ast.Walk(v.visitor(n), n)
|
||||
return nil // This will ensure this struct is no longer traversed
|
||||
}
|
||||
}
|
||||
return v
|
||||
}
|
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.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()
|
||||
|
||||
|
44
example/example.proto
Normal file
44
example/example.proto
Normal file
@@ -0,0 +1,44 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package example;
|
||||
|
||||
option go_package = "github.com/unistack-org/protoc-gen-go-micro/v3/example;examplepb";
|
||||
|
||||
import "tag/tag.proto";
|
||||
import "api/annotations.proto";
|
||||
import "openapiv2/annotations.proto";
|
||||
import "google/protobuf/wrappers.proto";
|
||||
|
||||
service Example {
|
||||
rpc Call(CallReq) returns (CallRsp) {
|
||||
option (micro.openapiv2.openapiv2_operation) = {
|
||||
operation_id: "Call";
|
||||
responses: {
|
||||
key: "default";
|
||||
value: {
|
||||
description: "Error response";
|
||||
schema: {
|
||||
json_schema: {
|
||||
ref: ".example.Error";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
option (micro.api.http) = { post: "/v1/example/call/{name}"; body: "*"; };
|
||||
option (micro.api.micro_method) = { timeout: 5; };
|
||||
};
|
||||
};
|
||||
|
||||
message CallReq {
|
||||
string name = 1 [(micro.tag.tags) = "xml:\",attr\"" ];
|
||||
string req = 2;
|
||||
};
|
||||
|
||||
message CallRsp {
|
||||
string rsp = 2;
|
||||
};
|
||||
|
||||
message Error {
|
||||
string msg = 1;
|
||||
};
|
9
go.mod
9
go.mod
@@ -1,11 +1,10 @@
|
||||
module github.com/unistack-org/protoc-gen-micro/v3
|
||||
module github.com/unistack-org/protoc-gen-go-micro/v3
|
||||
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/unistack-org/micro-proto v0.0.2-0.20210227213711-77c7563bd01e
|
||||
github.com/fatih/structtag v1.2.0
|
||||
github.com/unistack-org/micro-proto v0.0.8
|
||||
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
|
||||
|
12
go.sum
12
go.sum
@@ -4,6 +4,8 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/fatih/structtag v1.2.0 h1:/OdNE99OxoI/PqaW/SuSK9uxxT3f/tcSZgon/ssNSx4=
|
||||
github.com/fatih/structtag v1.2.0/go.mod h1:mBJUNpUnHmRKrKlQQlmCrh5PuhftFbNv8Ys4/aAZl94=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
@@ -15,17 +17,17 @@ github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:W
|
||||
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/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.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
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.2-0.20210227213711-77c7563bd01e h1:hQJ3V0QggeFdU5967wO5v6oWnaK42wUnG4UU4zWcyu4=
|
||||
github.com/unistack-org/micro-proto v0.0.2-0.20210227213711-77c7563bd01e/go.mod h1:GYO53DWmeldRIo90cAdQx8bLr/WJMxW62W4ja74p1Ac=
|
||||
github.com/unistack-org/micro-proto v0.0.8 h1:g4UZGQGeYGI3CFJtjuEm47aouYPviG8SDhSifl0831w=
|
||||
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/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=
|
||||
@@ -67,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.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=
|
||||
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
|
||||
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-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.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()
|
||||
|
||||
|
33
http.go
33
http.go
@@ -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
|
||||
@@ -20,23 +20,34 @@ func (g *Generator) httpGenerate(component string, plugin *protogen.Plugin) erro
|
||||
}
|
||||
gfile := plugin.NewGeneratedFile(gname, path)
|
||||
|
||||
gfile.P("// Code generated by protoc-gen-micro")
|
||||
gfile.P("// source: ", *file.Proto.Name)
|
||||
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()
|
||||
gfile.P("package ", file.GoPackageName)
|
||||
gfile.P()
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
34
main.go
34
main.go
@@ -12,7 +12,8 @@ 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", "")
|
||||
flagTagPath = flag.String("tag_path", "", "")
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -29,6 +30,7 @@ type Generator struct {
|
||||
components string
|
||||
standalone bool
|
||||
debug bool
|
||||
tagPath string
|
||||
}
|
||||
|
||||
func (g *Generator) Generate(plugin *protogen.Plugin) error {
|
||||
@@ -37,17 +39,36 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
|
||||
g.standalone = *flagStandalone
|
||||
g.debug = *flagDebug
|
||||
g.components = *flagComponents
|
||||
g.tagPath = *flagTagPath
|
||||
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":
|
||||
@@ -63,5 +84,10 @@ func (g *Generator) Generate(plugin *protogen.Plugin) error {
|
||||
|
||||
}
|
||||
|
||||
if err = g.astGenerate(plugin); err != nil {
|
||||
plugin.Error(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
25
micro.go
25
micro.go
@@ -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
|
||||
@@ -21,22 +21,29 @@ func (g *Generator) microGenerate(component string, plugin *protogen.Plugin) err
|
||||
}
|
||||
gfile := plugin.NewGeneratedFile(gname, path)
|
||||
|
||||
gfile.P("// Code generated by protoc-gen-micro")
|
||||
gfile.P("// source: ", *file.Proto.Name)
|
||||
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()
|
||||
gfile.P("package ", file.GoPackageName)
|
||||
gfile.P()
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
31
rpc.go
31
rpc.go
@@ -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
|
||||
@@ -20,22 +20,31 @@ func (g *Generator) rpcGenerate(component string, plugin *protogen.Plugin) error
|
||||
}
|
||||
gfile := plugin.NewGeneratedFile(gname, path)
|
||||
|
||||
gfile.P("// Code generated by protoc-gen-micro")
|
||||
gfile.P("// source: ", *file.Proto.Name)
|
||||
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()
|
||||
gfile.P("package ", file.GoPackageName)
|
||||
gfile.P()
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
70
util.go
70
util.go
@@ -10,19 +10,17 @@ import (
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
var (
|
||||
httpMethodMap = map[string]string{
|
||||
"GET": "MethodGet",
|
||||
"HEAD": "MethodHead",
|
||||
"POST": "MethodPost",
|
||||
"PUT": "MethodPut",
|
||||
"PATCH": "MethodPatch",
|
||||
"DELETE": "MethodDelete",
|
||||
"CONNECT": "MethodConnect",
|
||||
"OPTIONS": "MethodOptions",
|
||||
"TRACE": "MethodTrace",
|
||||
}
|
||||
)
|
||||
var 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 {
|
||||
return strings.ToLower(s[:1]) + s[1:]
|
||||
@@ -30,7 +28,7 @@ func unexport(s string) string {
|
||||
|
||||
func generateServiceClient(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
||||
serviceName := service.GoName
|
||||
//if rule, ok := getMicroApiService(service); ok {
|
||||
// if rule, ok := getMicroApiService(service); ok {
|
||||
// gfile.P("// client wrappers ", strings.Join(rule.ClientWrappers, ", "))
|
||||
// }
|
||||
gfile.P("type ", unexport(serviceName), "Client struct {")
|
||||
@@ -55,14 +53,18 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
|
||||
opts := proto.GetExtension(method.Desc.Options(), openapiv2_options.E_Openapiv2Operation)
|
||||
if opts != nil {
|
||||
r := opts.(*openapiv2_options.Operation)
|
||||
gfile.P("errmap := make(map[string]interface{}, ", len(r.Responses), ")")
|
||||
for code, response := range r.Responses {
|
||||
if response.Schema != nil && response.Schema.JsonSchema != nil {
|
||||
ref := response.Schema.JsonSchema.Ref
|
||||
gfile.P("errmap := make(map[string]interface{}, ", len(r.Responses.ResponseCode), ")")
|
||||
for _, rsp := range r.Responses.ResponseCode {
|
||||
if schema := rsp.Value.GetJsonReference(); schema != nil {
|
||||
ref := schema.XRef
|
||||
if strings.HasPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".") {
|
||||
ref = strings.TrimPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".")
|
||||
}
|
||||
gfile.P(`errmap["`, code, `"] = &`, ref, "{}")
|
||||
if ref == "micro.codec.Frame" {
|
||||
gfile.P(`errmap["`, rsp.Name, `"] = &`, microCodecPackage.Ident("Frame"), "{}")
|
||||
} else {
|
||||
gfile.P(`errmap["`, rsp.Name, `"] = &`, ref, "{}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -283,10 +285,11 @@ func generateServiceRegister(gfile *protogen.GeneratedFile, service *protogen.Se
|
||||
gfile.P(unexport(serviceName))
|
||||
gfile.P("}")
|
||||
gfile.P("h := &", unexport(serviceName), "Server{sh}")
|
||||
gfile.P("for _, endpoint := range New", serviceName, "Endpoints() {")
|
||||
gfile.P("opts = append(opts, ", microApiPackage.Ident("WithEndpoint"), "(endpoint))")
|
||||
gfile.P("var nopts []", microServerPackage.Ident("HandlerOption"))
|
||||
gfile.P("for _, endpoint := range ", serviceName, "Endpoints {")
|
||||
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("}")
|
||||
}
|
||||
|
||||
@@ -447,8 +450,10 @@ func generateServiceServerStreamInterface(gfile *protogen.GeneratedFile, service
|
||||
|
||||
func generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.Service) {
|
||||
serviceName := service.GoName
|
||||
gfile.P("func New", serviceName, "Endpoints() []*", microApiPackage.Ident("Endpoint"), " {")
|
||||
gfile.P("return []*", microApiPackage.Ident("Endpoint"), "{")
|
||||
gfile.P("var (")
|
||||
gfile.P(serviceName, "Name", "=", `"`, serviceName, `"`)
|
||||
gfile.P()
|
||||
gfile.P(serviceName, "Endpoints", "=", "[]", microApiPackage.Ident("Endpoint"), "{")
|
||||
for _, method := range service.Methods {
|
||||
if method.Desc.Options() == nil {
|
||||
continue
|
||||
@@ -456,13 +461,18 @@ func generateServiceEndpoints(gfile *protogen.GeneratedFile, service *protogen.S
|
||||
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
|
||||
endpoints, streaming := generateEndpoints(method)
|
||||
for _, endpoint := range endpoints {
|
||||
gfile.P("&", microApiPackage.Ident("Endpoint"), "{")
|
||||
gfile.P("{")
|
||||
generateEndpoint(gfile, serviceName, method.GoName, endpoint, streaming)
|
||||
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()
|
||||
}
|
||||
@@ -559,11 +569,11 @@ 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 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 && body != "" {
|
||||
gfile.P("Body:", fmt.Sprintf(`"%s",`, body))
|
||||
}
|
||||
|
@@ -15,6 +15,8 @@ var (
|
||||
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")
|
||||
microCodecPackage = protogen.GoImportPath("github.com/unistack-org/micro/v3/codec")
|
||||
timePackage = protogen.GoImportPath("time")
|
||||
deprecationComment = "// Deprecated: Do not use."
|
||||
versionComment = "v3.4.2"
|
||||
)
|
||||
|
Reference in New Issue
Block a user