Kino Roy | a9da9eb | 2022-10-07 23:13:01 -0700 | [diff] [blame] | 1 | // Licensed to the Apache Software Foundation (ASF) under one |
| 2 | // or more contributor license agreements. See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership. The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
| 18 | import Foundation |
| 19 | import Thrift |
| 20 | import Common |
| 21 | |
| 22 | class ThriftTestImpl : ThriftTest { |
| 23 | |
| 24 | /// Prints "testVoid()" and returns nothing. |
| 25 | /// |
| 26 | /// - Throws: |
| 27 | func testVoid() throws { |
| 28 | print("testVoid()") |
| 29 | } |
| 30 | |
| 31 | /// Prints 'testString("%s")' with thing as '%s' |
| 32 | /// @param string thing - the string to print |
| 33 | /// @return string - returns the string 'thing' |
| 34 | /// |
| 35 | /// - Parameters: |
| 36 | /// - thing: |
| 37 | /// - Returns: String |
| 38 | /// - Throws: |
| 39 | func testString(thing: String) throws -> String { |
| 40 | print("testString(\"\(thing)\")") |
| 41 | return thing |
| 42 | } |
| 43 | |
| 44 | /// Prints 'testBool("%s")' where '%s' with thing as 'true' or 'false' |
| 45 | /// @param bool thing - the bool data to print |
| 46 | /// @return bool - returns the bool 'thing' |
| 47 | /// |
| 48 | /// - Parameters: |
| 49 | /// - thing: |
| 50 | /// - Returns: Bool |
| 51 | /// - Throws: |
| 52 | func testBool(thing: Bool) throws -> Bool { |
| 53 | print("testBool\"(\(thing ? "true" : "false")\")") |
| 54 | return thing |
| 55 | } |
| 56 | |
| 57 | /// Prints 'testByte("%d")' with thing as '%d' |
| 58 | /// The types i8 and byte are synonyms, use of i8 is encouraged, byte still exists for the sake of compatibility. |
| 59 | /// @param byte thing - the i8/byte to print |
| 60 | /// @return i8 - returns the i8/byte 'thing' |
| 61 | /// |
| 62 | /// - Parameters: |
| 63 | /// - thing: |
| 64 | /// - Returns: Int8 |
| 65 | /// - Throws: |
| 66 | func testByte(thing: Int8) throws -> Int8 { |
| 67 | print("testByte(\"\(thing)\")") |
| 68 | return thing |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /// Prints 'testI32("%d")' with thing as '%d' |
| 73 | /// @param i32 thing - the i32 to print |
| 74 | /// @return i32 - returns the i32 'thing' |
| 75 | /// |
| 76 | /// - Parameters: |
| 77 | /// - thing: |
| 78 | /// - Returns: Int32 |
| 79 | /// - Throws: |
| 80 | func testI32(thing: Int32) throws -> Int32 { |
| 81 | print("testI32(\"\(thing)\")") |
| 82 | return thing |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /// Prints 'testI64("%d")' with thing as '%d' |
| 87 | /// @param i64 thing - the i64 to print |
| 88 | /// @return i64 - returns the i64 'thing' |
| 89 | /// |
| 90 | /// - Parameters: |
| 91 | /// - thing: |
| 92 | /// - Returns: Int64 |
| 93 | /// - Throws: |
| 94 | func testI64(thing: Int64) throws -> Int64 { |
| 95 | print("testI64(\"\(thing)\")") |
| 96 | return thing |
| 97 | } |
| 98 | |
| 99 | |
| 100 | /// Prints 'testDouble("%f")' with thing as '%f' |
| 101 | /// @param double thing - the double to print |
| 102 | /// @return double - returns the double 'thing' |
| 103 | /// |
| 104 | /// - Parameters: |
| 105 | /// - thing: |
| 106 | /// - Returns: Double |
| 107 | /// - Throws: |
| 108 | func testDouble(thing: Double) throws -> Double { |
| 109 | print("testDouble(\"\(thing)\")") |
| 110 | return thing |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /// Prints 'testBinary("%s")' where '%s' is a hex-formatted string of thing's data |
| 115 | /// @param binary thing - the binary data to print |
| 116 | /// @return binary - returns the binary 'thing' |
| 117 | /// |
| 118 | /// - Parameters: |
| 119 | /// - thing: |
| 120 | /// - Returns: Data |
| 121 | /// - Throws: |
| 122 | func testBinary(thing: Data) throws -> Data { |
| 123 | print("testBinary(\"\(thing)\")") |
| 124 | return thing |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /// Prints 'testStruct("{%s}")' where thing has been formatted into a string of comma separated values |
| 129 | /// @param Xtruct thing - the Xtruct to print |
| 130 | /// @return Xtruct - returns the Xtruct 'thing' |
| 131 | /// |
| 132 | /// - Parameters: |
| 133 | /// - thing: |
| 134 | /// - Returns: Xtruct |
| 135 | /// - Throws: |
| 136 | func testStruct(thing: Xtruct) throws -> Xtruct { |
| 137 | print("testStruct({\([thing.string_thing, "\(thing.byte_thing)", "\(thing.i32_thing)", "\(thing.i64_thing)"].joined(separator: ", "))})") |
| 138 | return thing |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /// Prints 'testNest("{%s}")' where thing has been formatted into a string of the nested struct |
| 143 | /// @param Xtruct2 thing - the Xtruct2 to print |
| 144 | /// @return Xtruct2 - returns the Xtruct2 'thing' |
| 145 | /// |
| 146 | /// - Parameters: |
| 147 | /// - thing: |
| 148 | /// - Returns: Xtruct2 |
| 149 | /// - Throws: |
| 150 | func testNest(thing: Xtruct2) throws -> Xtruct2 { |
| 151 | print("testNest(\(thing)") |
| 152 | return thing |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /// Prints 'testMap("{%s")' where thing has been formatted into a string of 'key => value' pairs |
| 157 | /// separated by commas and new lines |
| 158 | /// @param map<i32,i32> thing - the map<i32,i32> to print |
| 159 | /// @return map<i32,i32> - returns the map<i32,i32> 'thing' |
| 160 | /// |
| 161 | /// - Parameters: |
| 162 | /// - thing: |
| 163 | /// - Returns: TMap<Int32, Int32> |
| 164 | /// - Throws: |
| 165 | func testMap(thing: TMap<Int32, Int32>) throws -> TMap<Int32, Int32> { |
| 166 | print("testMap(\(thing)") |
| 167 | return thing |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /// Prints 'testStringMap("{%s}")' where thing has been formatted into a string of 'key => value' pairs |
| 172 | /// separated by commas and new lines |
| 173 | /// @param map<string,string> thing - the map<string,string> to print |
| 174 | /// @return map<string,string> - returns the map<string,string> 'thing' |
| 175 | /// |
| 176 | /// - Parameters: |
| 177 | /// - thing: |
| 178 | /// - Returns: TMap<String, String> |
| 179 | /// - Throws: |
| 180 | func testStringMap(thing: TMap<String, String>) throws -> TMap<String, String> { |
| 181 | print("testStringMap(\(thing)") |
| 182 | return thing |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /// Prints 'testSet("{%s}")' where thing has been formatted into a string of values |
| 187 | /// separated by commas and new lines |
| 188 | /// @param set<i32> thing - the set<i32> to print |
| 189 | /// @return set<i32> - returns the set<i32> 'thing' |
| 190 | /// |
| 191 | /// - Parameters: |
| 192 | /// - thing: |
| 193 | /// - Returns: TSet<Int32> |
| 194 | /// - Throws: |
| 195 | func testSet(thing: TSet<Int32>) throws -> TSet<Int32> { |
| 196 | print("testSet\(thing)") |
| 197 | return thing |
| 198 | } |
| 199 | |
| 200 | |
| 201 | /// Prints 'testList("{%s}")' where thing has been formatted into a string of values |
| 202 | /// separated by commas and new lines |
| 203 | /// @param list<i32> thing - the list<i32> to print |
| 204 | /// @return list<i32> - returns the list<i32> 'thing' |
| 205 | /// |
| 206 | /// - Parameters: |
| 207 | /// - thing: |
| 208 | /// - Returns: TList<Int32> |
| 209 | /// - Throws: |
| 210 | func testList(thing: TList<Int32>) throws -> TList<Int32> { |
| 211 | print("testList\(thing)") |
| 212 | return thing |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /// Prints 'testEnum("%d")' where thing has been formatted into its numeric value |
| 217 | /// @param Numberz thing - the Numberz to print |
| 218 | /// @return Numberz - returns the Numberz 'thing' |
| 219 | /// |
| 220 | /// - Parameters: |
| 221 | /// - thing: |
| 222 | /// - Returns: Numberz |
| 223 | /// - Throws: |
| 224 | func testEnum(thing: Numberz) throws -> Numberz { |
| 225 | print("testEnum\(thing.rawValue)") |
| 226 | return thing |
| 227 | } |
| 228 | |
| 229 | |
| 230 | /// Prints 'testTypedef("%d")' with thing as '%d' |
| 231 | /// @param UserId thing - the UserId to print |
| 232 | /// @return UserId - returns the UserId 'thing' |
| 233 | /// |
| 234 | /// - Parameters: |
| 235 | /// - thing: |
| 236 | /// - Returns: UserId |
| 237 | /// - Throws: |
| 238 | func testTypedef(thing: UserId) throws -> UserId { |
| 239 | print("testTypedef(\(thing)") |
| 240 | return thing |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /// Prints 'testMapMap("%d")' with hello as '%d' |
| 245 | /// @param i32 hello - the i32 to print |
| 246 | /// @return map<i32,map<i32,i32>> - returns a dictionary with these values: |
| 247 | /// {-4 => {-4 => -4, -3 => -3, -2 => -2, -1 => -1, }, 4 => {1 => 1, 2 => 2, 3 => 3, 4 => 4, }, } |
| 248 | /// |
| 249 | /// - Parameters: |
| 250 | /// - hello: |
| 251 | /// - Returns: TMap<Int32, TMap<Int32, Int32>> |
| 252 | /// - Throws: |
| 253 | func testMapMap(hello: Int32) throws -> TMap<Int32, TMap<Int32, Int32>> { |
| 254 | print("testMapMap(\(hello)") |
| 255 | return TMap<Int32, TMap<Int32, Int32>>([ |
| 256 | -4: [-4: -4, -3: -3, -2: -2, -1: -1], |
| 257 | 4: [4: 4, 3: 3, 2: 2, 1: 1] |
| 258 | ]) |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /// So you think you've got this all worked out, eh? |
| 263 | /// Creates a map with these values and prints it out: |
| 264 | /// { 1 => { 2 => argument, |
| 265 | /// 3 => argument, |
| 266 | /// }, |
| 267 | /// 2 => { 6 => <empty Insanity struct>, }, |
| 268 | /// } |
| 269 | /// @return map<UserId, map<Numberz,Insanity>> - a map with the above values |
| 270 | /// |
| 271 | /// - Parameters: |
| 272 | /// - argument: |
| 273 | /// - Returns: TMap<UserId, TMap<Numberz, Insanity>> |
| 274 | /// - Throws: |
| 275 | func testInsanity(argument: Insanity) throws -> TMap<UserId, TMap<Numberz, Insanity>> { |
| 276 | return TMap<UserId, TMap<Numberz, Insanity>>([ |
| 277 | 1: [ |
| 278 | .two: argument, |
| 279 | .three: argument |
| 280 | ], |
| 281 | 2: [ |
| 282 | .six: Insanity(userMap: [:], xtructs: []) |
| 283 | ] |
| 284 | ]) |
| 285 | } |
| 286 | |
| 287 | |
| 288 | /// Prints 'testMulti()' |
| 289 | /// @param i8 arg0 - |
| 290 | /// @param i32 arg1 - |
| 291 | /// @param i64 arg2 - |
| 292 | /// @param map<i16, string> arg3 - |
| 293 | /// @param Numberz arg4 - |
| 294 | /// @param UserId arg5 - |
| 295 | /// @return Xtruct - returns an Xtruct with string_thing = "Hello2, byte_thing = arg0, i32_thing = arg1 |
| 296 | /// and i64_thing = arg2 |
| 297 | /// |
| 298 | /// - Parameters: |
| 299 | /// - arg0: |
| 300 | /// - arg1: |
| 301 | /// - arg2: |
| 302 | /// - arg3: |
| 303 | /// - arg4: |
| 304 | /// - arg5: |
| 305 | /// - Returns: Xtruct |
| 306 | /// - Throws: |
| 307 | func testMulti(arg0: Int8, arg1: Int32, arg2: Int64, arg3: TMap<Int16, String>, arg4: Numberz, arg5: UserId) throws -> Xtruct { |
| 308 | print("testMulti()") |
| 309 | return Xtruct(string_thing: "Hello2", byte_thing: arg0, i32_thing: arg1, i64_thing: arg2) |
| 310 | } |
| 311 | |
| 312 | |
| 313 | /// Print 'testException(%s)' with arg as '%s' |
| 314 | /// @param string arg - a string indication what type of exception to throw |
| 315 | /// if arg == "Xception" throw Xception with errorCode = 1001 and message = arg |
| 316 | /// else if arg == "TException" throw TException |
| 317 | /// else do not throw anything |
| 318 | /// |
| 319 | /// - Parameters: |
| 320 | /// - arg: |
| 321 | /// - Throws: Xception |
| 322 | func testException(arg: String) throws { |
| 323 | print("testException(\(arg)") |
| 324 | if arg == "Xception" { |
| 325 | throw Xception(errorCode: 1001, message: arg) |
| 326 | } else if arg == "TException" { |
| 327 | throw TApplicationError() // is type TError (TException Swift equiv) |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | |
| 332 | /// Print 'testMultiException(%s, %s)' with arg0 as '%s' and arg1 as '%s' |
| 333 | /// @param string arg - a string indicating what type of exception to throw |
| 334 | /// if arg0 == "Xception" throw Xception with errorCode = 1001 and message = "This is an Xception" |
| 335 | /// else if arg0 == "Xception2" throw Xception2 with errorCode = 2002 and struct_thing.string_thing = "This is an Xception2" |
| 336 | /// else do not throw anything |
| 337 | /// @return Xtruct - an Xtruct with string_thing = arg1 |
| 338 | /// |
| 339 | /// - Parameters: |
| 340 | /// - arg0: |
| 341 | /// - arg1: |
| 342 | /// - Returns: Xtruct |
| 343 | /// - Throws: Xception, Xception2 |
| 344 | func testMultiException(arg0: String, arg1: String) throws -> Xtruct { |
| 345 | print("testMultiException(\(arg0), \(arg1)") |
| 346 | if arg0 == "Xception" { |
| 347 | throw Xception(errorCode: 1001, message: "This is an Xception") |
| 348 | } else if arg0 == "Xception2" { |
| 349 | throw Xception2(errorCode: 2002, struct_thing: Xtruct(string_thing: "This is an Xception2", byte_thing: 0, i32_thing: 0, i64_thing: 0)) |
| 350 | } |
| 351 | return Xtruct(string_thing: arg1, byte_thing: 0, i32_thing: 0, i64_thing: 0) |
| 352 | } |
| 353 | |
| 354 | |
| 355 | /// Print 'testOneway(%d): Sleeping...' with secondsToSleep as '%d' |
| 356 | /// sleep 'secondsToSleep' |
| 357 | /// Print 'testOneway(%d): done sleeping!' with secondsToSleep as '%d' |
| 358 | /// @param i32 secondsToSleep - the number of seconds to sleep |
| 359 | /// |
| 360 | /// - Parameters: |
| 361 | /// - secondsToSleep: |
| 362 | /// - Throws: |
| 363 | func testOneway(secondsToSleep: Int32) throws { |
| 364 | print("testOneway(\(secondsToSleep): Sleeping...") |
| 365 | Thread.sleep(forTimeInterval: TimeInterval(secondsToSleep)) |
| 366 | } |
| 367 | |
| 368 | func testUuid(thing: UUID) throws -> UUID { |
| 369 | print("testUuid(\(thing))") |
| 370 | return thing |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | class SecondServiceImpl : SecondService { |
| 375 | |
| 376 | /// Prints 'testString("%s")' with thing as '%s' |
| 377 | /// @param string thing - the string to print |
| 378 | /// @return string - returns the string 'thing' |
| 379 | /// |
| 380 | /// - Parameters: |
| 381 | /// - thing: |
| 382 | /// - Returns: String |
| 383 | /// - Throws: |
| 384 | func secondtestString(thing: String) throws -> String { |
| 385 | print("testString(\"\(thing)\")") |
| 386 | return thing |
| 387 | } |
| 388 | } |
| 389 | |