Skip to content

Commit

Permalink
feat(proto): 支持 proto 生成 mq 代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Ccheers committed Aug 1, 2022
1 parent 00642ad commit 25f4d35
Show file tree
Hide file tree
Showing 13 changed files with 462 additions and 95 deletions.
8 changes: 7 additions & 1 deletion protoc-gen-mq/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ gen_example:
--go-grpc_out=paths=source_relative:. \
--mq_out=paths=source_relative:. \
example/api/product/app/v1/v1.proto
#protoc-go-inject-tag -input=./example/api/product/app/v1/v1.pb.go
#protoc-go-inject-tag -input=./example/api/product/app/v1/v1.pb.go

.PHONY: proto
proto:
protoc -I . \
--go_out=paths=source_relative:. \
./proto/v1/options/*.proto
23 changes: 9 additions & 14 deletions protoc-gen-mq/define.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package main
import (
_ "embed"
"fmt"
"regexp"
"strings"

"github.com/Ccheers/kratos-mq/protoc-gen-mq/proto/v1/options"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
)

type service struct {
Expand Down Expand Up @@ -44,23 +45,17 @@ func (m *method) HandlerName() string {
return fmt.Sprintf("%s_%d_MQHandler", m.Name, m.Num)
}

// matchMQReg 匹配 mq tag @mq: topic::channel
var matchMQReg = regexp.MustCompile("@mq\\s*:\\s*`.+?`")

var (
matchTopicReg = regexp.MustCompile("topic\\s*:\\s*\"([\\w\\\\.]+)\"")
matchChannelReg = regexp.MustCompile("channel\\s*:\\s*\"([\\w\\\\.]+)\"")
)

func genMethod(m *protogen.Method, g *protogen.GeneratedFile) []*method {
var methods []*method

mqs := matchMQReg.FindAllString(m.Comments.Leading.String(), -1)
for _, str := range mqs {
topic := strings.TrimSpace(matchTopicReg.ReplaceAllString(matchTopicReg.FindString(str), "$1"))
channel := strings.TrimSpace(matchChannelReg.ReplaceAllString(matchChannelReg.FindString(str), "$1"))
methods = append(methods, buildMethodDesc(m, g, topic, channel))
mq, ok := proto.GetExtension(m.Desc.Options(), options.E_Mq).(*options.MQ)
if !ok {
return nil
}
for _, group := range mq.GetSubscribes() {
methods = append(methods, buildMethodDesc(m, g, group.GetTopic(), group.GetChannel()))
}

return methods
}

Expand Down
87 changes: 46 additions & 41 deletions protoc-gen-mq/example/api/product/app/v1/v1.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 44 additions & 33 deletions protoc-gen-mq/example/api/product/app/v1/v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,63 @@ option go_package = "github.com/Ccheers/kratos-mq/protoc-gen-mq/example/api/prod
package product.app.v1;

import "google/api/annotations.proto";
import "proto/v1/options/annotations.proto";

// blog service is a blog demo
service BlogService {
// 获取文章列表
rpc GetArticles(GetArticlesReq) returns (GetArticlesResp) {
option (google.api.http) = {
get: "/v1/articles"
additional_bindings {
get: "/v1/author/{author_id}/articles"
}
};
}
// 创建文章
// @mq:`topic:"tp1" channel:"ch1"`
// @mq:`topic:"tp2" channel:"ch2"`
rpc CreateArticle(Article) returns (Article) {
option (google.api.http) = {
post: "/v1/author/{author_id}/articles"
body: "*"
};
}
// 获取文章列表
rpc GetArticles(GetArticlesReq) returns (GetArticlesResp) {
option (google.api.http) = {
get: "/v1/articles"
additional_bindings {
get: "/v1/author/{author_id}/articles"
}
};
}
// 创建文章
// @mq:`topic:"tp1" channel:"ch1"`
// @mq:`topic:"tp2" channel:"ch2"`
rpc CreateArticle(Article) returns (Article) {
option (google.api.http) = {
post: "/v1/author/{author_id}/articles"
body: "*"
};
option (kmq.v1.options.mq) = {
subscribes: {
topic: "tp1",
channel: "ch3",
},
subscribes: {
topic: "tp2",
channel: "ch2",
},
};
}
}

message GetArticlesReq {
// @inject_tag: form:"title"
string title = 1;
// @inject_tag: form:"title"
string title = 1;

// @inject_tag: form:"page"
int32 page = 2;
// @inject_tag: form:"page"
int32 page = 2;

// @inject_tag: form:"page_size"
int32 page_size = 3;
// @inject_tag: form:"page_size"
int32 page_size = 3;

// 作者名
// @inject_tag: form:"author_id" uri:"author_id"
int32 author_id = 4;
// 作者名
// @inject_tag: form:"author_id" uri:"author_id"
int32 author_id = 4;
}

message GetArticlesResp {
int64 total = 1;
repeated Article articles = 2;
int64 total = 1;
repeated Article articles = 2;
}

message Article {
string title = 1;
string content = 2;
// @inject_tag: form:"author_id" uri:"author_id"
int32 author_id = 3;
string title = 1;
string content = 2;
// @inject_tag: form:"author_id" uri:"author_id"
int32 author_id = 3;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion protoc-gen-mq/example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ go 1.16

require (
github.com/Ccheers/kratos-mq v0.0.1
github.com/Ccheers/kratos-mq/protoc-gen-mq v0.0.1
github.com/go-kratos/kratos/v2 v2.3.1
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd
google.golang.org/grpc v1.46.2
google.golang.org/protobuf v1.28.0
)

replace github.com/Ccheers/kratos-mq => ../../
replace (
github.com/Ccheers/kratos-mq => ../../
github.com/Ccheers/kratos-mq/protoc-gen-mq => ../
)
2 changes: 1 addition & 1 deletion protoc-gen-mq/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s service) GetArticles(ctx context.Context, req *v1.GetArticlesReq) (*v1.G
}

func main() {
e := mq.NewServer(nil)
e := mq.NewServer(nil, nil)
v1.RegisterBlogServiceMQServer(e, &service{})
e.Start(context.Background())
}
1 change: 0 additions & 1 deletion protoc-gen-mq/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ go 1.16

require (
github.com/google/go-cmp v0.5.7 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.28.0
)
3 changes: 1 addition & 2 deletions protoc-gen-mq/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
Loading

0 comments on commit 25f4d35

Please sign in to comment.