blob: 0939a0147937d61174620023fd2cd4c66ba8ab48 [file] [log] [blame]
Jens Geyerf4598682014-05-08 23:18:44 +02001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20package common
21
22import (
John Boiles57852792018-01-05 14:37:05 -080023 "context"
24 "encoding/hex"
Jens Geyerf4598682014-05-08 23:18:44 +020025 "errors"
26 "fmt"
Jens Geyerf4598682014-05-08 23:18:44 +020027 "time"
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070028
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -070029 "github.com/apache/thrift/lib/go/thrift"
30
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -070031 //lint:ignore ST1001 allow dot import here
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070032 . "github.com/apache/thrift/test/go/src/gen/thrifttest"
Jens Geyerf4598682014-05-08 23:18:44 +020033)
34
35var PrintingHandler = &printingHandler{}
36
37type printingHandler struct{}
38
39// Prints "testVoid()" and returns nothing.
taozlec0d384a2017-07-17 18:40:42 +020040func (p *printingHandler) TestVoid(ctx context.Context) (err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020041 fmt.Println("testVoid()")
42 return nil
43}
44
45// Prints 'testString("%s")' with thing as '%s'
46// @param string thing - the string to print
47// @return string - returns the string 'thing'
48//
49// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -070050// - Thing
taozlec0d384a2017-07-17 18:40:42 +020051func (p *printingHandler) TestString(ctx context.Context, thing string) (r string, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020052 fmt.Printf("testString(\"%s\")\n", thing)
53 return thing, nil
54}
55
Jens Geyerfa0796d2015-10-16 21:33:39 +020056// Prints 'testBool("%t")' with thing as 'true' or 'false'
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090057// @param bool thing - the bool to print
58// @return bool - returns the bool 'thing'
59//
60// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -070061// - Thing
taozlec0d384a2017-07-17 18:40:42 +020062func (p *printingHandler) TestBool(ctx context.Context, thing bool) (r bool, err error) {
Jens Geyerfa0796d2015-10-16 21:33:39 +020063 fmt.Printf("testBool(%t)\n", thing)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090064 return thing, nil
65}
66
Jens Geyerf4598682014-05-08 23:18:44 +020067// Prints 'testByte("%d")' with thing as '%d'
68// @param byte thing - the byte to print
69// @return byte - returns the byte 'thing'
70//
71// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -070072// - Thing
taozlec0d384a2017-07-17 18:40:42 +020073func (p *printingHandler) TestByte(ctx context.Context, thing int8) (r int8, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020074 fmt.Printf("testByte(%d)\n", thing)
75 return thing, nil
76}
77
78// Prints 'testI32("%d")' with thing as '%d'
79// @param i32 thing - the i32 to print
80// @return i32 - returns the i32 'thing'
81//
82// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -070083// - Thing
taozlec0d384a2017-07-17 18:40:42 +020084func (p *printingHandler) TestI32(ctx context.Context, thing int32) (r int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020085 fmt.Printf("testI32(%d)\n", thing)
86 return thing, nil
87}
88
89// Prints 'testI64("%d")' with thing as '%d'
90// @param i64 thing - the i64 to print
91// @return i64 - returns the i64 'thing'
92//
93// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -070094// - Thing
taozlec0d384a2017-07-17 18:40:42 +020095func (p *printingHandler) TestI64(ctx context.Context, thing int64) (r int64, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020096 fmt.Printf("testI64(%d)\n", thing)
97 return thing, nil
98}
99
100// Prints 'testDouble("%f")' with thing as '%f'
101// @param double thing - the double to print
102// @return double - returns the double 'thing'
103//
104// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700105// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200106func (p *printingHandler) TestDouble(ctx context.Context, thing float64) (r float64, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200107 fmt.Printf("testDouble(%f)\n", thing)
108 return thing, nil
109}
110
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100111// Prints 'testBinary("%s")' where '%s' is a hex-formatted string of thing's data
112// @param []byte thing - the binary to print
113// @return []byte - returns the binary 'thing'
114//
115// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700116// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200117func (p *printingHandler) TestBinary(ctx context.Context, thing []byte) (r []byte, err error) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100118 fmt.Printf("testBinary(%s)\n", hex.EncodeToString(thing))
119 return thing, nil
120}
121
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700122// Prints 'testUuid("%s")' where '%s' is the uuid given. Note that the uuid byte order should be correct.
123// @param uuid thing - the uuid to print
124// @return uuid - returns the uuid 'thing'
125//
126// Parameters:
127// - Thing
128func (p *printingHandler) TestUuid(ctx context.Context, thing thrift.Tuuid) (r thrift.Tuuid, err error) {
129 fmt.Printf("testUuid(%s)\n", thing.String())
130 return thing, nil
131}
132
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100133// Prints 'testStruct("{%s}")' where thing has been formatted into a string of comma separated values
Jens Geyerf4598682014-05-08 23:18:44 +0200134// @param Xtruct thing - the Xtruct to print
135// @return Xtruct - returns the Xtruct 'thing'
136//
137// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700138// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200139func (p *printingHandler) TestStruct(ctx context.Context, thing *Xtruct) (r *Xtruct, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200140 fmt.Printf("testStruct({\"%s\", %d, %d, %d})\n", thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing)
141 return thing, err
142}
143
144// Prints 'testNest("{%s}")' where thing has been formatted into a string of the nested struct
145// @param Xtruct2 thing - the Xtruct2 to print
146// @return Xtruct2 - returns the Xtruct2 'thing'
147//
148// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700149// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200150func (p *printingHandler) TestNest(ctx context.Context, nest *Xtruct2) (r *Xtruct2, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200151 thing := nest.StructThing
152 fmt.Printf("testNest({%d, {\"%s\", %d, %d, %d}, %d})\n", nest.ByteThing, thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing, nest.I32Thing)
153 return nest, nil
154}
155
156// Prints 'testMap("{%s")' where thing has been formatted into a string of 'key => value' pairs
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700157//
158// separated by commas and new lines
159//
Jens Geyerf4598682014-05-08 23:18:44 +0200160// @param map<i32,i32> thing - the map<i32,i32> to print
161// @return map<i32,i32> - returns the map<i32,i32> 'thing'
162//
163// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700164// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200165func (p *printingHandler) TestMap(ctx context.Context, thing map[int32]int32) (r map[int32]int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200166 fmt.Printf("testMap({")
167 first := true
168 for k, v := range thing {
169 if first {
170 first = false
171 } else {
172 fmt.Printf(", ")
173 }
174 fmt.Printf("%d => %d", k, v)
175 }
176 fmt.Printf("})\n")
177 return thing, nil
178}
179
180// Prints 'testStringMap("{%s}")' where thing has been formatted into a string of 'key => value' pairs
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700181//
182// separated by commas and new lines
183//
Jens Geyerf4598682014-05-08 23:18:44 +0200184// @param map<string,string> thing - the map<string,string> to print
185// @return map<string,string> - returns the map<string,string> 'thing'
186//
187// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700188// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200189func (p *printingHandler) TestStringMap(ctx context.Context, thing map[string]string) (r map[string]string, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200190 fmt.Printf("testStringMap({")
191 first := true
192 for k, v := range thing {
193 if first {
194 first = false
195 } else {
196 fmt.Printf(", ")
197 }
198 fmt.Printf("%s => %s", k, v)
199 }
200 fmt.Printf("})\n")
201 return thing, nil
202}
203
204// Prints 'testSet("{%s}")' where thing has been formatted into a string of values
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700205//
206// separated by commas and new lines
207//
Jens Geyerf4598682014-05-08 23:18:44 +0200208// @param set<i32> thing - the set<i32> to print
209// @return set<i32> - returns the set<i32> 'thing'
210//
211// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700212// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200213func (p *printingHandler) TestSet(ctx context.Context, thing []int32) (r []int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200214 fmt.Printf("testSet({")
215 first := true
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -0700216 for k := range thing {
Jens Geyerf4598682014-05-08 23:18:44 +0200217 if first {
218 first = false
219 } else {
220 fmt.Printf(", ")
221 }
222 fmt.Printf("%d", k)
223 }
224 fmt.Printf("})\n")
225 return thing, nil
226}
227
228// Prints 'testList("{%s}")' where thing has been formatted into a string of values
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700229//
230// separated by commas and new lines
231//
Jens Geyerf4598682014-05-08 23:18:44 +0200232// @param list<i32> thing - the list<i32> to print
233// @return list<i32> - returns the list<i32> 'thing'
234//
235// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700236// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200237func (p *printingHandler) TestList(ctx context.Context, thing []int32) (r []int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200238 fmt.Printf("testList({")
239 for i, v := range thing {
240 if i != 0 {
241 fmt.Printf(", ")
242 }
243 fmt.Printf("%d", v)
244 }
245 fmt.Printf("})\n")
246 return thing, nil
247}
248
249// Prints 'testEnum("%d")' where thing has been formatted into it's numeric value
250// @param Numberz thing - the Numberz to print
251// @return Numberz - returns the Numberz 'thing'
252//
253// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700254// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200255func (p *printingHandler) TestEnum(ctx context.Context, thing Numberz) (r Numberz, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200256 fmt.Printf("testEnum(%d)\n", thing)
257 return thing, nil
258}
259
260// Prints 'testTypedef("%d")' with thing as '%d'
261// @param UserId thing - the UserId to print
262// @return UserId - returns the UserId 'thing'
263//
264// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700265// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200266func (p *printingHandler) TestTypedef(ctx context.Context, thing UserId) (r UserId, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200267 fmt.Printf("testTypedef(%d)\n", thing)
268 return thing, nil
269}
270
271// Prints 'testMapMap("%d")' with hello as '%d'
272// @param i32 hello - the i32 to print
273// @return map<i32,map<i32,i32>> - returns a dictionary with these values:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700274//
275// {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 => 2, 3 => 3, 4 => 4, }, }
Jens Geyerf4598682014-05-08 23:18:44 +0200276//
277// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700278// - Hello
taozlec0d384a2017-07-17 18:40:42 +0200279func (p *printingHandler) TestMapMap(ctx context.Context, hello int32) (r map[int32]map[int32]int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200280 fmt.Printf("testMapMap(%d)\n", hello)
281
282 r = map[int32]map[int32]int32{
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -0700283 -4: {-4: -4, -3: -3, -2: -2, -1: -1},
284 4: {4: 4, 3: 3, 2: 2, 1: 1},
Jens Geyerf4598682014-05-08 23:18:44 +0200285 }
286 return
287}
288
289// So you think you've got this all worked, out eh?
290//
291// Creates a the returned map with these values and prints it out:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700292//
293// { 1 => { 2 => argument,
294// 3 => argument,
295// },
296// 2 => { 6 => <empty Insanity struct>, },
297// }
298//
Jens Geyerf4598682014-05-08 23:18:44 +0200299// @return map<UserId, map<Numberz,Insanity>> - a map with the above values
300//
301// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700302// - Argument
taozlec0d384a2017-07-17 18:40:42 +0200303func (p *printingHandler) TestInsanity(ctx context.Context, argument *Insanity) (r map[UserId]map[Numberz]*Insanity, err error) {
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900304 fmt.Printf("testInsanity()\n")
305 r = make(map[UserId]map[Numberz]*Insanity)
John Boiles57852792018-01-05 14:37:05 -0800306 r[1] = map[Numberz]*Insanity{
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900307 2: argument,
308 3: argument,
309 }
John Boiles57852792018-01-05 14:37:05 -0800310 r[2] = map[Numberz]*Insanity{
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900311 6: NewInsanity(),
312 }
313 return
Jens Geyerf4598682014-05-08 23:18:44 +0200314}
315
316// Prints 'testMulti()'
317// @param byte arg0 -
318// @param i32 arg1 -
319// @param i64 arg2 -
320// @param map<i16, string> arg3 -
321// @param Numberz arg4 -
322// @param UserId arg5 -
323// @return Xtruct - returns an Xtruct with StringThing = "Hello2, ByteThing = arg0, I32Thing = arg1
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700324//
325// and I64Thing = arg2
Jens Geyerf4598682014-05-08 23:18:44 +0200326//
327// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700328// - Arg0
329// - Arg1
330// - Arg2
331// - Arg3
332// - Arg4
333// - Arg5
taozlec0d384a2017-07-17 18:40:42 +0200334func (p *printingHandler) TestMulti(ctx context.Context, arg0 int8, arg1 int32, arg2 int64, arg3 map[int16]string, arg4 Numberz, arg5 UserId) (r *Xtruct, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200335 fmt.Printf("testMulti()\n")
336 r = NewXtruct()
337
338 r.StringThing = "Hello2"
339 r.ByteThing = arg0
340 r.I32Thing = arg1
341 r.I64Thing = arg2
342 return
343}
344
345// Print 'testException(%s)' with arg as '%s'
346// @param string arg - a string indication what type of exception to throw
347// if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
348// elsen if arg == "TException" throw TException
349// else do not throw anything
350//
351// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700352// - Arg
taozlec0d384a2017-07-17 18:40:42 +0200353func (p *printingHandler) TestException(ctx context.Context, arg string) (err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200354 fmt.Printf("testException(%s)\n", arg)
355 switch arg {
356 case "Xception":
357 e := NewXception()
358 e.ErrorCode = 1001
359 e.Message = arg
360 return e
361 case "TException":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -0700362 //lint:ignore ST1005 To be consistent with other language libraries.
Jens Geyerf4598682014-05-08 23:18:44 +0200363 return errors.New("Just TException")
364 }
365 return
366}
367
368// Print 'testMultiException(%s, %s)' with arg0 as '%s' and arg1 as '%s'
369// @param string arg - a string indication what type of exception to throw
370// if arg0 == "Xception" throw Xception with errorCode = 1001 and message = "This is an Xception"
371// elsen if arg0 == "Xception2" throw Xception2 with errorCode = 2002 and message = "This is an Xception2"
372// else do not throw anything
373// @return Xtruct - an Xtruct with StringThing = arg1
374//
375// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700376// - Arg0
377// - Arg1
taozlec0d384a2017-07-17 18:40:42 +0200378func (p *printingHandler) TestMultiException(ctx context.Context, arg0 string, arg1 string) (r *Xtruct, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200379 fmt.Printf("testMultiException(%s, %s)\n", arg0, arg1)
380 switch arg0 {
381
382 case "Xception":
383 e := NewXception()
384 e.ErrorCode = 1001
385 e.Message = "This is an Xception"
386 return nil, e
387 case "Xception2":
388 e := NewXception2()
389 e.ErrorCode = 2002
390 e.StructThing = NewXtruct()
391 e.StructThing.StringThing = "This is an Xception2"
392 return nil, e
393 default:
394 r = NewXtruct()
395 r.StringThing = arg1
396 return
397 }
398}
399
400// Print 'testOneway(%d): Sleeping...' with secondsToSleep as '%d'
401// sleep 'secondsToSleep'
402// Print 'testOneway(%d): done sleeping!' with secondsToSleep as '%d'
403// @param i32 secondsToSleep - the number of seconds to sleep
404//
405// Parameters:
Yuxuan 'fishy' Wang2acfe0f2022-10-21 10:27:40 -0700406// - SecondsToSleep
taozlec0d384a2017-07-17 18:40:42 +0200407func (p *printingHandler) TestOneway(ctx context.Context, secondsToSleep int32) (err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200408 fmt.Printf("testOneway(%d): Sleeping...\n", secondsToSleep)
409 time.Sleep(time.Second * time.Duration(secondsToSleep))
410 fmt.Printf("testOneway(%d): done sleeping!\n", secondsToSleep)
411 return
412}