Skip to content

Commit

Permalink
perf: 支持单独给rpc添加//@bot标签
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Nov 6, 2024
1 parent f1f6436 commit 7ca34b1
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 29 deletions.
128 changes: 105 additions & 23 deletions api/bot/v1/counter.pb.go

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

21 changes: 19 additions & 2 deletions contrib/protoc-gen-bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ func genService(_ *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFi
UpdateType: g.QualifiedGoIdent(conf.update.pkg.Ident(conf.update.model)),
MessageType: g.QualifiedGoIdent(conf.message.pkg.Ident(conf.message.model)),
}
allEnable := isBotEnable(service.Comments.Leading.String())
for _, method := range service.Methods {
enable := allEnable || isBotEnable(method.Comments.Leading.String())
if !enable {
continue
}
sd.Methods = append(sd.Methods, &methodDesc{
Name: method.GoName,
OriginalName: string(method.Desc.Name()),
Expand All @@ -84,11 +89,23 @@ var botTagsMatchRegexp = regexp.MustCompile(` *//+ *@bot`)

func hasBotRule(services []*protogen.Service) bool {
for _, service := range services {
for _, line := range strings.Split(service.Comments.Leading.String(), "\n") {
if botTagsMatchRegexp.MatchString(line) {
if isBotEnable(service.Comments.Leading.String()) {
return true
}
for _, method := range service.Methods {
if isBotEnable(method.Comments.Leading.String()) {
return true
}
}
}
return false
}

func isBotEnable(comment string) bool {
for _, line := range strings.Split(comment, "\n") {
if botTagsMatchRegexp.MatchString(line) {
return true
}
}
return false
}
3 changes: 0 additions & 3 deletions contrib/protoc-gen-bot/template.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ const BotHandler{{$svrType}}{{.OriginalName}} = "/{{$svrName}}/{{.OriginalName}}

type {{.ServiceType}}Server interface {
{{- range .MethodSets}}
{{- if ne .Comment ""}}
{{.Comment}}
{{- end}}
{{.Name}}(context.Context, *{{.Request}}) (*{{.Reply}}, error)
{{- end}}
}
Expand Down
10 changes: 9 additions & 1 deletion proto/bot/v1/counter.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ syntax = "proto3";

package bot.v1;

// @bot
service CounterService {
// @bot `comment:"/start"`
rpc Start(StartRequest) returns (StartResponse);
// @bot `comment:"/count", callback_query:"count"`
rpc Counter(CounterRequest) returns (CounterResponse);
rpc Unknown(UnknownRequest) returns (UnknownResponse);
}

message StartRequest {
Expand All @@ -23,4 +25,10 @@ message CounterRequest {

message CounterResponse {
int32 count = 1;
}

message UnknownRequest {
}

message UnknownResponse {
}

0 comments on commit 7ca34b1

Please sign in to comment.