summaryrefslogtreecommitdiff
path: root/test/go/src/common/printing_handler.go
blob: b726373fe34e3f0cf48f31975c066b25fbbef88f (plain)
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
382
383
384
385
386
387
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package common

import (
	"context"
	"encoding/hex"
	"errors"
	"fmt"
	"time"

	//lint:ignore ST1001 allow dot import here
	. "github.com/apache/thrift/test/go/src/gen/thrifttest"
)

var PrintingHandler = &printingHandler{}

type printingHandler struct{}

// Prints "testVoid()" and returns nothing.
func (p *printingHandler) TestVoid(ctx context.Context) (err error) {
	fmt.Println("testVoid()")
	return nil
}

// Prints 'testString("%s")' with thing as '%s'
// @param string thing - the string to print
// @return string - returns the string 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestString(ctx context.Context, thing string) (r string, err error) {
	fmt.Printf("testString(\"%s\")\n", thing)
	return thing, nil
}

// Prints 'testBool("%t")' with thing as 'true' or 'false'
// @param bool thing - the bool to print
// @return bool - returns the bool 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestBool(ctx context.Context, thing bool) (r bool, err error) {
	fmt.Printf("testBool(%t)\n", thing)
	return thing, nil
}

// Prints 'testByte("%d")' with thing as '%d'
// @param byte thing - the byte to print
// @return byte - returns the byte 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestByte(ctx context.Context, thing int8) (r int8, err error) {
	fmt.Printf("testByte(%d)\n", thing)
	return thing, nil
}

// Prints 'testI32("%d")' with thing as '%d'
// @param i32 thing - the i32 to print
// @return i32 - returns the i32 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestI32(ctx context.Context, thing int32) (r int32, err error) {
	fmt.Printf("testI32(%d)\n", thing)
	return thing, nil
}

// Prints 'testI64("%d")' with thing as '%d'
// @param i64 thing - the i64 to print
// @return i64 - returns the i64 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestI64(ctx context.Context, thing int64) (r int64, err error) {
	fmt.Printf("testI64(%d)\n", thing)
	return thing, nil
}

// Prints 'testDouble("%f")' with thing as '%f'
// @param double thing - the double to print
// @return double - returns the double 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestDouble(ctx context.Context, thing float64) (r float64, err error) {
	fmt.Printf("testDouble(%f)\n", thing)
	return thing, nil
}

// Prints 'testBinary("%s")' where '%s' is a hex-formatted string of thing's data
// @param []byte thing - the binary to print
// @return []byte - returns the binary 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestBinary(ctx context.Context, thing []byte) (r []byte, err error) {
	fmt.Printf("testBinary(%s)\n", hex.EncodeToString(thing))
	return thing, nil
}

// Prints 'testStruct("{%s}")' where thing has been formatted into a string of comma separated values
// @param Xtruct thing - the Xtruct to print
// @return Xtruct - returns the Xtruct 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestStruct(ctx context.Context, thing *Xtruct) (r *Xtruct, err error) {
	fmt.Printf("testStruct({\"%s\", %d, %d, %d})\n", thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing)
	return thing, err
}

// Prints 'testNest("{%s}")' where thing has been formatted into a string of the nested struct
// @param Xtruct2 thing - the Xtruct2 to print
// @return Xtruct2 - returns the Xtruct2 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestNest(ctx context.Context, nest *Xtruct2) (r *Xtruct2, err error) {
	thing := nest.StructThing
	fmt.Printf("testNest({%d, {\"%s\", %d, %d, %d}, %d})\n", nest.ByteThing, thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing, nest.I32Thing)
	return nest, nil
}

// Prints 'testMap("{%s")' where thing has been formatted into a string of  'key => value' pairs
//  separated by commas and new lines
// @param map<i32,i32> thing - the map<i32,i32> to print
// @return map<i32,i32> - returns the map<i32,i32> 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestMap(ctx context.Context, thing map[int32]int32) (r map[int32]int32, err error) {
	fmt.Printf("testMap({")
	first := true
	for k, v := range thing {
		if first {
			first = false
		} else {
			fmt.Printf(", ")
		}
		fmt.Printf("%d => %d", k, v)
	}
	fmt.Printf("})\n")
	return thing, nil
}

// Prints 'testStringMap("{%s}")' where thing has been formatted into a string of  'key => value' pairs
//  separated by commas and new lines
// @param map<string,string> thing - the map<string,string> to print
// @return map<string,string> - returns the map<string,string> 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestStringMap(ctx context.Context, thing map[string]string) (r map[string]string, err error) {
	fmt.Printf("testStringMap({")
	first := true
	for k, v := range thing {
		if first {
			first = false
		} else {
			fmt.Printf(", ")
		}
		fmt.Printf("%s => %s", k, v)
	}
	fmt.Printf("})\n")
	return thing, nil
}

// Prints 'testSet("{%s}")' where thing has been formatted into a string of  values
//  separated by commas and new lines
// @param set<i32> thing - the set<i32> to print
// @return set<i32> - returns the set<i32> 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestSet(ctx context.Context, thing []int32) (r []int32, err error) {
	fmt.Printf("testSet({")
	first := true
	for k := range thing {
		if first {
			first = false
		} else {
			fmt.Printf(", ")
		}
		fmt.Printf("%d", k)
	}
	fmt.Printf("})\n")
	return thing, nil
}

// Prints 'testList("{%s}")' where thing has been formatted into a string of  values
//  separated by commas and new lines
// @param list<i32> thing - the list<i32> to print
// @return list<i32> - returns the list<i32> 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestList(ctx context.Context, thing []int32) (r []int32, err error) {
	fmt.Printf("testList({")
	for i, v := range thing {
		if i != 0 {
			fmt.Printf(", ")
		}
		fmt.Printf("%d", v)
	}
	fmt.Printf("})\n")
	return thing, nil
}

// Prints 'testEnum("%d")' where thing has been formatted into it's numeric value
// @param Numberz thing - the Numberz to print
// @return Numberz - returns the Numberz 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestEnum(ctx context.Context, thing Numberz) (r Numberz, err error) {
	fmt.Printf("testEnum(%d)\n", thing)
	return thing, nil
}

// Prints 'testTypedef("%d")' with thing as '%d'
// @param UserId thing - the UserId to print
// @return UserId - returns the UserId 'thing'
//
// Parameters:
//  - Thing
func (p *printingHandler) TestTypedef(ctx context.Context, thing UserId) (r UserId, err error) {
	fmt.Printf("testTypedef(%d)\n", thing)
	return thing, nil
}

// Prints 'testMapMap("%d")' with hello as '%d'
// @param i32 hello - the i32 to print
// @return map<i32,map<i32,i32>> - returns a dictionary with these values:
//   {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 => 2, 3 => 3, 4 => 4, }, }
//
// Parameters:
//  - Hello
func (p *printingHandler) TestMapMap(ctx context.Context, hello int32) (r map[int32]map[int32]int32, err error) {
	fmt.Printf("testMapMap(%d)\n", hello)

	r = map[int32]map[int32]int32{
		-4: {-4: -4, -3: -3, -2: -2, -1: -1},
		4:  {4: 4, 3: 3, 2: 2, 1: 1},
	}
	return
}

// So you think you've got this all worked, out eh?
//
// Creates a the returned map with these values and prints it out:
//   { 1 => { 2 => argument,
//            3 => argument,
//          },
//     2 => { 6 => <empty Insanity struct>, },
//   }
// @return map<UserId, map<Numberz,Insanity>> - a map with the above values
//
// Parameters:
//  - Argument
func (p *printingHandler) TestInsanity(ctx context.Context, argument *Insanity) (r map[UserId]map[Numberz]*Insanity, err error) {
	fmt.Printf("testInsanity()\n")
	r = make(map[UserId]map[Numberz]*Insanity)
	r[1] = map[Numberz]*Insanity{
		2: argument,
		3: argument,
	}
	r[2] = map[Numberz]*Insanity{
		6: NewInsanity(),
	}
	return
}

// Prints 'testMulti()'
// @param byte arg0 -
// @param i32 arg1 -
// @param i64 arg2 -
// @param map<i16, string> arg3 -
// @param Numberz arg4 -
// @param UserId arg5 -
// @return Xtruct - returns an Xtruct with StringThing = "Hello2, ByteThing = arg0, I32Thing = arg1
//    and I64Thing = arg2
//
// Parameters:
//  - Arg0
//  - Arg1
//  - Arg2
//  - Arg3
//  - Arg4
//  - Arg5
func (p *printingHandler) TestMulti(ctx context.Context, arg0 int8, arg1 int32, arg2 int64, arg3 map[int16]string, arg4 Numberz, arg5 UserId) (r *Xtruct, err error) {
	fmt.Printf("testMulti()\n")
	r = NewXtruct()

	r.StringThing = "Hello2"
	r.ByteThing = arg0
	r.I32Thing = arg1
	r.I64Thing = arg2
	return
}

// Print 'testException(%s)' with arg as '%s'
// @param string arg - a string indication what type of exception to throw
// if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
// elsen if arg == "TException" throw TException
// else do not throw anything
//
// Parameters:
//  - Arg
func (p *printingHandler) TestException(ctx context.Context, arg string) (err error) {
	fmt.Printf("testException(%s)\n", arg)
	switch arg {
	case "Xception":
		e := NewXception()
		e.ErrorCode = 1001
		e.Message = arg
		return e
	case "TException":
		//lint:ignore ST1005 To be consistent with other language libraries.
		return errors.New("Just TException")
	}
	return
}

// Print 'testMultiException(%s, %s)' with arg0 as '%s' and arg1 as '%s'
// @param string arg - a string indication what type of exception to throw
// if arg0 == "Xception" throw Xception with errorCode = 1001 and message = "This is an Xception"
// elsen if arg0 == "Xception2" throw Xception2 with errorCode = 2002 and message = "This is an Xception2"
// else do not throw anything
// @return Xtruct - an Xtruct with StringThing = arg1
//
// Parameters:
//  - Arg0
//  - Arg1
func (p *printingHandler) TestMultiException(ctx context.Context, arg0 string, arg1 string) (r *Xtruct, err error) {
	fmt.Printf("testMultiException(%s, %s)\n", arg0, arg1)
	switch arg0 {

	case "Xception":
		e := NewXception()
		e.ErrorCode = 1001
		e.Message = "This is an Xception"
		return nil, e
	case "Xception2":
		e := NewXception2()
		e.ErrorCode = 2002
		e.StructThing = NewXtruct()
		e.StructThing.StringThing = "This is an Xception2"
		return nil, e
	default:
		r = NewXtruct()
		r.StringThing = arg1
		return
	}
}

// Print 'testOneway(%d): Sleeping...' with secondsToSleep as '%d'
// sleep 'secondsToSleep'
// Print 'testOneway(%d): done sleeping!' with secondsToSleep as '%d'
// @param i32 secondsToSleep - the number of seconds to sleep
//
// Parameters:
//  - SecondsToSleep
func (p *printingHandler) TestOneway(ctx context.Context, secondsToSleep int32) (err error) {
	fmt.Printf("testOneway(%d): Sleeping...\n", secondsToSleep)
	time.Sleep(time.Second * time.Duration(secondsToSleep))
	fmt.Printf("testOneway(%d): done sleeping!\n", secondsToSleep)
	return
}