-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtchannel-template.go
191 lines (159 loc) · 4.56 KB
/
tchannel-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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
var tchannelTmpl = `
// @generated Code generated by thrift-gen. Do not modify.
// Package {{ .Package }} is generated code used to make or handle TChannel calls using Thrift.
package {{ .Package }}
import (
"fmt"
athrift "{{ .Imports.Thrift }}"
"{{ .Imports.TChannel }}"
{{ range .Includes }}
"{{ .Import }}"
{{ end }}
)
{{ range .Includes }}
var _ = {{ .Package }}.GoUnusedProtection__
{{ end }}
// Interfaces for the service and client for the services defined in the IDL.
{{ range .Services }}
// {{ .Interface }} is the interface that defines the server handler and client interface.
type {{ .Interface }} interface {
{{ if .HasExtends }}
{{ .ExtendsServicePrefix }}{{ .ExtendsService.Interface }}
{{ end }}
{{ range .Methods }}
{{ .Name }}({{ .ArgList }}) {{ .RetType }}
{{ end }}
}
{{ end }}
// Implementation of a client and service handler.
{{/* Generate client and service implementations for the above interfaces. */}}
{{ range $svc := .Services }}
type {{ .ClientStruct }} struct {
{{ if .HasExtends }}
{{ .ExtendsServicePrefix }}{{ .ExtendsService.Interface }}
{{ end }}
thriftService string
client thrift.TChanClient
}
func {{ .InheritedClientConstructor }}(thriftService string, client thrift.TChanClient) *{{ .ClientStruct }} {
return &{{ .ClientStruct }}{
{{ if .HasExtends }}
{{ .ExtendsServicePrefix }}{{ .ExtendsService.InheritedClientConstructor }}(thriftService, client),
{{ end }}
thriftService,
client,
}
}
// {{ .ClientConstructor }} creates a client that can be used to make remote calls.
func {{ .ClientConstructor }}(client thrift.TChanClient) {{ .Interface }} {
return {{ .InheritedClientConstructor }}("{{ .ThriftName }}", client)
}
{{ range .Methods }}
func (c *{{ $svc.ClientStruct }}) {{ .Name }}({{ .ArgList }}) {{ .RetType }} {
var resp {{ .ResultType }}
args := {{ .ArgsType }}{
{{ range .Arguments }}
{{ .ArgStructName }}: {{ .Name }},
{{ end }}
}
success, err := c.client.Call(ctx, c.thriftService, "{{ .ThriftName }}", &args, &resp)
if err == nil && !success {
switch {
{{ range .Exceptions }}
case resp.{{ .ArgStructName }} != nil:
err = resp.{{ .ArgStructName }}
{{ end }}
default:
err = fmt.Errorf("received no result or unknown exception for {{ .ThriftName }}")
}
}
{{ if .HasReturn }}
return resp.GetSuccess(), err
{{ else }}
return err
{{ end }}
}
{{ end }}
type {{ .ServerStruct }} struct {
{{ if .HasExtends }}
thrift.TChanServer
{{ end }}
handler {{ .Interface }}
}
// {{ .ServerConstructor }} wraps a handler for {{ .Interface }} so it can be
// registered with a thrift.Server.
func {{ .ServerConstructor }}(handler {{ .Interface }}) thrift.TChanServer {
return &{{ .ServerStruct }}{
{{ if .HasExtends }}
{{ .ExtendsServicePrefix }}{{ .ExtendsService.ServerConstructor }}(handler),
{{ end }}
handler,
}
}
func (s *{{ .ServerStruct }}) Service() string {
return "{{ .ThriftName }}"
}
func (s *{{ .ServerStruct }}) Methods() []string {
return []string{
{{ range .Methods }}
"{{ .ThriftName }}",
{{ end }}
{{ range .InheritedMethods }}
"{{ . }}",
{{ end }}
}
}
func (s *{{ .ServerStruct }}) Handle(ctx {{ contextType }}, methodName string, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
switch methodName {
{{ range .Methods }}
case "{{ .ThriftName }}":
return s.{{ .HandleFunc }}(ctx, protocol)
{{ end }}
{{ range .InheritedMethods }}
case "{{ . }}":
return s.TChanServer.Handle(ctx, methodName, protocol)
{{ end }}
default:
return false, nil, fmt.Errorf("method %v not found in service %v", methodName, s.Service())
}
}
{{ range .Methods }}
func (s *{{ $svc.ServerStruct }}) {{ .HandleFunc }}(ctx {{ contextType }}, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
var req {{ .ArgsType }}
var res {{ .ResultType }}
if err := req.Read(protocol); err != nil {
return false, nil, err
}
{{ if .HasReturn }}
r, err :=
{{ else }}
err :=
{{ end }}
s.handler.{{ .Name }}({{ .CallList "req" }})
if err != nil {
{{ if .HasExceptions }}
switch v := err.(type) {
{{ range .Exceptions }}
case {{ .ArgType }}:
if v == nil {
return false, nil, fmt.Errorf("Handler for {{ .Name }} returned non-nil error type {{ .ArgType }} but nil value")
}
res.{{ .ArgStructName }} = v
{{ end }}
default:
return false, nil, err
}
{{ else }}
return false, nil, err
{{ end }}
} else {
{{ if .HasReturn }}
res.Success = {{ .WrapResult "r" }}
{{ end }}
}
return err == nil, &res, nil
}
{{ end }}
{{ end }}
`