THRIFT-4902: Swift 4 and 5 compatibility (#1827)

This commit is contained in:
Antoine Cœur 2019-07-08 18:42:09 +08:00 committed by James E. King III
parent f9435643d7
commit 08a6eb6e1f
13 changed files with 96 additions and 66 deletions

View File

@ -149,6 +149,27 @@ jobs:
- JOB="Coverity Scan" - JOB="Coverity Scan"
- SCRIPT="covscan.sh" - SCRIPT="covscan.sh"
# ------------------------- phase: swift ------------------------
# We lint the podspec
- os: osx
osx_image: xcode10.1
language: swift
script:
- gem update cocoapods
- pod lib lint --allow-warnings --swift-version=3
env:
- JOB="pod lib lint"
- os: osx
osx_image: xcode10.2
language: swift
script:
- gem update cocoapods
- pod lib lint --allow-warnings --swift-version=4.0
- pod lib lint --allow-warnings --swift-version=4.2
- pod lib lint --allow-warnings --swift-version=5.0
env:
- JOB="pod lib lint"
### ------------------------- phase: osx -------------------------- ### ------------------------- phase: osx --------------------------
# disabled due to the time delays it imposes on build jobs # disabled due to the time delays it imposes on build jobs

View File

@ -1,16 +1,15 @@
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = "Thrift-swift3" s.name = 'Thrift'
s.version = "0.13.0" s.version = '0.13.0'
s.summary = "Apache Thrift is a lightweight, language-independent software stack with an associated code generation mechanism for RPC." s.summary = "Apache Thrift is a lightweight, language-independent software stack with an associated code generation mechanism for RPC."
s.description = <<-DESC s.description = <<-DESC
The Apache Thrift scalable cross-language software framework for networked services development combines a software stack with a code generation engine to build services that work efficiently and seamlessly between many programming languages. The Apache Thrift scalable cross-language software framework for networked services development combines a software stack with a code generation engine to build services that work efficiently and seamlessly between many programming languages.
DESC DESC
s.homepage = "http://thrift.apache.org" s.homepage = 'https://thrift.apache.org'
s.license = { :type => 'Apache License, Version 2.0', :url => 'https://www.apache.org/licenses/LICENSE-2.0' } s.license = { :type => 'Apache License, Version 2.0', :url => 'https://www.apache.org/licenses/LICENSE-2.0' }
s.author = { "Apache Thrift Developers" => "dev@thrift.apache.org" } s.author = { 'Apache Thrift Developers' => 'dev@thrift.apache.org' }
s.ios.deployment_target = '9.0' s.ios.deployment_target = '9.0'
s.osx.deployment_target = '10.10' s.osx.deployment_target = '10.10'
s.requires_arc = true s.source = { :git => 'https://github.com/apache/thrift.git', :tag => 'v0.13.0' }
s.source = { :git => "https://github.com/apache/thrift.git", :tag => "v0.13.0" } s.source_files = 'lib/swift/Sources/*.swift'
s.source_files = "lib/swift/Sources/*.swift"
end end

View File

@ -325,7 +325,7 @@ public class TBinaryProtocol: TProtocol {
} }
public func write(_ value: UInt8) throws { public func write(_ value: UInt8) throws {
let buff = Data(bytes: [value]) let buff = Data([value])
try ProtocolTransportTry(error: TProtocolError(message: "Transport write failed")) { try ProtocolTransportTry(error: TProtocolError(message: "Transport write failed")) {
try self.transport.write(data: buff) try self.transport.write(data: buff)
@ -334,8 +334,8 @@ public class TBinaryProtocol: TProtocol {
public func write(_ value: Int16) throws { public func write(_ value: Int16) throws {
var buff = Data() var buff = Data()
buff.append(Data(bytes: [UInt8(0xff & (value >> 8))])) buff.append(Data([UInt8(0xff & (value >> 8))]))
buff.append(Data(bytes: [UInt8(0xff & (value))])) buff.append(Data([UInt8(0xff & (value))]))
try ProtocolTransportTry(error: TProtocolError(message: "Transport write failed")) { try ProtocolTransportTry(error: TProtocolError(message: "Transport write failed")) {
try self.transport.write(data: buff) try self.transport.write(data: buff)
} }
@ -343,10 +343,10 @@ public class TBinaryProtocol: TProtocol {
public func write(_ value: Int32) throws { public func write(_ value: Int32) throws {
var buff = Data() var buff = Data()
buff.append(Data(bytes: [UInt8(0xff & (value >> 24))])) buff.append(Data([UInt8(0xff & (value >> 24))]))
buff.append(Data(bytes: [UInt8(0xff & (value >> 16))])) buff.append(Data([UInt8(0xff & (value >> 16))]))
buff.append(Data(bytes: [UInt8(0xff & (value >> 8))])) buff.append(Data([UInt8(0xff & (value >> 8))]))
buff.append(Data(bytes: [UInt8(0xff & (value))])) buff.append(Data([UInt8(0xff & (value))]))
try ProtocolTransportTry(error: TProtocolError(message: "Transport write failed")) { try ProtocolTransportTry(error: TProtocolError(message: "Transport write failed")) {
try self.transport.write(data: buff) try self.transport.write(data: buff)
@ -355,14 +355,14 @@ public class TBinaryProtocol: TProtocol {
public func write(_ value: Int64) throws { public func write(_ value: Int64) throws {
var buff = Data() var buff = Data()
buff.append(Data(bytes: [UInt8(0xff & (value >> 56))])) buff.append(Data([UInt8(0xff & (value >> 56))]))
buff.append(Data(bytes: [UInt8(0xff & (value >> 48))])) buff.append(Data([UInt8(0xff & (value >> 48))]))
buff.append(Data(bytes: [UInt8(0xff & (value >> 40))])) buff.append(Data([UInt8(0xff & (value >> 40))]))
buff.append(Data(bytes: [UInt8(0xff & (value >> 32))])) buff.append(Data([UInt8(0xff & (value >> 32))]))
buff.append(Data(bytes: [UInt8(0xff & (value >> 24))])) buff.append(Data([UInt8(0xff & (value >> 24))]))
buff.append(Data(bytes: [UInt8(0xff & (value >> 16))])) buff.append(Data([UInt8(0xff & (value >> 16))]))
buff.append(Data(bytes: [UInt8(0xff & (value >> 8))])) buff.append(Data([UInt8(0xff & (value >> 8))]))
buff.append(Data(bytes: [UInt8(0xff & (value))])) buff.append(Data([UInt8(0xff & (value))]))
try ProtocolTransportTry(error: TProtocolError(message: "Transport write failed")) { try ProtocolTransportTry(error: TProtocolError(message: "Transport write failed")) {
try self.transport.write(data: buff) try self.transport.write(data: buff)

View File

@ -67,7 +67,7 @@ public class TCompactProtocol: TProtocol {
/// Mark: - TCompactProtocol helpers /// Mark: - TCompactProtocol helpers
func writebyteDirect(_ byte: UInt8) throws { func writebyteDirect(_ byte: UInt8) throws {
let byte = Data(bytes: [byte]) let byte = Data([byte])
try ProtocolTransportTry(error: TProtocolError(message: "Transport Write Failed")) { try ProtocolTransportTry(error: TProtocolError(message: "Transport Write Failed")) {
try self.transport.write(data: byte) try self.transport.write(data: byte)
} }
@ -90,7 +90,7 @@ public class TCompactProtocol: TProtocol {
} }
try ProtocolTransportTry(error: TProtocolError(message: "Transport Write Failed")) { try ProtocolTransportTry(error: TProtocolError(message: "Transport Write Failed")) {
try self.transport.write(data: Data(bytes: i32buf[0..<idx])) try self.transport.write(data: Data(i32buf[0..<idx]))
} }
} }
@ -111,9 +111,8 @@ public class TCompactProtocol: TProtocol {
} }
try ProtocolTransportTry(error: TProtocolError(message: "Transport Write Failed")) { try ProtocolTransportTry(error: TProtocolError(message: "Transport Write Failed")) {
try self.transport.write(data: Data(bytes: varint64out[0..<idx])) try self.transport.write(data: Data(varint64out[0..<idx]))
} }
} }
func writeCollectionBegin(_ elementType: TType, size: Int32) throws { func writeCollectionBegin(_ elementType: TType, size: Int32) throws {
@ -249,7 +248,6 @@ public class TCompactProtocol: TProtocol {
let got = String(format:"%2X", protocolId) let got = String(format:"%2X", protocolId)
throw TProtocolError(message: "Wrong Protocol ID \(got)", throw TProtocolError(message: "Wrong Protocol ID \(got)",
extendedError: .mismatchedProtocol(expected: expected, got: got)) extendedError: .mismatchedProtocol(expected: expected, got: got))
} }
let versionAndType: UInt8 = try read() let versionAndType: UInt8 = try read()
@ -257,7 +255,6 @@ public class TCompactProtocol: TProtocol {
if version != TCompactProtocol.version { if version != TCompactProtocol.version {
throw TProtocolError(error: .badVersion(expected: "\(TCompactProtocol.version)", throw TProtocolError(error: .badVersion(expected: "\(TCompactProtocol.version)",
got:"\(version)")) got:"\(version)"))
} }
let type = (versionAndType >> UInt8(TCType.typeShiftAmount)) & TCType.typeBits let type = (versionAndType >> UInt8(TCType.typeShiftAmount)) & TCType.typeBits
@ -485,7 +482,6 @@ public class TCompactProtocol: TProtocol {
} }
lastFieldId = UInt8(fieldID) lastFieldId = UInt8(fieldID)
} }
public func writeFieldStop() throws { public func writeFieldStop() throws {

View File

@ -92,10 +92,10 @@ public class TFramedTransport: TTransport {
private func encodeFrameSize(size: UInt32) -> Data { private func encodeFrameSize(size: UInt32) -> Data {
var data = Data() var data = Data()
data.append(Data(bytes: [UInt8(0xff & (size >> 24))])) data.append(Data([UInt8(0xff & (size >> 24))]))
data.append(Data(bytes: [UInt8(0xff & (size >> 16))])) data.append(Data([UInt8(0xff & (size >> 16))]))
data.append(Data(bytes: [UInt8(0xff & (size >> 8))])) data.append(Data([UInt8(0xff & (size >> 8))]))
data.append(Data(bytes: [UInt8(0xff & (size))])) data.append(Data([UInt8(0xff & (size))]))
return data return data
} }

View File

@ -21,7 +21,7 @@ public struct TMap<Key : TSerializable & Hashable, Value : TSerializable>: Colle
public typealias Storage = Dictionary<Key, Value> public typealias Storage = Dictionary<Key, Value>
public typealias Element = Storage.Element public typealias Element = Storage.Element
public typealias Index = Storage.Index public typealias Index = Storage.Index
public typealias IndexDistance = Storage.IndexDistance public typealias IndexDistance = Int
public typealias Indices = Storage.Indices public typealias Indices = Storage.Indices
public typealias SubSequence = Storage.SubSequence public typealias SubSequence = Storage.SubSequence
internal var storage = Storage() internal var storage = Storage()
@ -33,11 +33,7 @@ public struct TMap<Key : TSerializable & Hashable, Value : TSerializable>: Colle
} }
public mutating func updateValue(_ value: Value, forKey key: Key) -> Value? { public mutating func updateValue(_ value: Value, forKey key: Key) -> Value? {
return updateValue(value, forKey: key) return storage.updateValue(value, forKey: key)
}
public mutating func removeAtIndex(_ index: DictionaryIndex<Key, Value>) -> (Key, Value) {
return removeAtIndex(index)
} }
public mutating func removeValueForKey(_ key: Key) -> Value? { public mutating func removeValueForKey(_ key: Key) -> Value? {

View File

@ -18,7 +18,6 @@
*/ */
import Foundation import Foundation
//
public enum TMessageType: Int32 { public enum TMessageType: Int32 {
case call = 1 case call = 1
@ -99,19 +98,19 @@ public protocol TProtocol {
} }
public extension TProtocol { public extension TProtocol {
public func writeFieldValue(_ value: TSerializable, name: String, type: TType, id: Int32) throws { func writeFieldValue(_ value: TSerializable, name: String, type: TType, id: Int32) throws {
try writeFieldBegin(name: name, type: type, fieldID: id) try writeFieldBegin(name: name, type: type, fieldID: id)
try value.write(to: self) try value.write(to: self)
try writeFieldEnd() try writeFieldEnd()
} }
public func validateValue(_ value: Any?, named name: String) throws { func validateValue(_ value: Any?, named name: String) throws {
if value == nil { if value == nil {
throw TProtocolError(error: .unknown, message: "Missing required value for field: \(name)") throw TProtocolError(error: .unknown, message: "Missing required value for field: \(name)")
} }
} }
public func readResultMessageBegin() throws { func readResultMessageBegin() throws {
let (_, type, _) = try readMessageBegin(); let (_, type, _) = try readMessageBegin();
if type == .exception { if type == .exception {
let x = try readException() let x = try readException()
@ -120,17 +119,17 @@ public extension TProtocol {
return return
} }
public func readException() throws -> TApplicationError { func readException() throws -> TApplicationError {
return try TApplicationError.read(from: self) return try TApplicationError.read(from: self)
} }
public func writeException(messageName name: String, sequenceID: Int32, ex: TApplicationError) throws { func writeException(messageName name: String, sequenceID: Int32, ex: TApplicationError) throws {
try writeMessageBegin(name: name, type: .exception, sequenceID: sequenceID) try writeMessageBegin(name: name, type: .exception, sequenceID: sequenceID)
try ex.write(to: self) try ex.write(to: self)
try writeMessageEnd() try writeMessageEnd()
} }
public func skip(type: TType) throws { func skip(type: TType) throws {
switch type { switch type {
case .bool: _ = try read() as Bool case .bool: _ = try read() as Bool
case .i8: _ = try read() as UInt8 case .i8: _ = try read() as UInt8

View File

@ -20,6 +20,13 @@
import Foundation import Foundation
import CoreFoundation import CoreFoundation
#if !swift(>=4.2)
// Swift 3/4 compatibility
fileprivate extension RunLoopMode {
static let `default` = defaultRunLoopMode
}
#endif
#if os(Linux) #if os(Linux)
public class TSSLSocketTransport { public class TSSLSocketTransport {
init(hostname: String, port: UInt16) { init(hostname: String, port: UInt16) {
@ -98,18 +105,18 @@ public class TSSLSocketTransport: TStreamTransport {
CFReadStreamSetProperty(readStream?.takeRetainedValue(), CFReadStreamSetProperty(readStream?.takeRetainedValue(),
.SSLSettings, .SSLSettings,
settings as CFTypeRef!) settings as CFTypeRef)
CFWriteStreamSetProperty(writeStream?.takeRetainedValue(), CFWriteStreamSetProperty(writeStream?.takeRetainedValue(),
.SSLSettings, .SSLSettings,
settings as CFTypeRef!) settings as CFTypeRef)
inputStream = readStream!.takeRetainedValue() inputStream = readStream!.takeRetainedValue()
inputStream?.schedule(in: .current, forMode: .defaultRunLoopMode) inputStream?.schedule(in: .current, forMode: .default)
inputStream?.open() inputStream?.open()
outputStream = writeStream!.takeRetainedValue() outputStream = writeStream!.takeRetainedValue()
outputStream?.schedule(in: .current, forMode: .defaultRunLoopMode) outputStream?.schedule(in: .current, forMode: .default)
outputStream?.open() outputStream?.open()
readStream?.release() readStream?.release()

View File

@ -32,7 +32,7 @@ public struct TSet<Element : TSerializable & Hashable> : SetAlgebra, Hashable, C
public typealias Indices = Storage.Indices public typealias Indices = Storage.Indices
public typealias Index = Storage.Index public typealias Index = Storage.Index
public typealias IndexDistance = Storage.IndexDistance public typealias IndexDistance = Int
public typealias SubSequence = Storage.SubSequence public typealias SubSequence = Storage.SubSequence

View File

@ -105,7 +105,6 @@ class TSocketServer<InProtocol: TProtocol, OutProtocol: TProtocol, Processor: TP
[weak self] notification in [weak self] notification in
guard let strongSelf = self else { return } guard let strongSelf = self else { return }
strongSelf.connectionAccepted(strongSelf.socketFileHandle) strongSelf.connectionAccepted(strongSelf.socketFileHandle)
} }
// tell socket to listen // tell socket to listen

View File

@ -29,6 +29,13 @@
import Foundation import Foundation
import CoreFoundation import CoreFoundation
#if !swift(>=4.2)
// Swift 3/4 compatibility
fileprivate extension RunLoopMode {
static let `default` = defaultRunLoopMode
}
#endif
private struct Sys { private struct Sys {
#if os(Linux) #if os(Linux)
static let read = Glibc.read static let read = Glibc.read
@ -72,7 +79,7 @@ public class TCFSocketTransport: TStreamTransport {
var readStream: Unmanaged<CFReadStream>? var readStream: Unmanaged<CFReadStream>?
var writeStream: Unmanaged<CFWriteStream>? var writeStream: Unmanaged<CFWriteStream>?
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
hostname as CFString!, hostname as CFString,
UInt32(port), UInt32(port),
&readStream, &readStream,
&writeStream) &writeStream)
@ -88,11 +95,11 @@ public class TCFSocketTransport: TStreamTransport {
} }
inputStream = readStream as InputStream inputStream = readStream as InputStream
inputStream.schedule(in: .current, forMode: .defaultRunLoopMode) inputStream.schedule(in: .current, forMode: .default)
inputStream.open() inputStream.open()
outputStream = writeStream as OutputStream outputStream = writeStream as OutputStream
outputStream.schedule(in: .current, forMode: .defaultRunLoopMode) outputStream.schedule(in: .current, forMode: .default)
outputStream.open() outputStream.open()
} else { } else {
@ -140,7 +147,6 @@ public class TSocketTransport : TTransport {
} }
#if os(Linux) #if os(Linux)
let sock = socket(AF_INET, Int32(SOCK_STREAM.rawValue), 0) let sock = socket(AF_INET, Int32(SOCK_STREAM.rawValue), 0)
var addr = sockaddr_in(sin_family: sa_family_t(AF_INET), var addr = sockaddr_in(sin_family: sa_family_t(AF_INET),
@ -184,7 +190,7 @@ public class TSocketTransport : TTransport {
var buff = Array<UInt8>.init(repeating: 0, count: size) var buff = Array<UInt8>.init(repeating: 0, count: size)
let readBytes = Sys.read(socketDescriptor, &buff, size) let readBytes = Sys.read(socketDescriptor, &buff, size)
return Data(bytes: buff[0..<readBytes]) return Data(buff[0..<readBytes])
} }
public func write(data: Data) { public func write(data: Data) {

View File

@ -20,6 +20,13 @@
import Foundation import Foundation
import CoreFoundation import CoreFoundation
#if !swift(>=4.2)
// Swift 3/4 compatibility
fileprivate extension RunLoopMode {
static let `default` = defaultRunLoopMode
}
#endif
#if os(Linux) #if os(Linux)
/// Currently unavailable in Linux /// Currently unavailable in Linux
/// Remove comments and build to fix /// Remove comments and build to fix
@ -64,7 +71,7 @@ import CoreFoundation
if bytesRead <= 0 { if bytesRead <= 0 {
throw TTransportError(error: .notOpen) throw TTransportError(error: .notOpen)
} }
read.append(Data(bytes: buffer)) read.append(Data(buffer))
} }
return read return read
} }
@ -85,7 +92,7 @@ import CoreFoundation
break break
} }
read.append(Data(bytes: buffer)) read.append(Data(buffer))
} }
return read return read
} }
@ -124,7 +131,7 @@ import CoreFoundation
input?.delegate = nil input?.delegate = nil
input?.close() input?.close()
input?.remove(from: .current, forMode: .defaultRunLoopMode) input?.remove(from: .current, forMode: .default)
input = nil input = nil
} }
@ -135,7 +142,7 @@ import CoreFoundation
} }
output?.delegate = nil output?.delegate = nil
output?.close() output?.close()
output?.remove(from: .current, forMode: .defaultRunLoopMode) output?.remove(from: .current, forMode: .default)
output = nil output = nil
} }
} }

View File

@ -29,10 +29,10 @@ public protocol TStruct : TSerializable {
} }
public extension TStruct { public extension TStruct {
public static var fieldIds: [String: (id: Int32, type: TType)] { return [:] } static var fieldIds: [String: (id: Int32, type: TType)] { return [:] }
public static var thriftType: TType { return .struct } static var thriftType: TType { return .struct }
public func write(to proto: TProtocol) throws { func write(to proto: TProtocol) throws {
// Write struct name first // Write struct name first
try proto.writeStructBegin(name: Self.structName) try proto.writeStructBegin(name: Self.structName)
@ -45,7 +45,7 @@ public extension TStruct {
try proto.writeStructEnd() try proto.writeStructEnd()
} }
public var hashValue: Int { var hashValue: Int {
let prime = 31 let prime = 31
var result = 1 var result = 1
self.forEach { _, value, _ in self.forEach { _, value, _ in