-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
145 lines (127 loc) · 3.61 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
package main
import (
"bytes"
"strings"
"text/template"
)
var httpTemplate = `
{{$svrType := .ServiceType}}
{{$svrName := .ServiceName}}
type {{.ServiceType}}EventServer interface {
{{- range .MethodSets}}
{{.Name}}(context.Context, *{{.Request}}) error
{{- end}}
mustEmbedUnimplemented{{.ServiceType}}EventServer()
}
func Register{{.ServiceType}}EventServer(r *message.Router, sg func(topic string) message.Subscriber, srv {{.ServiceType}}EventServer) {
{{- range .Methods}}
r.AddNoPublisherHandler(
"{{.EventName}}",
"{{.EventName}}",
sg("{{.EventName}}"),
_{{$svrType}}_{{.Name}}{{.Num}}_Event_Handler(srv),
)
{{- end}}
}
{{range .Methods}}
func _{{$svrType}}_{{.Name}}{{.Num}}_Event_Handler(srv {{$svrType}}EventServer) func(msg *message.Message) error {
return func(msg *message.Message) error {
var req {{.Request}}
err := protojson.Unmarshal(msg.Payload, &req)
if err != nil {
return err
}
return srv.{{.Name}}(msg.Context(), &req)
}
}
{{end}}
type {{.ServiceType}}EventClient interface {
{{- range .MethodSets}}
{{.Name}}(ctx context.Context, req *{{.Request}}) error
{{if gt .EventDelay 0 }}
{{.Name}}WithDelay(ctx context.Context, req *{{.Request}}, delay uint64) error
{{end}}
{{- end}}
}
type {{.ServiceType}}EventClientImpl struct{
publisher message.Publisher
}
func New{{.ServiceType}}EventClient (publisher message.Publisher) {{.ServiceType}}EventClient {
return &{{.ServiceType}}EventClientImpl{publisher}
}
{{range .MethodSets}}
func (c *{{$svrType}}EventClientImpl) {{.Name}}(ctx context.Context, req *{{.Request}}) error {
topic := "{{.EventName}}"
byteData, err := protojson.Marshal(req)
if err != nil {
return err
}
msg := message.NewMessage(watermill.NewUUID(), byteData)
msg.SetContext(ctx)
msg.Metadata.Set("topic", topic)
{{if gt .EventDelay 0}}
// 设置延迟队列时间,单位为{{.EventDelay}}ms
msg.Metadata.Set("x-delay", "{{.EventDelay}}")
{{end}}
return c.publisher.Publish(topic, msg)
}
{{end}}
{{range .MethodSets}}
{{if gt .EventDelay 0}}
func (c *{{$svrType}}EventClientImpl) {{.Name}}WithDelay(ctx context.Context, req *{{.Request}}, delay uint64) error {
topic := "{{.EventName}}"
byteData, err := protojson.Marshal(req)
if err != nil {
return err
}
msg := message.NewMessage(watermill.NewUUID(), byteData)
msg.SetContext(ctx)
msg.Metadata.Set("topic", topic)
msg.Metadata.Set("x-delay", fmt.Sprintf("%d", delay))
return c.publisher.Publish(topic, msg)
}
{{end}}
{{end}}
type Unsafe{{.ServiceType}}EventServer interface {
mustEmbedUnimplemented{{.ServiceType}}EventServer()
}
type Unimplemented{{.ServiceType}}EventServer struct {}
{{- range .MethodSets}}
func (Unimplemented{{$svrType}}EventServer) {{.Name}}(context.Context, *{{.Request}}) error {
return nil
}
{{- end}}
func (Unimplemented{{$svrType}}EventServer) mustEmbedUnimplemented{{.ServiceType}}EventServer() {}
`
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
// event_rule
EventName string // 事件名称
EventDelay uint64 // 延迟时间
}
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("http").Parse(strings.TrimSpace(httpTemplate))
if err != nil {
panic(err)
}
if err := tmpl.Execute(buf, s); err != nil {
panic(err)
}
return strings.Trim(buf.String(), "\r\n")
}