-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplate.go
151 lines (135 loc) · 3.72 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"bytes"
"strings"
"text/template"
"github.com/gopkg-dev/protoc-gen-go-asynq/asynq"
"github.com/huandu/xstrings"
)
var asynqTemplate = `
{{$svrType := .ServiceType}}
{{$svrName := .ServiceName}}
const {{.ServiceType}}QueueName = "{{snakecase $svrType}}"
type {{.ServiceType}}TaskServer interface {
{{- range .MethodSets}}
{{.Name}}(context.Context, *{{.Request}}) (error)
{{- end}}
}
func Register{{.ServiceType}}TaskServer(s *asynqx.Server, srv {{.ServiceType}}TaskServer) {
{{- range .Methods}}
s.HandleFunc("{{.Typename}}", _{{$svrType}}_{{.Name}}_Task_Handler(srv))
{{- end}}
}
{{range .Methods}}
func _{{$svrType}}_{{.Name}}_Task_Handler(srv {{$svrType}}TaskServer) func(context.Context, *asynq.Task) error {
return func(ctx context.Context, task *asynq.Task) error {
var in {{.Request}}
if err := {{ .PayloadType | getPayloadType}}.Unmarshal(task.Payload(), &in); err != nil {
return err
}
return srv.{{.Name}}(ctx, &in)
}
}
{{end}}
type {{.ServiceType}}SvcTask struct {}
var {{.ServiceType}}Task {{.ServiceType}}SvcTask
{{range .MethodSets}}
func (j *{{$svrType}}SvcTask) {{.Name}}(in *{{.Request}}, opts ...asynq.Option) (*asynq.Task, error) {
payload, err := {{ .PayloadType | getPayloadType}}.Marshal(in)
if err != nil {
return nil, err
}
{{- if .TimeOut }}
opts = append(opts, asynq.Timeout({{.TimeOut}}* time.Second))
{{- end}}
{{- if .MaxRetry }}
opts = append(opts, asynq.MaxRetry({{.MaxRetry}}))
{{- end}}
{{- if .Retention }}
opts = append(opts, asynq.Retention({{.Retention}}* time.Second))
{{- end}}
{{- if .Unique }}
opts = append(opts, asynq.Unique({{.Unique}}* time.Second))
{{- end}}
opts = append(opts, asynq.Queue({{$svrType}}QueueName))
task := asynq.NewTask("{{.Typename}}", payload, opts...)
return task, nil
}
{{end}}
type {{.ServiceType}}TaskClient interface {
{{- range .MethodSets}}
{{.Name}}(ctx context.Context, req *{{.Request}}, opts ...asynq.Option) (info *asynq.TaskInfo, err error)
{{- end}}
}
type {{.ServiceType}}TaskClientImpl struct{
cc *asynq.Client
}
func New{{.ServiceType}}TaskClient (client *asynq.Client) {{.ServiceType}}TaskClient {
return &{{.ServiceType}}TaskClientImpl{client}
}
{{range .MethodSets}}
func (c *{{$svrType}}TaskClientImpl) {{.Name}}(ctx context.Context, in *{{.Request}}, opts ...asynq.Option) (*asynq.TaskInfo, error) {
task, err := {{$svrType}}Task.{{.Name}}(in, opts...)
if err != nil {
return nil, err
}
info, err := c.cc.Enqueue(task)
if err != nil {
return nil, err
}
return info, nil
}
{{end}}
`
type serviceDesc struct {
ServiceType string // Greeter
ServiceName string // helloworld.Greeter
Metadata string // api/helloworld/helloworld.proto
Methods []*methodDesc
MethodSets map[string]*methodDesc
}
type methodDesc struct {
// method
Name string
Num int
Request string
Reply string
// asynq rule
Typename *string
TimeOut *int32
MaxRetry *int32
Retention *int32
Unique *int32
PayloadType *asynq.Task_PayloadType
}
func (s *serviceDesc) execute() string {
s.MethodSets = make(map[string]*methodDesc)
for _, m := range s.Methods {
s.MethodSets[m.Name] = m
}
buf := new(bytes.Buffer)
tmpl, err := template.New("asynq").Funcs(map[string]interface{}{
"lower": strings.ToLower,
"snakecase": xstrings.ToSnakeCase,
"getPayloadType": func(t *asynq.Task_PayloadType) string {
if t == nil {
return "proto"
}
switch t.String() {
case asynq.Task_Protobuf.String():
return "proto"
case asynq.Task_JSON.String():
return "json"
default:
return "proto"
}
},
}).Parse(strings.TrimSpace(asynqTemplate))
if err != nil {
panic(err)
}
if err := tmpl.Execute(buf, s); err != nil {
panic(err)
}
return strings.Trim(buf.String(), "\r\n")
}