diff options
author | Chris Simpson <apocolipse@gmail.com> | 2018-08-29 14:40:44 -0400 |
---|---|---|
committer | James E. King III <jking@apache.org> | 2018-09-02 07:03:19 -0400 |
commit | 2566ecd5d9999f7ff70e6ac702243f0dfb24e7aa (patch) | |
tree | 672c226d7cbce38fef2f065351b24ec0a0b582dc /lib/swift | |
parent | 6e29b192a336bff7d2e22b8c73bc1f1216a41204 (diff) | |
download | thrift-2566ecd5d9999f7ff70e6ac702243f0dfb24e7aa.tar.gz |
Update Swift Library and tests
Diffstat (limited to 'lib/swift')
-rw-r--r-- | lib/swift/README.md | 5 | ||||
-rw-r--r-- | lib/swift/Sources/TBinaryProtocol.swift | 6 | ||||
-rw-r--r-- | lib/swift/Sources/TClient.swift | 4 | ||||
-rw-r--r-- | lib/swift/Sources/TCompactProtocol.swift | 8 | ||||
-rw-r--r-- | lib/swift/Sources/TList.swift | 2 | ||||
-rw-r--r-- | lib/swift/Sources/TMap.swift | 2 | ||||
-rw-r--r-- | lib/swift/Sources/TSet.swift | 2 | ||||
-rw-r--r-- | lib/swift/Sources/TSocketTransport.swift | 4 | ||||
-rw-r--r-- | lib/swift/Sources/Thrift.swift | 2 | ||||
-rw-r--r-- | lib/swift/Tests/ThriftTests/TBinaryProtocolTests.swift | 22 | ||||
-rw-r--r-- | lib/swift/Tests/ThriftTests/TCompactProtocolTests.swift | 68 | ||||
-rw-r--r-- | lib/swift/Tests/ThriftTests/ThriftTests.swift | 8 |
12 files changed, 112 insertions, 21 deletions
diff --git a/lib/swift/README.md b/lib/swift/README.md index 50f522dc3..2ad88d618 100644 --- a/lib/swift/README.md +++ b/lib/swift/README.md @@ -20,6 +20,8 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +Brought to you by [FiscalNote, Inc](http://www.fiscalnote.com/) + ## Build @@ -88,10 +90,11 @@ func write(_ val: String) throws #### Generator Flags | Flag | Description | | ------------- |:-------------:| -| async_clients | Generate clients which invoke asynchronously via block syntax.Asynchronous classes are appended with `_Async` | +| async_clients | Generate clients which invoke asynchronously via block syntax. Asynchronous classes are appended with `_Async` | | no_strict* | Generates non-strict structs | | debug_descriptions | Allow use of debugDescription so the app can add description via a cateogory/extension | | log_unexpected | Log every time an unexpected field ID or type is encountered. | +| safe_enums | Generate enum types with an unknown case to handle unspecified values rather than throw a serialization error | diff --git a/lib/swift/Sources/TBinaryProtocol.swift b/lib/swift/Sources/TBinaryProtocol.swift index 47f3f283f..a97249abc 100644 --- a/lib/swift/Sources/TBinaryProtocol.swift +++ b/lib/swift/Sources/TBinaryProtocol.swift @@ -79,7 +79,7 @@ public class TBinaryProtocol: TProtocol { messageName = try read() } else { if strictRead { - let errorMessage = "Missing message version, old client? Message Name: \(currentMessageName)" + let errorMessage = "Missing message version, old client? Message Name: \(currentMessageName ?? "")" throw TProtocolError(error: .invalidData, message: errorMessage) } @@ -233,7 +233,7 @@ public class TBinaryProtocol: TProtocol { public func read() throws -> Double { let val = try read() as Int64 - return unsafeBitCast(val, to: Double.self) + return Double(bitPattern: UInt64(bitPattern: val)) } public func read() throws -> Data { @@ -371,7 +371,7 @@ public class TBinaryProtocol: TProtocol { public func write(_ value: Double) throws { // Notably unsafe, since Double and Int64 are the same size, this should work fine - try self.write(unsafeBitCast(value, to: Int64.self)) + try self.write(Int64(bitPattern: value.bitPattern)) } public func write(_ data: Data) throws { diff --git a/lib/swift/Sources/TClient.swift b/lib/swift/Sources/TClient.swift index c404c9aa6..cc3288a8c 100644 --- a/lib/swift/Sources/TClient.swift +++ b/lib/swift/Sources/TClient.swift @@ -22,12 +22,12 @@ open class TClient { public let inProtocol: TProtocol public let outProtocol: TProtocol - public init(inoutProtocol: TProtocol) { + required public init(inoutProtocol: TProtocol) { self.inProtocol = inoutProtocol self.outProtocol = inoutProtocol } - public init(inProtocol: TProtocol, outProtocol: TProtocol) { + required public init(inProtocol: TProtocol, outProtocol: TProtocol) { self.inProtocol = inProtocol self.outProtocol = outProtocol } diff --git a/lib/swift/Sources/TCompactProtocol.swift b/lib/swift/Sources/TCompactProtocol.swift index ac5b35adf..59773c34a 100644 --- a/lib/swift/Sources/TCompactProtocol.swift +++ b/lib/swift/Sources/TCompactProtocol.swift @@ -222,11 +222,11 @@ public class TCompactProtocol: TProtocol { /// /// - returns: zigzaged UInt32 func i32ToZigZag(_ n : Int32) -> UInt32 { - return UInt32(n << 1) ^ UInt32(n >> 31) + return UInt32(bitPattern: Int32(n << 1) ^ Int32(n >> 31)) } func i64ToZigZag(_ n : Int64) -> UInt64 { - return UInt64(n << 1) ^ UInt64(n >> 63) + return UInt64(bitPattern: Int64(n << 1) ^ Int64(n >> 63)) } func zigZagToi32(_ n: UInt32) -> Int32 { @@ -379,7 +379,7 @@ public class TCompactProtocol: TProtocol { return UnsafePointer<UInt64>(OpaquePointer(ptr)).pointee } let bits = CFSwapInt64LittleToHost(i64) - return unsafeBitCast(bits, to: Double.self) + return Double(bitPattern: bits) } public func read() throws -> Data { @@ -557,7 +557,7 @@ public class TCompactProtocol: TProtocol { } public func write(_ value: Double) throws { - var bits = CFSwapInt64HostToLittle(unsafeBitCast(value, to: UInt64.self)) + var bits = CFSwapInt64HostToLittle(value.bitPattern) let data = withUnsafePointer(to: &bits) { return Data(bytes: UnsafePointer<UInt8>(OpaquePointer($0)), count: MemoryLayout<UInt64>.size) } diff --git a/lib/swift/Sources/TList.swift b/lib/swift/Sources/TList.swift index 007715608..c239d10c5 100644 --- a/lib/swift/Sources/TList.swift +++ b/lib/swift/Sources/TList.swift @@ -18,7 +18,7 @@ */ public struct TList<Element : TSerializable> : RandomAccessCollection, MutableCollection, ExpressibleByArrayLiteral, TSerializable, Hashable { - typealias Storage = Array<Element> + public typealias Storage = Array<Element> public typealias Indices = Storage.Indices internal var storage = Storage() diff --git a/lib/swift/Sources/TMap.swift b/lib/swift/Sources/TMap.swift index f8b02d29d..898037739 100644 --- a/lib/swift/Sources/TMap.swift +++ b/lib/swift/Sources/TMap.swift @@ -18,7 +18,7 @@ */ public struct TMap<Key : TSerializable & Hashable, Value : TSerializable>: Collection, ExpressibleByDictionaryLiteral, Hashable, TSerializable { - typealias Storage = Dictionary<Key, Value> + public typealias Storage = Dictionary<Key, Value> public typealias Element = Storage.Element public typealias Index = Storage.Index public typealias IndexDistance = Storage.IndexDistance diff --git a/lib/swift/Sources/TSet.swift b/lib/swift/Sources/TSet.swift index 3e014c1b9..1ecd170a5 100644 --- a/lib/swift/Sources/TSet.swift +++ b/lib/swift/Sources/TSet.swift @@ -21,7 +21,7 @@ import Foundation public struct TSet<Element : TSerializable & Hashable> : SetAlgebra, Hashable, Collection, ExpressibleByArrayLiteral, TSerializable { /// Typealias for Storage type - typealias Storage = Set<Element> + public typealias Storage = Set<Element> /// Internal Storage used for TSet (Set\<Element\>) diff --git a/lib/swift/Sources/TSocketTransport.swift b/lib/swift/Sources/TSocketTransport.swift index 891bd27e0..0316e37d7 100644 --- a/lib/swift/Sources/TSocketTransport.swift +++ b/lib/swift/Sources/TSocketTransport.swift @@ -83,8 +83,8 @@ public class TCFSocketTransport: TStreamTransport { CFWriteStreamSetProperty(writeStream, .shouldCloseNativeSocket, kCFBooleanTrue) if secure { - CFReadStreamSetProperty(readStream, .socketSecurityLevel, StreamSocketSecurityLevel.negotiatedSSL._rawValue) - CFWriteStreamSetProperty(writeStream, .socketSecurityLevel, StreamSocketSecurityLevel.negotiatedSSL._rawValue) + CFReadStreamSetProperty(readStream, .socketSecurityLevel, StreamSocketSecurityLevel.negotiatedSSL.rawValue as CFString) + CFWriteStreamSetProperty(writeStream, .socketSecurityLevel, StreamSocketSecurityLevel.negotiatedSSL.rawValue as CFString) } inputStream = readStream as InputStream diff --git a/lib/swift/Sources/Thrift.swift b/lib/swift/Sources/Thrift.swift index afa309692..5bd175809 100644 --- a/lib/swift/Sources/Thrift.swift +++ b/lib/swift/Sources/Thrift.swift @@ -1,3 +1,3 @@ class Thrift { - let version = "0.0.1" + let version = "1.1.0" } diff --git a/lib/swift/Tests/ThriftTests/TBinaryProtocolTests.swift b/lib/swift/Tests/ThriftTests/TBinaryProtocolTests.swift index f0e48b961..56a557261 100644 --- a/lib/swift/Tests/ThriftTests/TBinaryProtocolTests.swift +++ b/lib/swift/Tests/ThriftTests/TBinaryProtocolTests.swift @@ -129,6 +129,28 @@ class TBinaryProtocolTests: XCTestCase { XCTAssertFalse(true, "Caught Error attempting to read \(error)") } } + func testUnsafeBitcastUpdate() { + let value: Double = 3.14159 + let val: Int64 = 31415926 + let uval: UInt64 = 31415926 + + let i64 = Int64(bitPattern: value.bitPattern) + let ubc = unsafeBitCast(value, to: Int64.self) + + XCTAssertEqual(i64, ubc, "Bitcast Double-> i64 Values don't match") + + let dbl = Double(bitPattern: UInt64(val)) + let ubdb = unsafeBitCast(val, to: Double.self) + + XCTAssertEqual(dbl, ubdb, "Bitcast i64 -> Double Values don't match") + + let db2 = Double(bitPattern: uval) + let usbc2 = unsafeBitCast(uval, to: Double.self) + + XCTAssertEqual(db2, usbc2, "Bitcast u64 -> Double Values don't match") + + + } static var allTests : [(String, (TBinaryProtocolTests) -> () throws -> Void)] { return [ diff --git a/lib/swift/Tests/ThriftTests/TCompactProtocolTests.swift b/lib/swift/Tests/ThriftTests/TCompactProtocolTests.swift index ccc32aa5c..882c26068 100644 --- a/lib/swift/Tests/ThriftTests/TCompactProtocolTests.swift +++ b/lib/swift/Tests/ThriftTests/TCompactProtocolTests.swift @@ -128,6 +128,70 @@ class TCompactProtocolTests: XCTestCase { } } + func testInt32ZigZag() { + let zero: Int32 = 0 + let one: Int32 = 1 + let nOne: Int32 = -1 + let two: Int32 = 2 + let nTwo: Int32 = -2 + let max = Int32.max + let min = Int32.min + + XCTAssertEqual(proto.i32ToZigZag(zero), UInt32(0), "Error 32bit zigzag on \(zero)") + XCTAssertEqual(proto.zigZagToi32(0), zero, "Error 32bit zigzag on \(zero)") + + XCTAssertEqual(proto.i32ToZigZag(nOne), UInt32(1), "Error 32bit zigzag on \(nOne)") + XCTAssertEqual(proto.zigZagToi32(1), nOne, "Error 32bit zigzag on \(nOne)") + + XCTAssertEqual(proto.i32ToZigZag(one), UInt32(2), "Error 32bit zigzag on \(one)") + XCTAssertEqual(proto.zigZagToi32(2), one, "Error 32bit zigzag on \(one)") + + XCTAssertEqual(proto.i32ToZigZag(nTwo), UInt32(3), "Error 32bit zigzag on \(nTwo)") + XCTAssertEqual(proto.zigZagToi32(3), nTwo, "Error 32bit zigzag on \(nTwo)") + + XCTAssertEqual(proto.i32ToZigZag(two), UInt32(4), "Error 32bit zigzag on \(two)") + XCTAssertEqual(proto.zigZagToi32(4), two, "Error 32bit zigzag on \(two)") + + let uMaxMinusOne: UInt32 = UInt32.max - 1 + XCTAssertEqual(proto.i32ToZigZag(max), uMaxMinusOne, "Error 32bit zigzag on \(max)") + XCTAssertEqual(proto.zigZagToi32(uMaxMinusOne), max, "Error 32bit zigzag on \(max)") + + XCTAssertEqual(proto.i32ToZigZag(min), UInt32.max, "Error 32bit zigzag on \(min)") + XCTAssertEqual(proto.zigZagToi32(UInt32.max), min, "Error 32bit zigzag on \(min)") + } + + func testInt64ZigZag() { + let zero: Int64 = 0 + let one: Int64 = 1 + let nOne: Int64 = -1 + let two: Int64 = 2 + let nTwo: Int64 = -2 + let max = Int64.max + let min = Int64.min + + XCTAssertEqual(proto.i64ToZigZag(zero), UInt64(0), "Error 64bit zigzag on \(zero)") + XCTAssertEqual(proto.zigZagToi64(0), zero, "Error 64bit zigzag on \(zero)") + + XCTAssertEqual(proto.i64ToZigZag(nOne), UInt64(1), "Error 64bit zigzag on \(nOne)") + XCTAssertEqual(proto.zigZagToi64(1), nOne, "Error 64bit zigzag on \(nOne)") + + XCTAssertEqual(proto.i64ToZigZag(one), UInt64(2), "Error 64bit zigzag on \(one)") + XCTAssertEqual(proto.zigZagToi64(2), one, "Error 64bit zigzag on \(one)") + + XCTAssertEqual(proto.i64ToZigZag(nTwo), UInt64(3), "Error 64bit zigzag on \(nTwo)") + XCTAssertEqual(proto.zigZagToi64(3), nTwo, "Error 64bit zigzag on \(nTwo)") + + XCTAssertEqual(proto.i64ToZigZag(two), UInt64(4), "Error 64bit zigzag on \(two)") + XCTAssertEqual(proto.zigZagToi64(4), two, "Error 64bit zigzag on \(two)") + + let uMaxMinusOne: UInt64 = UInt64.max - 1 + XCTAssertEqual(proto.i64ToZigZag(max), uMaxMinusOne, "Error 64bit zigzag on \(max)") + XCTAssertEqual(proto.zigZagToi64(uMaxMinusOne), max, "Error 64bit zigzag on \(max)") + + XCTAssertEqual(proto.i64ToZigZag(min), UInt64.max, "Error 64bit zigzag on \(min)") + XCTAssertEqual(proto.zigZagToi64(UInt64.max), min, "Error 64bit zigzag on \(min)") + } + static var allTests : [(String, (TCompactProtocolTests) -> () throws -> Void)] { return [ ("testInt8WriteRead", testInt8WriteRead), @@ -138,7 +202,9 @@ class TCompactProtocolTests: XCTestCase { ("testBoolWriteRead", testBoolWriteRead), ("testStringWriteRead", testStringWriteRead), ("testDataWriteRead", testDataWriteRead), - ("testStructWriteRead", testStructWriteRead) + ("testStructWriteRead", testStructWriteRead), + ("testInt32ZigZag", testInt32ZigZag), + ("testInt64ZigZag", testInt64ZigZag) ] } } diff --git a/lib/swift/Tests/ThriftTests/ThriftTests.swift b/lib/swift/Tests/ThriftTests/ThriftTests.swift index 0c81330cb..931610018 100644 --- a/lib/swift/Tests/ThriftTests/ThriftTests.swift +++ b/lib/swift/Tests/ThriftTests/ThriftTests.swift @@ -3,13 +3,13 @@ import XCTest class ThriftTests: XCTestCase { func testVersion() { - XCTAssertEqual(Thrift().version, "0.0.1") + XCTAssertEqual(Thrift().version, "1.1.0") } - + func test_in_addr_extension() { - + } - + static var allTests : [(String, (ThriftTests) -> () throws -> Void)] { return [ ("testVersion", testVersion), |