blob: b726373fe34e3f0cf48f31975c066b25fbbef88f [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' Wang17373a32021-08-26 11:04:27 -070029 //lint:ignore ST1001 allow dot import here
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070030 . "github.com/apache/thrift/test/go/src/gen/thrifttest"
Jens Geyerf4598682014-05-08 23:18:44 +020031)
32
33var PrintingHandler = &printingHandler{}
34
35type printingHandler struct{}
36
37// Prints "testVoid()" and returns nothing.
taozlec0d384a2017-07-17 18:40:42 +020038func (p *printingHandler) TestVoid(ctx context.Context) (err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020039 fmt.Println("testVoid()")
40 return nil
41}
42
43// Prints 'testString("%s")' with thing as '%s'
44// @param string thing - the string to print
45// @return string - returns the string 'thing'
46//
47// Parameters:
48// - Thing
taozlec0d384a2017-07-17 18:40:42 +020049func (p *printingHandler) TestString(ctx context.Context, thing string) (r string, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020050 fmt.Printf("testString(\"%s\")\n", thing)
51 return thing, nil
52}
53
Jens Geyerfa0796d2015-10-16 21:33:39 +020054// Prints 'testBool("%t")' with thing as 'true' or 'false'
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090055// @param bool thing - the bool to print
56// @return bool - returns the bool 'thing'
57//
58// Parameters:
59// - Thing
taozlec0d384a2017-07-17 18:40:42 +020060func (p *printingHandler) TestBool(ctx context.Context, thing bool) (r bool, err error) {
Jens Geyerfa0796d2015-10-16 21:33:39 +020061 fmt.Printf("testBool(%t)\n", thing)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090062 return thing, nil
63}
64
Jens Geyerf4598682014-05-08 23:18:44 +020065// Prints 'testByte("%d")' with thing as '%d'
66// @param byte thing - the byte to print
67// @return byte - returns the byte 'thing'
68//
69// Parameters:
70// - Thing
taozlec0d384a2017-07-17 18:40:42 +020071func (p *printingHandler) TestByte(ctx context.Context, thing int8) (r int8, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020072 fmt.Printf("testByte(%d)\n", thing)
73 return thing, nil
74}
75
76// Prints 'testI32("%d")' with thing as '%d'
77// @param i32 thing - the i32 to print
78// @return i32 - returns the i32 'thing'
79//
80// Parameters:
81// - Thing
taozlec0d384a2017-07-17 18:40:42 +020082func (p *printingHandler) TestI32(ctx context.Context, thing int32) (r int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020083 fmt.Printf("testI32(%d)\n", thing)
84 return thing, nil
85}
86
87// Prints 'testI64("%d")' with thing as '%d'
88// @param i64 thing - the i64 to print
89// @return i64 - returns the i64 'thing'
90//
91// Parameters:
92// - Thing
taozlec0d384a2017-07-17 18:40:42 +020093func (p *printingHandler) TestI64(ctx context.Context, thing int64) (r int64, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020094 fmt.Printf("testI64(%d)\n", thing)
95 return thing, nil
96}
97
98// Prints 'testDouble("%f")' with thing as '%f'
99// @param double thing - the double to print
100// @return double - returns the double 'thing'
101//
102// Parameters:
103// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200104func (p *printingHandler) TestDouble(ctx context.Context, thing float64) (r float64, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200105 fmt.Printf("testDouble(%f)\n", thing)
106 return thing, nil
107}
108
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100109// Prints 'testBinary("%s")' where '%s' is a hex-formatted string of thing's data
110// @param []byte thing - the binary to print
111// @return []byte - returns the binary 'thing'
112//
113// Parameters:
114// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200115func (p *printingHandler) TestBinary(ctx context.Context, thing []byte) (r []byte, err error) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100116 fmt.Printf("testBinary(%s)\n", hex.EncodeToString(thing))
117 return thing, nil
118}
119
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100120// Prints 'testStruct("{%s}")' where thing has been formatted into a string of comma separated values
Jens Geyerf4598682014-05-08 23:18:44 +0200121// @param Xtruct thing - the Xtruct to print
122// @return Xtruct - returns the Xtruct 'thing'
123//
124// Parameters:
125// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200126func (p *printingHandler) TestStruct(ctx context.Context, thing *Xtruct) (r *Xtruct, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200127 fmt.Printf("testStruct({\"%s\", %d, %d, %d})\n", thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing)
128 return thing, err
129}
130
131// Prints 'testNest("{%s}")' where thing has been formatted into a string of the nested struct
132// @param Xtruct2 thing - the Xtruct2 to print
133// @return Xtruct2 - returns the Xtruct2 'thing'
134//
135// Parameters:
136// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200137func (p *printingHandler) TestNest(ctx context.Context, nest *Xtruct2) (r *Xtruct2, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200138 thing := nest.StructThing
139 fmt.Printf("testNest({%d, {\"%s\", %d, %d, %d}, %d})\n", nest.ByteThing, thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing, nest.I32Thing)
140 return nest, nil
141}
142
143// Prints 'testMap("{%s")' where thing has been formatted into a string of 'key => value' pairs
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100144// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200145// @param map<i32,i32> thing - the map<i32,i32> to print
146// @return map<i32,i32> - returns the map<i32,i32> 'thing'
147//
148// Parameters:
149// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200150func (p *printingHandler) TestMap(ctx context.Context, thing map[int32]int32) (r map[int32]int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200151 fmt.Printf("testMap({")
152 first := true
153 for k, v := range thing {
154 if first {
155 first = false
156 } else {
157 fmt.Printf(", ")
158 }
159 fmt.Printf("%d => %d", k, v)
160 }
161 fmt.Printf("})\n")
162 return thing, nil
163}
164
165// Prints 'testStringMap("{%s}")' where thing has been formatted into a string of 'key => value' pairs
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100166// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200167// @param map<string,string> thing - the map<string,string> to print
168// @return map<string,string> - returns the map<string,string> 'thing'
169//
170// Parameters:
171// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200172func (p *printingHandler) TestStringMap(ctx context.Context, thing map[string]string) (r map[string]string, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200173 fmt.Printf("testStringMap({")
174 first := true
175 for k, v := range thing {
176 if first {
177 first = false
178 } else {
179 fmt.Printf(", ")
180 }
181 fmt.Printf("%s => %s", k, v)
182 }
183 fmt.Printf("})\n")
184 return thing, nil
185}
186
187// Prints 'testSet("{%s}")' where thing has been formatted into a string of values
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100188// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200189// @param set<i32> thing - the set<i32> to print
190// @return set<i32> - returns the set<i32> 'thing'
191//
192// Parameters:
193// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200194func (p *printingHandler) TestSet(ctx context.Context, thing []int32) (r []int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200195 fmt.Printf("testSet({")
196 first := true
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -0700197 for k := range thing {
Jens Geyerf4598682014-05-08 23:18:44 +0200198 if first {
199 first = false
200 } else {
201 fmt.Printf(", ")
202 }
203 fmt.Printf("%d", k)
204 }
205 fmt.Printf("})\n")
206 return thing, nil
207}
208
209// Prints 'testList("{%s}")' where thing has been formatted into a string of values
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100210// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200211// @param list<i32> thing - the list<i32> to print
212// @return list<i32> - returns the list<i32> 'thing'
213//
214// Parameters:
215// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200216func (p *printingHandler) TestList(ctx context.Context, thing []int32) (r []int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200217 fmt.Printf("testList({")
218 for i, v := range thing {
219 if i != 0 {
220 fmt.Printf(", ")
221 }
222 fmt.Printf("%d", v)
223 }
224 fmt.Printf("})\n")
225 return thing, nil
226}
227
228// Prints 'testEnum("%d")' where thing has been formatted into it's numeric value
229// @param Numberz thing - the Numberz to print
230// @return Numberz - returns the Numberz 'thing'
231//
232// Parameters:
233// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200234func (p *printingHandler) TestEnum(ctx context.Context, thing Numberz) (r Numberz, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200235 fmt.Printf("testEnum(%d)\n", thing)
236 return thing, nil
237}
238
239// Prints 'testTypedef("%d")' with thing as '%d'
240// @param UserId thing - the UserId to print
241// @return UserId - returns the UserId 'thing'
242//
243// Parameters:
244// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200245func (p *printingHandler) TestTypedef(ctx context.Context, thing UserId) (r UserId, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200246 fmt.Printf("testTypedef(%d)\n", thing)
247 return thing, nil
248}
249
250// Prints 'testMapMap("%d")' with hello as '%d'
251// @param i32 hello - the i32 to print
252// @return map<i32,map<i32,i32>> - returns a dictionary with these values:
253// {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 => 2, 3 => 3, 4 => 4, }, }
254//
255// Parameters:
256// - Hello
taozlec0d384a2017-07-17 18:40:42 +0200257func (p *printingHandler) TestMapMap(ctx context.Context, hello int32) (r map[int32]map[int32]int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200258 fmt.Printf("testMapMap(%d)\n", hello)
259
260 r = map[int32]map[int32]int32{
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -0700261 -4: {-4: -4, -3: -3, -2: -2, -1: -1},
262 4: {4: 4, 3: 3, 2: 2, 1: 1},
Jens Geyerf4598682014-05-08 23:18:44 +0200263 }
264 return
265}
266
267// So you think you've got this all worked, out eh?
268//
269// Creates a the returned map with these values and prints it out:
270// { 1 => { 2 => argument,
271// 3 => argument,
272// },
273// 2 => { 6 => <empty Insanity struct>, },
274// }
275// @return map<UserId, map<Numberz,Insanity>> - a map with the above values
276//
277// Parameters:
278// - Argument
taozlec0d384a2017-07-17 18:40:42 +0200279func (p *printingHandler) TestInsanity(ctx context.Context, argument *Insanity) (r map[UserId]map[Numberz]*Insanity, err error) {
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900280 fmt.Printf("testInsanity()\n")
281 r = make(map[UserId]map[Numberz]*Insanity)
John Boiles57852792018-01-05 14:37:05 -0800282 r[1] = map[Numberz]*Insanity{
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900283 2: argument,
284 3: argument,
285 }
John Boiles57852792018-01-05 14:37:05 -0800286 r[2] = map[Numberz]*Insanity{
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900287 6: NewInsanity(),
288 }
289 return
Jens Geyerf4598682014-05-08 23:18:44 +0200290}
291
292// Prints 'testMulti()'
293// @param byte arg0 -
294// @param i32 arg1 -
295// @param i64 arg2 -
296// @param map<i16, string> arg3 -
297// @param Numberz arg4 -
298// @param UserId arg5 -
299// @return Xtruct - returns an Xtruct with StringThing = "Hello2, ByteThing = arg0, I32Thing = arg1
300// and I64Thing = arg2
301//
302// Parameters:
303// - Arg0
304// - Arg1
305// - Arg2
306// - Arg3
307// - Arg4
308// - Arg5
taozlec0d384a2017-07-17 18:40:42 +0200309func (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 +0200310 fmt.Printf("testMulti()\n")
311 r = NewXtruct()
312
313 r.StringThing = "Hello2"
314 r.ByteThing = arg0
315 r.I32Thing = arg1
316 r.I64Thing = arg2
317 return
318}
319
320// Print 'testException(%s)' with arg as '%s'
321// @param string arg - a string indication what type of exception to throw
322// if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
323// elsen if arg == "TException" throw TException
324// else do not throw anything
325//
326// Parameters:
327// - Arg
taozlec0d384a2017-07-17 18:40:42 +0200328func (p *printingHandler) TestException(ctx context.Context, arg string) (err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200329 fmt.Printf("testException(%s)\n", arg)
330 switch arg {
331 case "Xception":
332 e := NewXception()
333 e.ErrorCode = 1001
334 e.Message = arg
335 return e
336 case "TException":
Yuxuan 'fishy' Wang17373a32021-08-26 11:04:27 -0700337 //lint:ignore ST1005 To be consistent with other language libraries.
Jens Geyerf4598682014-05-08 23:18:44 +0200338 return errors.New("Just TException")
339 }
340 return
341}
342
343// Print 'testMultiException(%s, %s)' with arg0 as '%s' and arg1 as '%s'
344// @param string arg - a string indication what type of exception to throw
345// if arg0 == "Xception" throw Xception with errorCode = 1001 and message = "This is an Xception"
346// elsen if arg0 == "Xception2" throw Xception2 with errorCode = 2002 and message = "This is an Xception2"
347// else do not throw anything
348// @return Xtruct - an Xtruct with StringThing = arg1
349//
350// Parameters:
351// - Arg0
352// - Arg1
taozlec0d384a2017-07-17 18:40:42 +0200353func (p *printingHandler) TestMultiException(ctx context.Context, arg0 string, arg1 string) (r *Xtruct, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200354 fmt.Printf("testMultiException(%s, %s)\n", arg0, arg1)
355 switch arg0 {
356
357 case "Xception":
358 e := NewXception()
359 e.ErrorCode = 1001
360 e.Message = "This is an Xception"
361 return nil, e
362 case "Xception2":
363 e := NewXception2()
364 e.ErrorCode = 2002
365 e.StructThing = NewXtruct()
366 e.StructThing.StringThing = "This is an Xception2"
367 return nil, e
368 default:
369 r = NewXtruct()
370 r.StringThing = arg1
371 return
372 }
373}
374
375// Print 'testOneway(%d): Sleeping...' with secondsToSleep as '%d'
376// sleep 'secondsToSleep'
377// Print 'testOneway(%d): done sleeping!' with secondsToSleep as '%d'
378// @param i32 secondsToSleep - the number of seconds to sleep
379//
380// Parameters:
381// - SecondsToSleep
taozlec0d384a2017-07-17 18:40:42 +0200382func (p *printingHandler) TestOneway(ctx context.Context, secondsToSleep int32) (err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200383 fmt.Printf("testOneway(%d): Sleeping...\n", secondsToSleep)
384 time.Sleep(time.Second * time.Duration(secondsToSleep))
385 fmt.Printf("testOneway(%d): done sleeping!\n", secondsToSleep)
386 return
387}