blob: d91dde40323f4fe4bb5ba1b2608a7f5dd529e434 [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
29 . "github.com/apache/thrift/test/go/src/gen/thrifttest"
Jens Geyerf4598682014-05-08 23:18:44 +020030)
31
32var PrintingHandler = &printingHandler{}
33
34type printingHandler struct{}
35
36// Prints "testVoid()" and returns nothing.
taozlec0d384a2017-07-17 18:40:42 +020037func (p *printingHandler) TestVoid(ctx context.Context) (err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020038 fmt.Println("testVoid()")
39 return nil
40}
41
42// Prints 'testString("%s")' with thing as '%s'
43// @param string thing - the string to print
44// @return string - returns the string 'thing'
45//
46// Parameters:
47// - Thing
taozlec0d384a2017-07-17 18:40:42 +020048func (p *printingHandler) TestString(ctx context.Context, thing string) (r string, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020049 fmt.Printf("testString(\"%s\")\n", thing)
50 return thing, nil
51}
52
Jens Geyerfa0796d2015-10-16 21:33:39 +020053// Prints 'testBool("%t")' with thing as 'true' or 'false'
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090054// @param bool thing - the bool to print
55// @return bool - returns the bool 'thing'
56//
57// Parameters:
58// - Thing
taozlec0d384a2017-07-17 18:40:42 +020059func (p *printingHandler) TestBool(ctx context.Context, thing bool) (r bool, err error) {
Jens Geyerfa0796d2015-10-16 21:33:39 +020060 fmt.Printf("testBool(%t)\n", thing)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090061 return thing, nil
62}
63
Jens Geyerf4598682014-05-08 23:18:44 +020064// Prints 'testByte("%d")' with thing as '%d'
65// @param byte thing - the byte to print
66// @return byte - returns the byte 'thing'
67//
68// Parameters:
69// - Thing
taozlec0d384a2017-07-17 18:40:42 +020070func (p *printingHandler) TestByte(ctx context.Context, thing int8) (r int8, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020071 fmt.Printf("testByte(%d)\n", thing)
72 return thing, nil
73}
74
75// Prints 'testI32("%d")' with thing as '%d'
76// @param i32 thing - the i32 to print
77// @return i32 - returns the i32 'thing'
78//
79// Parameters:
80// - Thing
taozlec0d384a2017-07-17 18:40:42 +020081func (p *printingHandler) TestI32(ctx context.Context, thing int32) (r int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020082 fmt.Printf("testI32(%d)\n", thing)
83 return thing, nil
84}
85
86// Prints 'testI64("%d")' with thing as '%d'
87// @param i64 thing - the i64 to print
88// @return i64 - returns the i64 'thing'
89//
90// Parameters:
91// - Thing
taozlec0d384a2017-07-17 18:40:42 +020092func (p *printingHandler) TestI64(ctx context.Context, thing int64) (r int64, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020093 fmt.Printf("testI64(%d)\n", thing)
94 return thing, nil
95}
96
97// Prints 'testDouble("%f")' with thing as '%f'
98// @param double thing - the double to print
99// @return double - returns the double 'thing'
100//
101// Parameters:
102// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200103func (p *printingHandler) TestDouble(ctx context.Context, thing float64) (r float64, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200104 fmt.Printf("testDouble(%f)\n", thing)
105 return thing, nil
106}
107
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100108// Prints 'testBinary("%s")' where '%s' is a hex-formatted string of thing's data
109// @param []byte thing - the binary to print
110// @return []byte - returns the binary 'thing'
111//
112// Parameters:
113// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200114func (p *printingHandler) TestBinary(ctx context.Context, thing []byte) (r []byte, err error) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100115 fmt.Printf("testBinary(%s)\n", hex.EncodeToString(thing))
116 return thing, nil
117}
118
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100119// Prints 'testStruct("{%s}")' where thing has been formatted into a string of comma separated values
Jens Geyerf4598682014-05-08 23:18:44 +0200120// @param Xtruct thing - the Xtruct to print
121// @return Xtruct - returns the Xtruct 'thing'
122//
123// Parameters:
124// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200125func (p *printingHandler) TestStruct(ctx context.Context, thing *Xtruct) (r *Xtruct, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200126 fmt.Printf("testStruct({\"%s\", %d, %d, %d})\n", thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing)
127 return thing, err
128}
129
130// Prints 'testNest("{%s}")' where thing has been formatted into a string of the nested struct
131// @param Xtruct2 thing - the Xtruct2 to print
132// @return Xtruct2 - returns the Xtruct2 'thing'
133//
134// Parameters:
135// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200136func (p *printingHandler) TestNest(ctx context.Context, nest *Xtruct2) (r *Xtruct2, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200137 thing := nest.StructThing
138 fmt.Printf("testNest({%d, {\"%s\", %d, %d, %d}, %d})\n", nest.ByteThing, thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing, nest.I32Thing)
139 return nest, nil
140}
141
142// Prints 'testMap("{%s")' where thing has been formatted into a string of 'key => value' pairs
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100143// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200144// @param map<i32,i32> thing - the map<i32,i32> to print
145// @return map<i32,i32> - returns the map<i32,i32> 'thing'
146//
147// Parameters:
148// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200149func (p *printingHandler) TestMap(ctx context.Context, thing map[int32]int32) (r map[int32]int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200150 fmt.Printf("testMap({")
151 first := true
152 for k, v := range thing {
153 if first {
154 first = false
155 } else {
156 fmt.Printf(", ")
157 }
158 fmt.Printf("%d => %d", k, v)
159 }
160 fmt.Printf("})\n")
161 return thing, nil
162}
163
164// Prints 'testStringMap("{%s}")' where thing has been formatted into a string of 'key => value' pairs
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100165// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200166// @param map<string,string> thing - the map<string,string> to print
167// @return map<string,string> - returns the map<string,string> 'thing'
168//
169// Parameters:
170// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200171func (p *printingHandler) TestStringMap(ctx context.Context, thing map[string]string) (r map[string]string, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200172 fmt.Printf("testStringMap({")
173 first := true
174 for k, v := range thing {
175 if first {
176 first = false
177 } else {
178 fmt.Printf(", ")
179 }
180 fmt.Printf("%s => %s", k, v)
181 }
182 fmt.Printf("})\n")
183 return thing, nil
184}
185
186// Prints 'testSet("{%s}")' where thing has been formatted into a string of values
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100187// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200188// @param set<i32> thing - the set<i32> to print
189// @return set<i32> - returns the set<i32> 'thing'
190//
191// Parameters:
192// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200193func (p *printingHandler) TestSet(ctx context.Context, thing []int32) (r []int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200194 fmt.Printf("testSet({")
195 first := true
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -0700196 for k := range thing {
Jens Geyerf4598682014-05-08 23:18:44 +0200197 if first {
198 first = false
199 } else {
200 fmt.Printf(", ")
201 }
202 fmt.Printf("%d", k)
203 }
204 fmt.Printf("})\n")
205 return thing, nil
206}
207
208// Prints 'testList("{%s}")' where thing has been formatted into a string of values
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100209// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200210// @param list<i32> thing - the list<i32> to print
211// @return list<i32> - returns the list<i32> 'thing'
212//
213// Parameters:
214// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200215func (p *printingHandler) TestList(ctx context.Context, thing []int32) (r []int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200216 fmt.Printf("testList({")
217 for i, v := range thing {
218 if i != 0 {
219 fmt.Printf(", ")
220 }
221 fmt.Printf("%d", v)
222 }
223 fmt.Printf("})\n")
224 return thing, nil
225}
226
227// Prints 'testEnum("%d")' where thing has been formatted into it's numeric value
228// @param Numberz thing - the Numberz to print
229// @return Numberz - returns the Numberz 'thing'
230//
231// Parameters:
232// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200233func (p *printingHandler) TestEnum(ctx context.Context, thing Numberz) (r Numberz, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200234 fmt.Printf("testEnum(%d)\n", thing)
235 return thing, nil
236}
237
238// Prints 'testTypedef("%d")' with thing as '%d'
239// @param UserId thing - the UserId to print
240// @return UserId - returns the UserId 'thing'
241//
242// Parameters:
243// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200244func (p *printingHandler) TestTypedef(ctx context.Context, thing UserId) (r UserId, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200245 fmt.Printf("testTypedef(%d)\n", thing)
246 return thing, nil
247}
248
249// Prints 'testMapMap("%d")' with hello as '%d'
250// @param i32 hello - the i32 to print
251// @return map<i32,map<i32,i32>> - returns a dictionary with these values:
252// {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 => 2, 3 => 3, 4 => 4, }, }
253//
254// Parameters:
255// - Hello
taozlec0d384a2017-07-17 18:40:42 +0200256func (p *printingHandler) TestMapMap(ctx context.Context, hello int32) (r map[int32]map[int32]int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200257 fmt.Printf("testMapMap(%d)\n", hello)
258
259 r = map[int32]map[int32]int32{
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -0700260 -4: {-4: -4, -3: -3, -2: -2, -1: -1},
261 4: {4: 4, 3: 3, 2: 2, 1: 1},
Jens Geyerf4598682014-05-08 23:18:44 +0200262 }
263 return
264}
265
266// So you think you've got this all worked, out eh?
267//
268// Creates a the returned map with these values and prints it out:
269// { 1 => { 2 => argument,
270// 3 => argument,
271// },
272// 2 => { 6 => <empty Insanity struct>, },
273// }
274// @return map<UserId, map<Numberz,Insanity>> - a map with the above values
275//
276// Parameters:
277// - Argument
taozlec0d384a2017-07-17 18:40:42 +0200278func (p *printingHandler) TestInsanity(ctx context.Context, argument *Insanity) (r map[UserId]map[Numberz]*Insanity, err error) {
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900279 fmt.Printf("testInsanity()\n")
280 r = make(map[UserId]map[Numberz]*Insanity)
John Boiles57852792018-01-05 14:37:05 -0800281 r[1] = map[Numberz]*Insanity{
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900282 2: argument,
283 3: argument,
284 }
John Boiles57852792018-01-05 14:37:05 -0800285 r[2] = map[Numberz]*Insanity{
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900286 6: NewInsanity(),
287 }
288 return
Jens Geyerf4598682014-05-08 23:18:44 +0200289}
290
291// Prints 'testMulti()'
292// @param byte arg0 -
293// @param i32 arg1 -
294// @param i64 arg2 -
295// @param map<i16, string> arg3 -
296// @param Numberz arg4 -
297// @param UserId arg5 -
298// @return Xtruct - returns an Xtruct with StringThing = "Hello2, ByteThing = arg0, I32Thing = arg1
299// and I64Thing = arg2
300//
301// Parameters:
302// - Arg0
303// - Arg1
304// - Arg2
305// - Arg3
306// - Arg4
307// - Arg5
taozlec0d384a2017-07-17 18:40:42 +0200308func (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 +0200309 fmt.Printf("testMulti()\n")
310 r = NewXtruct()
311
312 r.StringThing = "Hello2"
313 r.ByteThing = arg0
314 r.I32Thing = arg1
315 r.I64Thing = arg2
316 return
317}
318
319// Print 'testException(%s)' with arg as '%s'
320// @param string arg - a string indication what type of exception to throw
321// if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
322// elsen if arg == "TException" throw TException
323// else do not throw anything
324//
325// Parameters:
326// - Arg
taozlec0d384a2017-07-17 18:40:42 +0200327func (p *printingHandler) TestException(ctx context.Context, arg string) (err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200328 fmt.Printf("testException(%s)\n", arg)
329 switch arg {
330 case "Xception":
331 e := NewXception()
332 e.ErrorCode = 1001
333 e.Message = arg
334 return e
335 case "TException":
336 return errors.New("Just TException")
337 }
338 return
339}
340
341// Print 'testMultiException(%s, %s)' with arg0 as '%s' and arg1 as '%s'
342// @param string arg - a string indication what type of exception to throw
343// if arg0 == "Xception" throw Xception with errorCode = 1001 and message = "This is an Xception"
344// elsen if arg0 == "Xception2" throw Xception2 with errorCode = 2002 and message = "This is an Xception2"
345// else do not throw anything
346// @return Xtruct - an Xtruct with StringThing = arg1
347//
348// Parameters:
349// - Arg0
350// - Arg1
taozlec0d384a2017-07-17 18:40:42 +0200351func (p *printingHandler) TestMultiException(ctx context.Context, arg0 string, arg1 string) (r *Xtruct, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200352 fmt.Printf("testMultiException(%s, %s)\n", arg0, arg1)
353 switch arg0 {
354
355 case "Xception":
356 e := NewXception()
357 e.ErrorCode = 1001
358 e.Message = "This is an Xception"
359 return nil, e
360 case "Xception2":
361 e := NewXception2()
362 e.ErrorCode = 2002
363 e.StructThing = NewXtruct()
364 e.StructThing.StringThing = "This is an Xception2"
365 return nil, e
366 default:
367 r = NewXtruct()
368 r.StringThing = arg1
369 return
370 }
371}
372
373// Print 'testOneway(%d): Sleeping...' with secondsToSleep as '%d'
374// sleep 'secondsToSleep'
375// Print 'testOneway(%d): done sleeping!' with secondsToSleep as '%d'
376// @param i32 secondsToSleep - the number of seconds to sleep
377//
378// Parameters:
379// - SecondsToSleep
taozlec0d384a2017-07-17 18:40:42 +0200380func (p *printingHandler) TestOneway(ctx context.Context, secondsToSleep int32) (err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200381 fmt.Printf("testOneway(%d): Sleeping...\n", secondsToSleep)
382 time.Sleep(time.Second * time.Duration(secondsToSleep))
383 fmt.Printf("testOneway(%d): done sleeping!\n", secondsToSleep)
384 return
385}