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