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 ( |
John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame^] | 23 | "context" |
| 24 | "encoding/hex" |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 25 | "errors" |
| 26 | "fmt" |
| 27 | . "gen/thrifttest" |
| 28 | "time" |
| 29 | ) |
| 30 | |
| 31 | var PrintingHandler = &printingHandler{} |
| 32 | |
| 33 | type printingHandler struct{} |
| 34 | |
| 35 | // Prints "testVoid()" and returns nothing. |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 36 | func (p *printingHandler) TestVoid(ctx context.Context) (err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 37 | 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 47 | func (p *printingHandler) TestString(ctx context.Context, thing string) (r string, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 48 | fmt.Printf("testString(\"%s\")\n", thing) |
| 49 | return thing, nil |
| 50 | } |
| 51 | |
Jens Geyer | fa0796d | 2015-10-16 21:33:39 +0200 | [diff] [blame] | 52 | // Prints 'testBool("%t")' with thing as 'true' or 'false' |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 53 | // @param bool thing - the bool to print |
| 54 | // @return bool - returns the bool 'thing' |
| 55 | // |
| 56 | // Parameters: |
| 57 | // - Thing |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 58 | func (p *printingHandler) TestBool(ctx context.Context, thing bool) (r bool, err error) { |
Jens Geyer | fa0796d | 2015-10-16 21:33:39 +0200 | [diff] [blame] | 59 | fmt.Printf("testBool(%t)\n", thing) |
Nobuaki Sukegawa | a649e74 | 2015-09-21 13:53:25 +0900 | [diff] [blame] | 60 | return thing, nil |
| 61 | } |
| 62 | |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 63 | // 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 69 | func (p *printingHandler) TestByte(ctx context.Context, thing int8) (r int8, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 70 | 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 80 | func (p *printingHandler) TestI32(ctx context.Context, thing int32) (r int32, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 81 | 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 91 | func (p *printingHandler) TestI64(ctx context.Context, thing int64) (r int64, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 92 | 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 102 | func (p *printingHandler) TestDouble(ctx context.Context, thing float64) (r float64, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 103 | fmt.Printf("testDouble(%f)\n", thing) |
| 104 | return thing, nil |
| 105 | } |
| 106 | |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 107 | // 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 113 | func (p *printingHandler) TestBinary(ctx context.Context, thing []byte) (r []byte, err error) { |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 114 | fmt.Printf("testBinary(%s)\n", hex.EncodeToString(thing)) |
| 115 | return thing, nil |
| 116 | } |
| 117 | |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 118 | // 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] | 119 | // @param Xtruct thing - the Xtruct to print |
| 120 | // @return Xtruct - returns the Xtruct 'thing' |
| 121 | // |
| 122 | // Parameters: |
| 123 | // - Thing |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 124 | func (p *printingHandler) TestStruct(ctx context.Context, thing *Xtruct) (r *Xtruct, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 125 | 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 135 | func (p *printingHandler) TestNest(ctx context.Context, nest *Xtruct2) (r *Xtruct2, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 136 | 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 Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 142 | // separated by commas and new lines |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 143 | // @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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 148 | func (p *printingHandler) TestMap(ctx context.Context, thing map[int32]int32) (r map[int32]int32, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 149 | 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 Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 164 | // separated by commas and new lines |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 165 | // @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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 170 | func (p *printingHandler) TestStringMap(ctx context.Context, thing map[string]string) (r map[string]string, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 171 | 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 Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 186 | // separated by commas and new lines |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 187 | // @param set<i32> thing - the set<i32> to print |
| 188 | // @return set<i32> - returns the set<i32> 'thing' |
| 189 | // |
| 190 | // Parameters: |
| 191 | // - Thing |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 192 | func (p *printingHandler) TestSet(ctx context.Context, thing []int32) (r []int32, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 193 | 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 Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 208 | // separated by commas and new lines |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 209 | // @param list<i32> thing - the list<i32> to print |
| 210 | // @return list<i32> - returns the list<i32> 'thing' |
| 211 | // |
| 212 | // Parameters: |
| 213 | // - Thing |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 214 | func (p *printingHandler) TestList(ctx context.Context, thing []int32) (r []int32, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 215 | 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 232 | func (p *printingHandler) TestEnum(ctx context.Context, thing Numberz) (r Numberz, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 233 | 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 243 | func (p *printingHandler) TestTypedef(ctx context.Context, thing UserId) (r UserId, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 244 | 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 255 | func (p *printingHandler) TestMapMap(ctx context.Context, hello int32) (r map[int32]map[int32]int32, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 256 | 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 277 | func (p *printingHandler) TestInsanity(ctx context.Context, argument *Insanity) (r map[UserId]map[Numberz]*Insanity, err error) { |
Nobuaki Sukegawa | 2fab3de | 2015-08-16 15:42:58 +0900 | [diff] [blame] | 278 | fmt.Printf("testInsanity()\n") |
| 279 | r = make(map[UserId]map[Numberz]*Insanity) |
John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame^] | 280 | r[1] = map[Numberz]*Insanity{ |
Nobuaki Sukegawa | 2fab3de | 2015-08-16 15:42:58 +0900 | [diff] [blame] | 281 | 2: argument, |
| 282 | 3: argument, |
| 283 | } |
John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame^] | 284 | r[2] = map[Numberz]*Insanity{ |
Nobuaki Sukegawa | 2fab3de | 2015-08-16 15:42:58 +0900 | [diff] [blame] | 285 | 6: NewInsanity(), |
| 286 | } |
| 287 | return |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 288 | } |
| 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 307 | func (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 Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 308 | 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 326 | func (p *printingHandler) TestException(ctx context.Context, arg string) (err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 327 | 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 350 | func (p *printingHandler) TestMultiException(ctx context.Context, arg0 string, arg1 string) (r *Xtruct, err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 351 | 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 |
taozle | c0d384a | 2017-07-17 18:40:42 +0200 | [diff] [blame] | 379 | func (p *printingHandler) TestOneway(ctx context.Context, secondsToSleep int32) (err error) { |
Jens Geyer | f459868 | 2014-05-08 23:18:44 +0200 | [diff] [blame] | 380 | 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 | } |