blob: 2b22d0c9773c72edcf6201dc574b976df2e740d0 [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"
27 . "gen/thrifttest"
28 "time"
29)
30
31var PrintingHandler = &printingHandler{}
32
33type printingHandler struct{}
34
35// Prints "testVoid()" and returns nothing.
taozlec0d384a2017-07-17 18:40:42 +020036func (p *printingHandler) TestVoid(ctx context.Context) (err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020037 fmt.Println("testVoid()")
38 return nil
39}
40
41// Prints 'testString("%s")' with thing as '%s'
42// @param string thing - the string to print
43// @return string - returns the string 'thing'
44//
45// Parameters:
46// - Thing
taozlec0d384a2017-07-17 18:40:42 +020047func (p *printingHandler) TestString(ctx context.Context, thing string) (r string, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020048 fmt.Printf("testString(\"%s\")\n", thing)
49 return thing, nil
50}
51
Jens Geyerfa0796d2015-10-16 21:33:39 +020052// Prints 'testBool("%t")' with thing as 'true' or 'false'
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090053// @param bool thing - the bool to print
54// @return bool - returns the bool 'thing'
55//
56// Parameters:
57// - Thing
taozlec0d384a2017-07-17 18:40:42 +020058func (p *printingHandler) TestBool(ctx context.Context, thing bool) (r bool, err error) {
Jens Geyerfa0796d2015-10-16 21:33:39 +020059 fmt.Printf("testBool(%t)\n", thing)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090060 return thing, nil
61}
62
Jens Geyerf4598682014-05-08 23:18:44 +020063// Prints 'testByte("%d")' with thing as '%d'
64// @param byte thing - the byte to print
65// @return byte - returns the byte 'thing'
66//
67// Parameters:
68// - Thing
taozlec0d384a2017-07-17 18:40:42 +020069func (p *printingHandler) TestByte(ctx context.Context, thing int8) (r int8, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020070 fmt.Printf("testByte(%d)\n", thing)
71 return thing, nil
72}
73
74// Prints 'testI32("%d")' with thing as '%d'
75// @param i32 thing - the i32 to print
76// @return i32 - returns the i32 'thing'
77//
78// Parameters:
79// - Thing
taozlec0d384a2017-07-17 18:40:42 +020080func (p *printingHandler) TestI32(ctx context.Context, thing int32) (r int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020081 fmt.Printf("testI32(%d)\n", thing)
82 return thing, nil
83}
84
85// Prints 'testI64("%d")' with thing as '%d'
86// @param i64 thing - the i64 to print
87// @return i64 - returns the i64 'thing'
88//
89// Parameters:
90// - Thing
taozlec0d384a2017-07-17 18:40:42 +020091func (p *printingHandler) TestI64(ctx context.Context, thing int64) (r int64, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020092 fmt.Printf("testI64(%d)\n", thing)
93 return thing, nil
94}
95
96// Prints 'testDouble("%f")' with thing as '%f'
97// @param double thing - the double to print
98// @return double - returns the double 'thing'
99//
100// Parameters:
101// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200102func (p *printingHandler) TestDouble(ctx context.Context, thing float64) (r float64, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200103 fmt.Printf("testDouble(%f)\n", thing)
104 return thing, nil
105}
106
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100107// Prints 'testBinary("%s")' where '%s' is a hex-formatted string of thing's data
108// @param []byte thing - the binary to print
109// @return []byte - returns the binary 'thing'
110//
111// Parameters:
112// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200113func (p *printingHandler) TestBinary(ctx context.Context, thing []byte) (r []byte, err error) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100114 fmt.Printf("testBinary(%s)\n", hex.EncodeToString(thing))
115 return thing, nil
116}
117
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100118// Prints 'testStruct("{%s}")' where thing has been formatted into a string of comma separated values
Jens Geyerf4598682014-05-08 23:18:44 +0200119// @param Xtruct thing - the Xtruct to print
120// @return Xtruct - returns the Xtruct 'thing'
121//
122// Parameters:
123// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200124func (p *printingHandler) TestStruct(ctx context.Context, thing *Xtruct) (r *Xtruct, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200125 fmt.Printf("testStruct({\"%s\", %d, %d, %d})\n", thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing)
126 return thing, err
127}
128
129// Prints 'testNest("{%s}")' where thing has been formatted into a string of the nested struct
130// @param Xtruct2 thing - the Xtruct2 to print
131// @return Xtruct2 - returns the Xtruct2 'thing'
132//
133// Parameters:
134// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200135func (p *printingHandler) TestNest(ctx context.Context, nest *Xtruct2) (r *Xtruct2, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200136 thing := nest.StructThing
137 fmt.Printf("testNest({%d, {\"%s\", %d, %d, %d}, %d})\n", nest.ByteThing, thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing, nest.I32Thing)
138 return nest, nil
139}
140
141// Prints 'testMap("{%s")' where thing has been formatted into a string of 'key => value' pairs
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100142// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200143// @param map<i32,i32> thing - the map<i32,i32> to print
144// @return map<i32,i32> - returns the map<i32,i32> 'thing'
145//
146// Parameters:
147// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200148func (p *printingHandler) TestMap(ctx context.Context, thing map[int32]int32) (r map[int32]int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200149 fmt.Printf("testMap({")
150 first := true
151 for k, v := range thing {
152 if first {
153 first = false
154 } else {
155 fmt.Printf(", ")
156 }
157 fmt.Printf("%d => %d", k, v)
158 }
159 fmt.Printf("})\n")
160 return thing, nil
161}
162
163// Prints 'testStringMap("{%s}")' where thing has been formatted into a string of 'key => value' pairs
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100164// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200165// @param map<string,string> thing - the map<string,string> to print
166// @return map<string,string> - returns the map<string,string> 'thing'
167//
168// Parameters:
169// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200170func (p *printingHandler) TestStringMap(ctx context.Context, thing map[string]string) (r map[string]string, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200171 fmt.Printf("testStringMap({")
172 first := true
173 for k, v := range thing {
174 if first {
175 first = false
176 } else {
177 fmt.Printf(", ")
178 }
179 fmt.Printf("%s => %s", k, v)
180 }
181 fmt.Printf("})\n")
182 return thing, nil
183}
184
185// Prints 'testSet("{%s}")' where thing has been formatted into a string of values
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100186// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200187// @param set<i32> thing - the set<i32> to print
188// @return set<i32> - returns the set<i32> 'thing'
189//
190// Parameters:
191// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200192func (p *printingHandler) TestSet(ctx context.Context, thing []int32) (r []int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200193 fmt.Printf("testSet({")
194 first := true
195 for k, _ := range thing {
196 if first {
197 first = false
198 } else {
199 fmt.Printf(", ")
200 }
201 fmt.Printf("%d", k)
202 }
203 fmt.Printf("})\n")
204 return thing, nil
205}
206
207// Prints 'testList("{%s}")' where thing has been formatted into a string of values
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100208// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200209// @param list<i32> thing - the list<i32> to print
210// @return list<i32> - returns the list<i32> 'thing'
211//
212// Parameters:
213// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200214func (p *printingHandler) TestList(ctx context.Context, thing []int32) (r []int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200215 fmt.Printf("testList({")
216 for i, v := range thing {
217 if i != 0 {
218 fmt.Printf(", ")
219 }
220 fmt.Printf("%d", v)
221 }
222 fmt.Printf("})\n")
223 return thing, nil
224}
225
226// Prints 'testEnum("%d")' where thing has been formatted into it's numeric value
227// @param Numberz thing - the Numberz to print
228// @return Numberz - returns the Numberz 'thing'
229//
230// Parameters:
231// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200232func (p *printingHandler) TestEnum(ctx context.Context, thing Numberz) (r Numberz, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200233 fmt.Printf("testEnum(%d)\n", thing)
234 return thing, nil
235}
236
237// Prints 'testTypedef("%d")' with thing as '%d'
238// @param UserId thing - the UserId to print
239// @return UserId - returns the UserId 'thing'
240//
241// Parameters:
242// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200243func (p *printingHandler) TestTypedef(ctx context.Context, thing UserId) (r UserId, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200244 fmt.Printf("testTypedef(%d)\n", thing)
245 return thing, nil
246}
247
248// Prints 'testMapMap("%d")' with hello as '%d'
249// @param i32 hello - the i32 to print
250// @return map<i32,map<i32,i32>> - returns a dictionary with these values:
251// {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 => 2, 3 => 3, 4 => 4, }, }
252//
253// Parameters:
254// - Hello
taozlec0d384a2017-07-17 18:40:42 +0200255func (p *printingHandler) TestMapMap(ctx context.Context, hello int32) (r map[int32]map[int32]int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200256 fmt.Printf("testMapMap(%d)\n", hello)
257
258 r = map[int32]map[int32]int32{
259 -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1},
260 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1},
261 }
262 return
263}
264
265// So you think you've got this all worked, out eh?
266//
267// Creates a the returned map with these values and prints it out:
268// { 1 => { 2 => argument,
269// 3 => argument,
270// },
271// 2 => { 6 => <empty Insanity struct>, },
272// }
273// @return map<UserId, map<Numberz,Insanity>> - a map with the above values
274//
275// Parameters:
276// - Argument
taozlec0d384a2017-07-17 18:40:42 +0200277func (p *printingHandler) TestInsanity(ctx context.Context, argument *Insanity) (r map[UserId]map[Numberz]*Insanity, err error) {
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900278 fmt.Printf("testInsanity()\n")
279 r = make(map[UserId]map[Numberz]*Insanity)
John Boiles57852792018-01-05 14:37:05 -0800280 r[1] = map[Numberz]*Insanity{
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900281 2: argument,
282 3: argument,
283 }
John Boiles57852792018-01-05 14:37:05 -0800284 r[2] = map[Numberz]*Insanity{
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900285 6: NewInsanity(),
286 }
287 return
Jens Geyerf4598682014-05-08 23:18:44 +0200288}
289
290// Prints 'testMulti()'
291// @param byte arg0 -
292// @param i32 arg1 -
293// @param i64 arg2 -
294// @param map<i16, string> arg3 -
295// @param Numberz arg4 -
296// @param UserId arg5 -
297// @return Xtruct - returns an Xtruct with StringThing = "Hello2, ByteThing = arg0, I32Thing = arg1
298// and I64Thing = arg2
299//
300// Parameters:
301// - Arg0
302// - Arg1
303// - Arg2
304// - Arg3
305// - Arg4
306// - Arg5
taozlec0d384a2017-07-17 18:40:42 +0200307func (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 +0200308 fmt.Printf("testMulti()\n")
309 r = NewXtruct()
310
311 r.StringThing = "Hello2"
312 r.ByteThing = arg0
313 r.I32Thing = arg1
314 r.I64Thing = arg2
315 return
316}
317
318// Print 'testException(%s)' with arg as '%s'
319// @param string arg - a string indication what type of exception to throw
320// if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
321// elsen if arg == "TException" throw TException
322// else do not throw anything
323//
324// Parameters:
325// - Arg
taozlec0d384a2017-07-17 18:40:42 +0200326func (p *printingHandler) TestException(ctx context.Context, arg string) (err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200327 fmt.Printf("testException(%s)\n", arg)
328 switch arg {
329 case "Xception":
330 e := NewXception()
331 e.ErrorCode = 1001
332 e.Message = arg
333 return e
334 case "TException":
335 return errors.New("Just TException")
336 }
337 return
338}
339
340// Print 'testMultiException(%s, %s)' with arg0 as '%s' and arg1 as '%s'
341// @param string arg - a string indication what type of exception to throw
342// if arg0 == "Xception" throw Xception with errorCode = 1001 and message = "This is an Xception"
343// elsen if arg0 == "Xception2" throw Xception2 with errorCode = 2002 and message = "This is an Xception2"
344// else do not throw anything
345// @return Xtruct - an Xtruct with StringThing = arg1
346//
347// Parameters:
348// - Arg0
349// - Arg1
taozlec0d384a2017-07-17 18:40:42 +0200350func (p *printingHandler) TestMultiException(ctx context.Context, arg0 string, arg1 string) (r *Xtruct, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200351 fmt.Printf("testMultiException(%s, %s)\n", arg0, arg1)
352 switch arg0 {
353
354 case "Xception":
355 e := NewXception()
356 e.ErrorCode = 1001
357 e.Message = "This is an Xception"
358 return nil, e
359 case "Xception2":
360 e := NewXception2()
361 e.ErrorCode = 2002
362 e.StructThing = NewXtruct()
363 e.StructThing.StringThing = "This is an Xception2"
364 return nil, e
365 default:
366 r = NewXtruct()
367 r.StringThing = arg1
368 return
369 }
370}
371
372// Print 'testOneway(%d): Sleeping...' with secondsToSleep as '%d'
373// sleep 'secondsToSleep'
374// Print 'testOneway(%d): done sleeping!' with secondsToSleep as '%d'
375// @param i32 secondsToSleep - the number of seconds to sleep
376//
377// Parameters:
378// - SecondsToSleep
taozlec0d384a2017-07-17 18:40:42 +0200379func (p *printingHandler) TestOneway(ctx context.Context, secondsToSleep int32) (err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200380 fmt.Printf("testOneway(%d): Sleeping...\n", secondsToSleep)
381 time.Sleep(time.Second * time.Duration(secondsToSleep))
382 fmt.Printf("testOneway(%d): done sleeping!\n", secondsToSleep)
383 return
384}