Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
fix: new impl
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Dec 26, 2023
1 parent 05f078d commit 48ea7aa
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 24 deletions.
24 changes: 18 additions & 6 deletions cmd/protoc-gen-saber-asynq/asynq.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,17 @@ func genService(gen *protogen.Plugin, file *protogen.File, g *protogen.Generated
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
continue
}
rule, ok := proto.GetExtension(method.Desc.Options(), asynq.E_Task).(*asynq.Task)
if rule != nil && ok {
rule, ok := MatchAsynqRule(method.Comments.Leading)
if ok {
sd.Methods = append(sd.Methods, buildAsynqRule(g, method, rule))
} else {
r, ok := proto.GetExtension(method.Desc.Options(), asynq.E_Task).(*asynq.Task)
if r != nil && ok {
sd.Methods = append(sd.Methods, buildAsynqRule(g, method, &Task{
Pattern: r.Pattern,
CronSpec: r.CronSpec,
}))
}
}
}
if len(sd.Methods) == 0 {
Expand All @@ -115,20 +123,24 @@ func hasHTTPRule(services []*protogen.Service) bool {
if method.Desc.IsStreamingClient() || method.Desc.IsStreamingServer() {
continue
}
rule, ok := proto.GetExtension(method.Desc.Options(), asynq.E_Task).(*asynq.Task)
if rule != nil && ok {
if _, ok := MatchAsynqRule(method.Comments.Leading); ok {
return true
} else {
rule, ok := proto.GetExtension(method.Desc.Options(), asynq.E_Task).(*asynq.Task)
if rule != nil && ok {
return true
}
}
}
}
return false
}

func buildAsynqRule(g *protogen.GeneratedFile, m *protogen.Method, rule *asynq.Task) *methodDesc {
func buildAsynqRule(g *protogen.GeneratedFile, m *protogen.Method, rule *Task) *methodDesc {
return buildMethodDesc(g, m, rule)
}

func buildMethodDesc(g *protogen.GeneratedFile, m *protogen.Method, rule *asynq.Task) *methodDesc {
func buildMethodDesc(g *protogen.GeneratedFile, m *protogen.Method, rule *Task) *methodDesc {
defer func() { methodSets[m.GoName]++ }()
comment := m.Comments.Leading.String() + m.Comments.Trailing.String()
if comment != "" {
Expand Down
39 changes: 38 additions & 1 deletion cmd/protoc-gen-saber-asynq/defined.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
package main

const version = "v0.2.0"
import (
"strings"

"github.com/things-go/protogen-saber/internal/protoutil"
"google.golang.org/protobuf/compiler/protogen"
)

const version = "v0.3.0"

// annotation const value
const (
annotation_Path = "asynq"
annotation_Key_Pattern = "pattern"
annotation_Key_CronSpec = "cron_spec"
)

type serviceDesc struct {
ServiceType string // Greeter
Expand All @@ -19,3 +33,26 @@ type methodDesc struct {
Pattern string // 匹配器
CronSpec string // cron specification
}

type Task struct {
Pattern string
CronSpec string
}

func MatchAsynqRule(c protogen.Comments) (*Task, bool) {
annotes := protoutil.NewComments(c).FindAnnotation(annotation_Path)
if len(annotes) > 0 {
t := &Task{}
for _, v := range annotes {
if strings.EqualFold(v.Key, annotation_Key_Pattern) {
t.Pattern = v.Value
} else if strings.EqualFold(v.Key, annotation_Key_CronSpec) {
t.CronSpec = v.Value
}
}
if t.Pattern != "" && t.CronSpec != "" {
return t, true
}
}
return nil, false
}
6 changes: 6 additions & 0 deletions example/asynq/asynq.asynq.pb.go

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

20 changes: 9 additions & 11 deletions example/asynq/asynq.pb.go

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

9 changes: 3 additions & 6 deletions example/asynq/asynq.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ service User {
};
};
// 异步更新用户
rpc UpdateUser(UpdateUserPayload) returns (google.protobuf.Empty) {
option (things_go.asynq.task) = {
pattern: "user:update",
cron_spec: "@every 120s"
};
};
// #[asynq(pattern="user:update")]
// #[asynq(cron_spec="@every 120s")]
rpc UpdateUser(UpdateUserPayload) returns (google.protobuf.Empty);
}

message CreateUserPayload {
Expand Down

0 comments on commit 48ea7aa

Please sign in to comment.