Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +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 | import Foundation |
| 21 | |
| 22 | |
| 23 | public extension TProtocol { |
| 24 | |
| 25 | public func readMessageBegin() throws -> (String, TMessageType, Int) { |
| 26 | |
| 27 | var name : NSString? |
| 28 | var type : Int32 = -1 |
| 29 | var sequenceID : Int32 = -1 |
| 30 | |
| 31 | try readMessageBeginReturningName(&name, type: &type, sequenceID: &sequenceID) |
| 32 | |
| 33 | return (name as String!, TMessageType(rawValue: type)!, Int(sequenceID)) |
| 34 | } |
| 35 | |
| 36 | public func writeMessageBeginWithName(name: String, type: TMessageType, sequenceID: Int) throws { |
| 37 | try writeMessageBeginWithName(name, type: type.rawValue, sequenceID: Int32(sequenceID)) |
| 38 | } |
| 39 | |
| 40 | public func readStructBegin() throws -> (String?) { |
| 41 | |
| 42 | var name : NSString? = nil |
| 43 | |
| 44 | try readStructBeginReturningName(&name) |
| 45 | |
| 46 | return (name as String?) |
| 47 | } |
| 48 | |
| 49 | public func readFieldBegin() throws -> (String?, TType, Int) { |
| 50 | |
| 51 | var name : NSString? = nil |
| 52 | var type : Int32 = -1 |
| 53 | var fieldID : Int32 = -1 |
| 54 | |
| 55 | try readFieldBeginReturningName(&name, type: &type, fieldID: &fieldID) |
| 56 | |
| 57 | return (name as String?, TType(rawValue: type)!, Int(fieldID)) |
| 58 | } |
| 59 | |
| 60 | public func writeFieldBeginWithName(name: String, type: TType, fieldID: Int) throws { |
| 61 | try writeFieldBeginWithName(name, type: type.rawValue, fieldID: Int32(fieldID)) |
| 62 | } |
| 63 | |
| 64 | public func readMapBegin() throws -> (TType, TType, Int32) { |
| 65 | |
| 66 | var keyType : Int32 = -1 |
| 67 | var valueType : Int32 = -1 |
| 68 | var size : Int32 = 0 |
| 69 | |
| 70 | try readMapBeginReturningKeyType(&keyType, valueType: &valueType, size: &size) |
| 71 | |
| 72 | return (TType(rawValue: keyType)!, TType(rawValue: valueType)!, size) |
| 73 | } |
| 74 | |
| 75 | public func writeMapBeginWithKeyType(keyType: TType, valueType: TType, size: Int) throws { |
| 76 | try writeMapBeginWithKeyType(keyType.rawValue, valueType: valueType.rawValue, size: Int32(size)) |
| 77 | } |
| 78 | |
| 79 | public func readSetBegin() throws -> (TType, Int32) { |
| 80 | |
| 81 | var elementType : Int32 = -1 |
| 82 | var size : Int32 = 0 |
| 83 | |
| 84 | try readSetBeginReturningElementType(&elementType, size: &size) |
| 85 | |
| 86 | return (TType(rawValue: elementType)!, size) |
| 87 | } |
| 88 | |
| 89 | public func writeSetBeginWithElementType(elementType: TType, size: Int) throws { |
| 90 | try writeSetBeginWithElementType(elementType.rawValue, size: Int32(size)) |
| 91 | } |
| 92 | |
| 93 | public func readListBegin() throws -> (TType, Int32) { |
| 94 | |
| 95 | var elementType : Int32 = -1 |
| 96 | var size : Int32 = 0 |
| 97 | |
| 98 | try readListBeginReturningElementType(&elementType, size: &size) |
| 99 | |
| 100 | return (TType(rawValue: elementType)!, size) |
| 101 | } |
| 102 | |
| 103 | public func writeListBeginWithElementType(elementType: TType, size: Int) throws { |
| 104 | try writeListBeginWithElementType(elementType.rawValue, size: Int32(size)) |
| 105 | } |
| 106 | |
| 107 | public func writeFieldValue<T: TSerializable>(value: T, name: String, type: TType, id: Int32) throws { |
| 108 | try writeFieldBeginWithName(name, type: type.rawValue, fieldID: id) |
| 109 | try writeValue(value) |
| 110 | try writeFieldEnd() |
| 111 | } |
| 112 | |
| 113 | public func readValue<T: TSerializable>() throws -> T { |
| 114 | return try T.readValueFromProtocol(self) |
| 115 | } |
| 116 | |
| 117 | public func writeValue<T: TSerializable>(value: T) throws { |
| 118 | try T.writeValue(value, toProtocol: self) |
| 119 | } |
| 120 | |
| 121 | public func readResultMessageBegin() throws { |
| 122 | |
| 123 | let (_, type, _) = try readMessageBegin(); |
| 124 | |
| 125 | if type == .EXCEPTION { |
| 126 | let x = try readException() |
| 127 | throw x |
| 128 | } |
| 129 | |
| 130 | return |
| 131 | } |
| 132 | |
| 133 | public func validateValue(value: Any?, named name: String) throws { |
| 134 | |
| 135 | if value == nil { |
| 136 | throw NSError( |
| 137 | domain: TProtocolErrorDomain, |
| 138 | code: Int(TProtocolError.Unknown.rawValue), |
| 139 | userInfo: [TProtocolErrorFieldNameKey: name]) |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
| 144 | public func readException() throws -> ErrorType { |
| 145 | |
| 146 | var reason : String? |
| 147 | var type = TApplicationError.Unknown |
| 148 | |
| 149 | try readStructBegin() |
| 150 | |
| 151 | fields: while (true) { |
| 152 | |
| 153 | let (_, fieldType, fieldID) = try readFieldBegin() |
| 154 | |
| 155 | switch (fieldID, fieldType) { |
| 156 | case (_, .STOP): |
| 157 | break fields |
| 158 | |
| 159 | case (1, .STRING): |
| 160 | reason = try readValue() as String |
| 161 | |
| 162 | case (2, .I32): |
| 163 | let typeVal = try readValue() as Int32 |
| 164 | if let tmp = TApplicationError(rawValue: typeVal) { |
| 165 | type = tmp |
| 166 | } |
| 167 | |
| 168 | case let (_, unknownType): |
| 169 | try skipType(unknownType) |
| 170 | } |
| 171 | |
| 172 | try readFieldEnd() |
| 173 | } |
| 174 | |
| 175 | try readStructEnd() |
| 176 | |
| 177 | return NSError(type:type, reason:reason ?? "") |
| 178 | } |
| 179 | |
| 180 | public func writeExceptionForMessageName(name: String, sequenceID: Int, ex: NSError) throws { |
| 181 | try writeMessageBeginWithName(name, type: .EXCEPTION, sequenceID: sequenceID) |
| 182 | try ex.write(self) |
| 183 | try writeMessageEnd() |
| 184 | } |
| 185 | |
| 186 | public func skipType(type: TType) throws { |
| 187 | try TProtocolUtil.skipType(type.rawValue, onProtocol: self) |
| 188 | } |
| 189 | |
| 190 | } |