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