Compare commits

..

7 Commits

Author SHA1 Message Date
5ecc4986dd dont tag if tag_path is empty
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-05-11 09:07:12 +03:00
9ed1ca9a89 add info to readme
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-05-08 12:53:14 +03:00
805b52cf8d add tags example
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-05-08 12:46:02 +03:00
d41fa1a64f add tag support
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2021-05-08 00:06:40 +03:00
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
10 changed files with 295 additions and 44 deletions

View File

@@ -15,8 +15,9 @@ $> 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

156
ast.go Normal file
View File

@@ -0,0 +1,156 @@
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) astGenerate(plugin *protogen.Plugin) error {
if g.tagPath == "" {
return nil
}
for _, file := range plugin.Files {
if !file.Generate {
continue
}
for _, message := range file.Messages {
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 := 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 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
}

44
example/example.proto Normal file
View File

@@ -0,0 +1,44 @@
syntax = "proto3";
package example;
option go_package = "github.com/unistack-org/protoc-gen-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;
};

3
go.mod
View File

@@ -3,7 +3,8 @@ module github.com/unistack-org/protoc-gen-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.2
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.26.0
)

8
go.sum
View File

@@ -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.2 h1:mL1ZPRGPCWJOiMBiJrkk8Y513rPrJmhJWxyLceckAXU=
github.com/unistack-org/micro-proto v0.0.2/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=

28
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
@@ -21,22 +21,30 @@ 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("// source: ", file.Proto.GetName())
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
View File

@@ -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
}

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
@@ -22,21 +22,26 @@ 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("// source: ", file.Proto.GetName())
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)
}
}
}

27
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
@@ -21,21 +21,28 @@ 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("// source: ", file.Proto.GetName())
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)
}
}
}

15
util.go
View File

@@ -283,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("}")
}
@@ -559,11 +560,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))
}