blob: bed7086d8cab0eac75dbe78e7d8634796a1bf836 [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 (
23 "errors"
24 "fmt"
Jens Geyer8bcfdd92014-12-14 03:14:26 +010025 "encoding/hex"
Jens Geyerf4598682014-05-08 23:18:44 +020026 . "gen/thrifttest"
27 "time"
28)
29
30var PrintingHandler = &printingHandler{}
31
32type printingHandler struct{}
33
34// Prints "testVoid()" and returns nothing.
35func (p *printingHandler) TestVoid() (err error) {
36 fmt.Println("testVoid()")
37 return nil
38}
39
40// Prints 'testString("%s")' with thing as '%s'
41// @param string thing - the string to print
42// @return string - returns the string 'thing'
43//
44// Parameters:
45// - Thing
46func (p *printingHandler) TestString(thing string) (r string, err error) {
47 fmt.Printf("testString(\"%s\")\n", thing)
48 return thing, nil
49}
50
51// Prints 'testByte("%d")' with thing as '%d'
52// @param byte thing - the byte to print
53// @return byte - returns the byte 'thing'
54//
55// Parameters:
56// - Thing
57func (p *printingHandler) TestByte(thing int8) (r int8, err error) {
58 fmt.Printf("testByte(%d)\n", thing)
59 return thing, nil
60}
61
62// Prints 'testI32("%d")' with thing as '%d'
63// @param i32 thing - the i32 to print
64// @return i32 - returns the i32 'thing'
65//
66// Parameters:
67// - Thing
68func (p *printingHandler) TestI32(thing int32) (r int32, err error) {
69 fmt.Printf("testI32(%d)\n", thing)
70 return thing, nil
71}
72
73// Prints 'testI64("%d")' with thing as '%d'
74// @param i64 thing - the i64 to print
75// @return i64 - returns the i64 'thing'
76//
77// Parameters:
78// - Thing
79func (p *printingHandler) TestI64(thing int64) (r int64, err error) {
80 fmt.Printf("testI64(%d)\n", thing)
81 return thing, nil
82}
83
84// Prints 'testDouble("%f")' with thing as '%f'
85// @param double thing - the double to print
86// @return double - returns the double 'thing'
87//
88// Parameters:
89// - Thing
90func (p *printingHandler) TestDouble(thing float64) (r float64, err error) {
91 fmt.Printf("testDouble(%f)\n", thing)
92 return thing, nil
93}
94
Jens Geyer8bcfdd92014-12-14 03:14:26 +010095// Prints 'testBinary("%s")' where '%s' is a hex-formatted string of thing's data
96// @param []byte thing - the binary to print
97// @return []byte - returns the binary 'thing'
98//
99// Parameters:
100// - Thing
101func (p *printingHandler) TestBinary(thing []byte) (r []byte, err error) {
102 fmt.Printf("testBinary(%s)\n", hex.EncodeToString(thing))
103 return thing, nil
104}
105
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100106// Prints 'testStruct("{%s}")' where thing has been formatted into a string of comma separated values
Jens Geyerf4598682014-05-08 23:18:44 +0200107// @param Xtruct thing - the Xtruct to print
108// @return Xtruct - returns the Xtruct 'thing'
109//
110// Parameters:
111// - Thing
112func (p *printingHandler) TestStruct(thing *Xtruct) (r *Xtruct, err error) {
113 fmt.Printf("testStruct({\"%s\", %d, %d, %d})\n", thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing)
114 return thing, err
115}
116
117// Prints 'testNest("{%s}")' where thing has been formatted into a string of the nested struct
118// @param Xtruct2 thing - the Xtruct2 to print
119// @return Xtruct2 - returns the Xtruct2 'thing'
120//
121// Parameters:
122// - Thing
123func (p *printingHandler) TestNest(nest *Xtruct2) (r *Xtruct2, err error) {
124 thing := nest.StructThing
125 fmt.Printf("testNest({%d, {\"%s\", %d, %d, %d}, %d})\n", nest.ByteThing, thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing, nest.I32Thing)
126 return nest, nil
127}
128
129// Prints 'testMap("{%s")' where thing has been formatted into a string of 'key => value' pairs
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100130// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200131// @param map<i32,i32> thing - the map<i32,i32> to print
132// @return map<i32,i32> - returns the map<i32,i32> 'thing'
133//
134// Parameters:
135// - Thing
136func (p *printingHandler) TestMap(thing map[int32]int32) (r map[int32]int32, err error) {
137 fmt.Printf("testMap({")
138 first := true
139 for k, v := range thing {
140 if first {
141 first = false
142 } else {
143 fmt.Printf(", ")
144 }
145 fmt.Printf("%d => %d", k, v)
146 }
147 fmt.Printf("})\n")
148 return thing, nil
149}
150
151// Prints 'testStringMap("{%s}")' where thing has been formatted into a string of 'key => value' pairs
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100152// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200153// @param map<string,string> thing - the map<string,string> to print
154// @return map<string,string> - returns the map<string,string> 'thing'
155//
156// Parameters:
157// - Thing
158func (p *printingHandler) TestStringMap(thing map[string]string) (r map[string]string, err error) {
159 fmt.Printf("testStringMap({")
160 first := true
161 for k, v := range thing {
162 if first {
163 first = false
164 } else {
165 fmt.Printf(", ")
166 }
167 fmt.Printf("%s => %s", k, v)
168 }
169 fmt.Printf("})\n")
170 return thing, nil
171}
172
173// Prints 'testSet("{%s}")' where thing has been formatted into a string of values
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100174// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200175// @param set<i32> thing - the set<i32> to print
176// @return set<i32> - returns the set<i32> 'thing'
177//
178// Parameters:
179// - Thing
180func (p *printingHandler) TestSet(thing map[int32]bool) (r map[int32]bool, err error) {
181 fmt.Printf("testSet({")
182 first := true
183 for k, _ := range thing {
184 if first {
185 first = false
186 } else {
187 fmt.Printf(", ")
188 }
189 fmt.Printf("%d", k)
190 }
191 fmt.Printf("})\n")
192 return thing, nil
193}
194
195// Prints 'testList("{%s}")' where thing has been formatted into a string of values
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100196// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200197// @param list<i32> thing - the list<i32> to print
198// @return list<i32> - returns the list<i32> 'thing'
199//
200// Parameters:
201// - Thing
202func (p *printingHandler) TestList(thing []int32) (r []int32, err error) {
203 fmt.Printf("testList({")
204 for i, v := range thing {
205 if i != 0 {
206 fmt.Printf(", ")
207 }
208 fmt.Printf("%d", v)
209 }
210 fmt.Printf("})\n")
211 return thing, nil
212}
213
214// Prints 'testEnum("%d")' where thing has been formatted into it's numeric value
215// @param Numberz thing - the Numberz to print
216// @return Numberz - returns the Numberz 'thing'
217//
218// Parameters:
219// - Thing
220func (p *printingHandler) TestEnum(thing Numberz) (r Numberz, err error) {
221 fmt.Printf("testEnum(%d)\n", thing)
222 return thing, nil
223}
224
225// Prints 'testTypedef("%d")' with thing as '%d'
226// @param UserId thing - the UserId to print
227// @return UserId - returns the UserId 'thing'
228//
229// Parameters:
230// - Thing
231func (p *printingHandler) TestTypedef(thing UserId) (r UserId, err error) {
232 fmt.Printf("testTypedef(%d)\n", thing)
233 return thing, nil
234}
235
236// Prints 'testMapMap("%d")' with hello as '%d'
237// @param i32 hello - the i32 to print
238// @return map<i32,map<i32,i32>> - returns a dictionary with these values:
239// {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 => 2, 3 => 3, 4 => 4, }, }
240//
241// Parameters:
242// - Hello
243func (p *printingHandler) TestMapMap(hello int32) (r map[int32]map[int32]int32, err error) {
244 fmt.Printf("testMapMap(%d)\n", hello)
245
246 r = map[int32]map[int32]int32{
247 -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1},
248 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1},
249 }
250 return
251}
252
253// So you think you've got this all worked, out eh?
254//
255// Creates a the returned map with these values and prints it out:
256// { 1 => { 2 => argument,
257// 3 => argument,
258// },
259// 2 => { 6 => <empty Insanity struct>, },
260// }
261// @return map<UserId, map<Numberz,Insanity>> - a map with the above values
262//
263// Parameters:
264// - Argument
265func (p *printingHandler) TestInsanity(argument *Insanity) (r map[UserId]map[Numberz]*Insanity, err error) {
266 return nil, errors.New("No Insanity")
267}
268
269// Prints 'testMulti()'
270// @param byte arg0 -
271// @param i32 arg1 -
272// @param i64 arg2 -
273// @param map<i16, string> arg3 -
274// @param Numberz arg4 -
275// @param UserId arg5 -
276// @return Xtruct - returns an Xtruct with StringThing = "Hello2, ByteThing = arg0, I32Thing = arg1
277// and I64Thing = arg2
278//
279// Parameters:
280// - Arg0
281// - Arg1
282// - Arg2
283// - Arg3
284// - Arg4
285// - Arg5
286func (p *printingHandler) TestMulti(arg0 int8, arg1 int32, arg2 int64, arg3 map[int16]string, arg4 Numberz, arg5 UserId) (r *Xtruct, err error) {
287 fmt.Printf("testMulti()\n")
288 r = NewXtruct()
289
290 r.StringThing = "Hello2"
291 r.ByteThing = arg0
292 r.I32Thing = arg1
293 r.I64Thing = arg2
294 return
295}
296
297// Print 'testException(%s)' with arg as '%s'
298// @param string arg - a string indication what type of exception to throw
299// if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
300// elsen if arg == "TException" throw TException
301// else do not throw anything
302//
303// Parameters:
304// - Arg
305func (p *printingHandler) TestException(arg string) (err error) {
306 fmt.Printf("testException(%s)\n", arg)
307 switch arg {
308 case "Xception":
309 e := NewXception()
310 e.ErrorCode = 1001
311 e.Message = arg
312 return e
313 case "TException":
314 return errors.New("Just TException")
315 }
316 return
317}
318
319// Print 'testMultiException(%s, %s)' with arg0 as '%s' and arg1 as '%s'
320// @param string arg - a string indication what type of exception to throw
321// if arg0 == "Xception" throw Xception with errorCode = 1001 and message = "This is an Xception"
322// elsen if arg0 == "Xception2" throw Xception2 with errorCode = 2002 and message = "This is an Xception2"
323// else do not throw anything
324// @return Xtruct - an Xtruct with StringThing = arg1
325//
326// Parameters:
327// - Arg0
328// - Arg1
329func (p *printingHandler) TestMultiException(arg0 string, arg1 string) (r *Xtruct, err error) {
330 fmt.Printf("testMultiException(%s, %s)\n", arg0, arg1)
331 switch arg0 {
332
333 case "Xception":
334 e := NewXception()
335 e.ErrorCode = 1001
336 e.Message = "This is an Xception"
337 return nil, e
338 case "Xception2":
339 e := NewXception2()
340 e.ErrorCode = 2002
341 e.StructThing = NewXtruct()
342 e.StructThing.StringThing = "This is an Xception2"
343 return nil, e
344 default:
345 r = NewXtruct()
346 r.StringThing = arg1
347 return
348 }
349}
350
351// Print 'testOneway(%d): Sleeping...' with secondsToSleep as '%d'
352// sleep 'secondsToSleep'
353// Print 'testOneway(%d): done sleeping!' with secondsToSleep as '%d'
354// @param i32 secondsToSleep - the number of seconds to sleep
355//
356// Parameters:
357// - SecondsToSleep
358func (p *printingHandler) TestOneway(secondsToSleep int32) (err error) {
359 fmt.Printf("testOneway(%d): Sleeping...\n", secondsToSleep)
360 time.Sleep(time.Second * time.Duration(secondsToSleep))
361 fmt.Printf("testOneway(%d): done sleeping!\n", secondsToSleep)
362 return
363}