-
Notifications
You must be signed in to change notification settings - Fork 81
/
tchan-keyvalue.go
381 lines (310 loc) · 9.33 KB
/
tchan-keyvalue.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// @generated Code generated by thrift-gen. Do not modify.
// Package keyvalue is generated code used to make or handle TChannel calls using Thrift.
package keyvalue
import (
"fmt"
athrift "github.com/uber/tchannel-go/thirdparty/github.com/apache/thrift/lib/go/thrift"
"github.com/uber/tchannel-go/thrift"
)
// Interfaces for the service and client for the services defined in the IDL.
// TChanAdmin is the interface that defines the server handler and client interface.
type TChanAdmin interface {
TChanBaseService
ClearAll(ctx thrift.Context) error
}
// TChanKeyValue is the interface that defines the server handler and client interface.
type TChanKeyValue interface {
TChanBaseService
Get(ctx thrift.Context, key string) (string, error)
Set(ctx thrift.Context, key string, value string) error
}
// TChanBaseService is the interface that defines the server handler and client interface.
type TChanBaseService interface {
HealthCheck(ctx thrift.Context) (string, error)
}
// Implementation of a client and service handler.
type tchanAdminClient struct {
TChanBaseService
thriftService string
client thrift.TChanClient
}
func NewTChanAdminInheritedClient(thriftService string, client thrift.TChanClient) *tchanAdminClient {
return &tchanAdminClient{
NewTChanBaseServiceInheritedClient(thriftService, client),
thriftService,
client,
}
}
// NewTChanAdminClient creates a client that can be used to make remote calls.
func NewTChanAdminClient(client thrift.TChanClient) TChanAdmin {
return NewTChanAdminInheritedClient("Admin", client)
}
func (c *tchanAdminClient) ClearAll(ctx thrift.Context) error {
var resp AdminClearAllResult
args := AdminClearAllArgs{}
success, err := c.client.Call(ctx, c.thriftService, "clearAll", &args, &resp)
if err == nil && !success {
switch {
case resp.NotAuthorized != nil:
err = resp.NotAuthorized
default:
err = fmt.Errorf("received no result or unknown exception for clearAll")
}
}
return err
}
type tchanAdminServer struct {
thrift.TChanServer
handler TChanAdmin
}
// NewTChanAdminServer wraps a handler for TChanAdmin so it can be
// registered with a thrift.Server.
func NewTChanAdminServer(handler TChanAdmin) thrift.TChanServer {
return &tchanAdminServer{
NewTChanBaseServiceServer(handler),
handler,
}
}
func (s *tchanAdminServer) Service() string {
return "Admin"
}
func (s *tchanAdminServer) Methods() []string {
return []string{
"clearAll",
"HealthCheck",
}
}
func (s *tchanAdminServer) Handle(ctx thrift.Context, methodName string, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
switch methodName {
case "clearAll":
return s.handleClearAll(ctx, protocol)
case "HealthCheck":
return s.TChanServer.Handle(ctx, methodName, protocol)
default:
return false, nil, fmt.Errorf("method %v not found in service %v", methodName, s.Service())
}
}
func (s *tchanAdminServer) handleClearAll(ctx thrift.Context, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
var req AdminClearAllArgs
var res AdminClearAllResult
if err := req.Read(protocol); err != nil {
return false, nil, err
}
err :=
s.handler.ClearAll(ctx)
if err != nil {
switch v := err.(type) {
case *NotAuthorized:
if v == nil {
return false, nil, fmt.Errorf("Handler for notAuthorized returned non-nil error type *NotAuthorized but nil value")
}
res.NotAuthorized = v
default:
return false, nil, err
}
} else {
}
return err == nil, &res, nil
}
type tchanKeyValueClient struct {
TChanBaseService
thriftService string
client thrift.TChanClient
}
func NewTChanKeyValueInheritedClient(thriftService string, client thrift.TChanClient) *tchanKeyValueClient {
return &tchanKeyValueClient{
NewTChanBaseServiceInheritedClient(thriftService, client),
thriftService,
client,
}
}
// NewTChanKeyValueClient creates a client that can be used to make remote calls.
func NewTChanKeyValueClient(client thrift.TChanClient) TChanKeyValue {
return NewTChanKeyValueInheritedClient("KeyValue", client)
}
func (c *tchanKeyValueClient) Get(ctx thrift.Context, key string) (string, error) {
var resp KeyValueGetResult
args := KeyValueGetArgs{
Key: key,
}
success, err := c.client.Call(ctx, c.thriftService, "Get", &args, &resp)
if err == nil && !success {
switch {
case resp.NotFound != nil:
err = resp.NotFound
case resp.InvalidKey != nil:
err = resp.InvalidKey
default:
err = fmt.Errorf("received no result or unknown exception for Get")
}
}
return resp.GetSuccess(), err
}
func (c *tchanKeyValueClient) Set(ctx thrift.Context, key string, value string) error {
var resp KeyValueSetResult
args := KeyValueSetArgs{
Key: key,
Value: value,
}
success, err := c.client.Call(ctx, c.thriftService, "Set", &args, &resp)
if err == nil && !success {
switch {
case resp.InvalidKey != nil:
err = resp.InvalidKey
default:
err = fmt.Errorf("received no result or unknown exception for Set")
}
}
return err
}
type tchanKeyValueServer struct {
thrift.TChanServer
handler TChanKeyValue
}
// NewTChanKeyValueServer wraps a handler for TChanKeyValue so it can be
// registered with a thrift.Server.
func NewTChanKeyValueServer(handler TChanKeyValue) thrift.TChanServer {
return &tchanKeyValueServer{
NewTChanBaseServiceServer(handler),
handler,
}
}
func (s *tchanKeyValueServer) Service() string {
return "KeyValue"
}
func (s *tchanKeyValueServer) Methods() []string {
return []string{
"Get",
"Set",
"HealthCheck",
}
}
func (s *tchanKeyValueServer) Handle(ctx thrift.Context, methodName string, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
switch methodName {
case "Get":
return s.handleGet(ctx, protocol)
case "Set":
return s.handleSet(ctx, protocol)
case "HealthCheck":
return s.TChanServer.Handle(ctx, methodName, protocol)
default:
return false, nil, fmt.Errorf("method %v not found in service %v", methodName, s.Service())
}
}
func (s *tchanKeyValueServer) handleGet(ctx thrift.Context, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
var req KeyValueGetArgs
var res KeyValueGetResult
if err := req.Read(protocol); err != nil {
return false, nil, err
}
r, err :=
s.handler.Get(ctx, req.Key)
if err != nil {
switch v := err.(type) {
case *KeyNotFound:
if v == nil {
return false, nil, fmt.Errorf("Handler for notFound returned non-nil error type *KeyNotFound but nil value")
}
res.NotFound = v
case *InvalidKey:
if v == nil {
return false, nil, fmt.Errorf("Handler for invalidKey returned non-nil error type *InvalidKey but nil value")
}
res.InvalidKey = v
default:
return false, nil, err
}
} else {
res.Success = &r
}
return err == nil, &res, nil
}
func (s *tchanKeyValueServer) handleSet(ctx thrift.Context, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
var req KeyValueSetArgs
var res KeyValueSetResult
if err := req.Read(protocol); err != nil {
return false, nil, err
}
err :=
s.handler.Set(ctx, req.Key, req.Value)
if err != nil {
switch v := err.(type) {
case *InvalidKey:
if v == nil {
return false, nil, fmt.Errorf("Handler for invalidKey returned non-nil error type *InvalidKey but nil value")
}
res.InvalidKey = v
default:
return false, nil, err
}
} else {
}
return err == nil, &res, nil
}
type tchanBaseServiceClient struct {
thriftService string
client thrift.TChanClient
}
func NewTChanBaseServiceInheritedClient(thriftService string, client thrift.TChanClient) *tchanBaseServiceClient {
return &tchanBaseServiceClient{
thriftService,
client,
}
}
// NewTChanBaseServiceClient creates a client that can be used to make remote calls.
func NewTChanBaseServiceClient(client thrift.TChanClient) TChanBaseService {
return NewTChanBaseServiceInheritedClient("baseService", client)
}
func (c *tchanBaseServiceClient) HealthCheck(ctx thrift.Context) (string, error) {
var resp BaseServiceHealthCheckResult
args := BaseServiceHealthCheckArgs{}
success, err := c.client.Call(ctx, c.thriftService, "HealthCheck", &args, &resp)
if err == nil && !success {
switch {
default:
err = fmt.Errorf("received no result or unknown exception for HealthCheck")
}
}
return resp.GetSuccess(), err
}
type tchanBaseServiceServer struct {
handler TChanBaseService
}
// NewTChanBaseServiceServer wraps a handler for TChanBaseService so it can be
// registered with a thrift.Server.
func NewTChanBaseServiceServer(handler TChanBaseService) thrift.TChanServer {
return &tchanBaseServiceServer{
handler,
}
}
func (s *tchanBaseServiceServer) Service() string {
return "baseService"
}
func (s *tchanBaseServiceServer) Methods() []string {
return []string{
"HealthCheck",
}
}
func (s *tchanBaseServiceServer) Handle(ctx thrift.Context, methodName string, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
switch methodName {
case "HealthCheck":
return s.handleHealthCheck(ctx, protocol)
default:
return false, nil, fmt.Errorf("method %v not found in service %v", methodName, s.Service())
}
}
func (s *tchanBaseServiceServer) handleHealthCheck(ctx thrift.Context, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
var req BaseServiceHealthCheckArgs
var res BaseServiceHealthCheckResult
if err := req.Read(protocol); err != nil {
return false, nil, err
}
r, err :=
s.handler.HealthCheck(ctx)
if err != nil {
return false, nil, err
} else {
res.Success = &r
}
return err == nil, &res, nil
}