blob: c0a2862678880f1537f259d3cf5b569f2a601873 [file] [log] [blame]
taozlec0d384a2017-07-17 18:40:42 +02001// +build !go1.7
2
Jens Geyerf4598682014-05-08 23:18:44 +02003/*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22package common
23
24import (
25 "errors"
26 "fmt"
Jens Geyer8bcfdd92014-12-14 03:14:26 +010027 "encoding/hex"
Jens Geyerf4598682014-05-08 23:18:44 +020028 . "gen/thrifttest"
29 "time"
taozlec0d384a2017-07-17 18:40:42 +020030
31 "golang.org/x/net/context"
Jens Geyerf4598682014-05-08 23:18:44 +020032)
33
34var PrintingHandler = &printingHandler{}
35
36type printingHandler struct{}
37
38// Prints "testVoid()" and returns nothing.
taozlec0d384a2017-07-17 18:40:42 +020039func (p *printingHandler) TestVoid(ctx context.Context) (err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020040 fmt.Println("testVoid()")
41 return nil
42}
43
44// Prints 'testString("%s")' with thing as '%s'
45// @param string thing - the string to print
46// @return string - returns the string 'thing'
47//
48// Parameters:
49// - Thing
taozlec0d384a2017-07-17 18:40:42 +020050func (p *printingHandler) TestString(ctx context.Context, thing string) (r string, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020051 fmt.Printf("testString(\"%s\")\n", thing)
52 return thing, nil
53}
54
Jens Geyerfa0796d2015-10-16 21:33:39 +020055// Prints 'testBool("%t")' with thing as 'true' or 'false'
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090056// @param bool thing - the bool to print
57// @return bool - returns the bool 'thing'
58//
59// Parameters:
60// - Thing
taozlec0d384a2017-07-17 18:40:42 +020061func (p *printingHandler) TestBool(ctx context.Context, thing bool) (r bool, err error) {
Jens Geyerfa0796d2015-10-16 21:33:39 +020062 fmt.Printf("testBool(%t)\n", thing)
Nobuaki Sukegawaa649e742015-09-21 13:53:25 +090063 return thing, nil
64}
65
Jens Geyerf4598682014-05-08 23:18:44 +020066// Prints 'testByte("%d")' with thing as '%d'
67// @param byte thing - the byte to print
68// @return byte - returns the byte 'thing'
69//
70// Parameters:
71// - Thing
taozlec0d384a2017-07-17 18:40:42 +020072func (p *printingHandler) TestByte(ctx context.Context, thing int8) (r int8, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020073 fmt.Printf("testByte(%d)\n", thing)
74 return thing, nil
75}
76
77// Prints 'testI32("%d")' with thing as '%d'
78// @param i32 thing - the i32 to print
79// @return i32 - returns the i32 'thing'
80//
81// Parameters:
82// - Thing
taozlec0d384a2017-07-17 18:40:42 +020083func (p *printingHandler) TestI32(ctx context.Context, thing int32) (r int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020084 fmt.Printf("testI32(%d)\n", thing)
85 return thing, nil
86}
87
88// Prints 'testI64("%d")' with thing as '%d'
89// @param i64 thing - the i64 to print
90// @return i64 - returns the i64 'thing'
91//
92// Parameters:
93// - Thing
taozlec0d384a2017-07-17 18:40:42 +020094func (p *printingHandler) TestI64(ctx context.Context, thing int64) (r int64, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +020095 fmt.Printf("testI64(%d)\n", thing)
96 return thing, nil
97}
98
99// Prints 'testDouble("%f")' with thing as '%f'
100// @param double thing - the double to print
101// @return double - returns the double 'thing'
102//
103// Parameters:
104// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200105func (p *printingHandler) TestDouble(ctx context.Context, thing float64) (r float64, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200106 fmt.Printf("testDouble(%f)\n", thing)
107 return thing, nil
108}
109
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100110// Prints 'testBinary("%s")' where '%s' is a hex-formatted string of thing's data
111// @param []byte thing - the binary to print
112// @return []byte - returns the binary 'thing'
113//
114// Parameters:
115// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200116func (p *printingHandler) TestBinary(ctx context.Context, thing []byte) (r []byte, err error) {
Jens Geyer8bcfdd92014-12-14 03:14:26 +0100117 fmt.Printf("testBinary(%s)\n", hex.EncodeToString(thing))
118 return thing, nil
119}
120
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100121// Prints 'testStruct("{%s}")' where thing has been formatted into a string of comma separated values
Jens Geyerf4598682014-05-08 23:18:44 +0200122// @param Xtruct thing - the Xtruct to print
123// @return Xtruct - returns the Xtruct 'thing'
124//
125// Parameters:
126// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200127func (p *printingHandler) TestStruct(ctx context.Context, thing *Xtruct) (r *Xtruct, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200128 fmt.Printf("testStruct({\"%s\", %d, %d, %d})\n", thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing)
129 return thing, err
130}
131
132// Prints 'testNest("{%s}")' where thing has been formatted into a string of the nested struct
133// @param Xtruct2 thing - the Xtruct2 to print
134// @return Xtruct2 - returns the Xtruct2 'thing'
135//
136// Parameters:
137// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200138func (p *printingHandler) TestNest(ctx context.Context, nest *Xtruct2) (r *Xtruct2, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200139 thing := nest.StructThing
140 fmt.Printf("testNest({%d, {\"%s\", %d, %d, %d}, %d})\n", nest.ByteThing, thing.StringThing, thing.ByteThing, thing.I32Thing, thing.I64Thing, nest.I32Thing)
141 return nest, nil
142}
143
144// Prints 'testMap("{%s")' where thing has been formatted into a string of 'key => value' pairs
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100145// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200146// @param map<i32,i32> thing - the map<i32,i32> to print
147// @return map<i32,i32> - returns the map<i32,i32> 'thing'
148//
149// Parameters:
150// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200151func (p *printingHandler) TestMap(ctx context.Context, thing map[int32]int32) (r map[int32]int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200152 fmt.Printf("testMap({")
153 first := true
154 for k, v := range thing {
155 if first {
156 first = false
157 } else {
158 fmt.Printf(", ")
159 }
160 fmt.Printf("%d => %d", k, v)
161 }
162 fmt.Printf("})\n")
163 return thing, nil
164}
165
166// Prints 'testStringMap("{%s}")' where thing has been formatted into a string of 'key => value' pairs
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100167// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200168// @param map<string,string> thing - the map<string,string> to print
169// @return map<string,string> - returns the map<string,string> 'thing'
170//
171// Parameters:
172// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200173func (p *printingHandler) TestStringMap(ctx context.Context, thing map[string]string) (r map[string]string, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200174 fmt.Printf("testStringMap({")
175 first := true
176 for k, v := range thing {
177 if first {
178 first = false
179 } else {
180 fmt.Printf(", ")
181 }
182 fmt.Printf("%s => %s", k, v)
183 }
184 fmt.Printf("})\n")
185 return thing, nil
186}
187
188// Prints 'testSet("{%s}")' where thing has been formatted into a string of values
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100189// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200190// @param set<i32> thing - the set<i32> to print
191// @return set<i32> - returns the set<i32> 'thing'
192//
193// Parameters:
194// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200195func (p *printingHandler) TestSet(ctx context.Context, thing []int32) (r []int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200196 fmt.Printf("testSet({")
197 first := true
198 for k, _ := range thing {
199 if first {
200 first = false
201 } else {
202 fmt.Printf(", ")
203 }
204 fmt.Printf("%d", k)
205 }
206 fmt.Printf("})\n")
207 return thing, nil
208}
209
210// Prints 'testList("{%s}")' where thing has been formatted into a string of values
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100211// separated by commas and new lines
Jens Geyerf4598682014-05-08 23:18:44 +0200212// @param list<i32> thing - the list<i32> to print
213// @return list<i32> - returns the list<i32> 'thing'
214//
215// Parameters:
216// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200217func (p *printingHandler) TestList(ctx context.Context, thing []int32) (r []int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200218 fmt.Printf("testList({")
219 for i, v := range thing {
220 if i != 0 {
221 fmt.Printf(", ")
222 }
223 fmt.Printf("%d", v)
224 }
225 fmt.Printf("})\n")
226 return thing, nil
227}
228
229// Prints 'testEnum("%d")' where thing has been formatted into it's numeric value
230// @param Numberz thing - the Numberz to print
231// @return Numberz - returns the Numberz 'thing'
232//
233// Parameters:
234// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200235func (p *printingHandler) TestEnum(ctx context.Context, thing Numberz) (r Numberz, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200236 fmt.Printf("testEnum(%d)\n", thing)
237 return thing, nil
238}
239
240// Prints 'testTypedef("%d")' with thing as '%d'
241// @param UserId thing - the UserId to print
242// @return UserId - returns the UserId 'thing'
243//
244// Parameters:
245// - Thing
taozlec0d384a2017-07-17 18:40:42 +0200246func (p *printingHandler) TestTypedef(ctx context.Context, thing UserId) (r UserId, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200247 fmt.Printf("testTypedef(%d)\n", thing)
248 return thing, nil
249}
250
251// Prints 'testMapMap("%d")' with hello as '%d'
252// @param i32 hello - the i32 to print
253// @return map<i32,map<i32,i32>> - returns a dictionary with these values:
254// {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 => 2, 3 => 3, 4 => 4, }, }
255//
256// Parameters:
257// - Hello
taozlec0d384a2017-07-17 18:40:42 +0200258func (p *printingHandler) TestMapMap(ctx context.Context, hello int32) (r map[int32]map[int32]int32, err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200259 fmt.Printf("testMapMap(%d)\n", hello)
260
261 r = map[int32]map[int32]int32{
262 -4: map[int32]int32{-4: -4, -3: -3, -2: -2, -1: -1},
263 4: map[int32]int32{4: 4, 3: 3, 2: 2, 1: 1},
264 }
265 return
266}
267
268// So you think you've got this all worked, out eh?
269//
270// Creates a the returned map with these values and prints it out:
271// { 1 => { 2 => argument,
272// 3 => argument,
273// },
274// 2 => { 6 => <empty Insanity struct>, },
275// }
276// @return map<UserId, map<Numberz,Insanity>> - a map with the above values
277//
278// Parameters:
279// - Argument
taozlec0d384a2017-07-17 18:40:42 +0200280func (p *printingHandler) TestInsanity(ctx context.Context, argument *Insanity) (r map[UserId]map[Numberz]*Insanity, err error) {
Nobuaki Sukegawa2fab3de2015-08-16 15:42:58 +0900281 fmt.Printf("testInsanity()\n")
282 r = make(map[UserId]map[Numberz]*Insanity)
283 r[1] = map[Numberz]*Insanity {
284 2: argument,
285 3: argument,
286 }
287 r[2] = map[Numberz]*Insanity {
288 6: NewInsanity(),
289 }
290 return
Jens Geyerf4598682014-05-08 23:18:44 +0200291}
292
293// Prints 'testMulti()'
294// @param byte arg0 -
295// @param i32 arg1 -
296// @param i64 arg2 -
297// @param map<i16, string> arg3 -
298// @param Numberz arg4 -
299// @param UserId arg5 -
300// @return Xtruct - returns an Xtruct with StringThing = "Hello2, ByteThing = arg0, I32Thing = arg1
301// and I64Thing = arg2
302//
303// Parameters:
304// - Arg0
305// - Arg1
306// - Arg2
307// - Arg3
308// - Arg4
309// - Arg5
taozlec0d384a2017-07-17 18:40:42 +0200310func (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 +0200311 fmt.Printf("testMulti()\n")
312 r = NewXtruct()
313
314 r.StringThing = "Hello2"
315 r.ByteThing = arg0
316 r.I32Thing = arg1
317 r.I64Thing = arg2
318 return
319}
320
321// Print 'testException(%s)' with arg as '%s'
322// @param string arg - a string indication what type of exception to throw
323// if arg == "Xception" throw Xception with errorCode = 1001 and message = arg
324// elsen if arg == "TException" throw TException
325// else do not throw anything
326//
327// Parameters:
328// - Arg
taozlec0d384a2017-07-17 18:40:42 +0200329func (p *printingHandler) TestException(ctx context.Context, arg string) (err error) {
Jens Geyerf4598682014-05-08 23:18:44 +0200330 fmt.Printf("testException(%s)\n", arg)
331 switch arg {
332 case "Xception":
333 e := NewXception()
334 e.ErrorCode = 1001
335 e.Message = arg
336 return e
337 case "TException":
338 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}