Skip to content
This repository has been archived by the owner on Oct 29, 2023. It is now read-only.

Commit

Permalink
Linux support (cloudant#162)
Browse files Browse the repository at this point in the history
* Add linux support

* Add Linux support for changes

* call finish for operations on linux

* Convert headers to String:String for all platforms

* Changed the downcasting from [String:NSObject] to [String:Any]

* Issue-285 [BulkDocsTests failure Changed casting from NSDictionary to [String: Any]

* Update linux build to Swift 3.1

* Update .swift-version to 3.1

* Use KVO APIs for Operations

* Update Xcode version for macOS Travis builds

* Workaround lack of Bundle(for:) on Linux (cloudant#155)

* Workaround lack of Bundle(for:) on Linux

* Specify version number instead of using Unknown

* Specify 3.1.1 in .swift-version (cloudant#156)

* Specify 3.1.1 in .swift-version
* Update Travis build to Swift 3.1.1

* Import Dispatch explicitly

* Avoid bridging between Foundation and stdlib types

* Remove InterceptorTests

* Crash in Cloudant tests due to httpAdditionalHeaders [382]

* Update default bundleVersionString to 0.7.0

* Whitespace
  • Loading branch information
ianpartridge authored Jul 6, 2017
1 parent 997ee68 commit 5d28657
Show file tree
Hide file tree
Showing 28 changed files with 345 additions and 360 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0
3.1.1
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ matrix:
- sudo apt-get install -y build-essential git libcurl3 libblocksruntime-dev clang libicu-dev uuid-dev
- wget -q -O - https://swift.org/keys/all-keys.asc | gpg --import -
- gpg --keyserver hkp://pool.sks-keyservers.net --refresh-keys Swift
- wget https://swift.org/builds/swift-3.0-release/ubuntu1404/swift-3.0-RELEASE/swift-3.0-RELEASE-ubuntu14.04.tar.gz
- gunzip swift-3.0-RELEASE-ubuntu14.04.tar.gz
- tar -xvf swift-3.0-RELEASE-ubuntu14.04.tar
- export PATH=${PWD}/swift-3.0-RELEASE-ubuntu14.04/usr/bin:"${PATH}"
- wget https://swift.org/builds/swift-3.1.1-release/ubuntu1404/swift-3.1.1-RELEASE/swift-3.1.1-RELEASE-ubuntu14.04.tar.gz
- gunzip swift-3.1.1-RELEASE-ubuntu14.04.tar.gz
- tar -xvf swift-3.1.1-RELEASE-ubuntu14.04.tar
- export PATH=${PWD}/swift-3.1.1-RELEASE-ubuntu14.04/usr/bin:"${PATH}"
- os: osx
language: objective-c
osx_image: xcode8
osx_image: xcode8.3
before_script:
- brew install couchdb
- brew services start couchdb
Expand Down
4 changes: 0 additions & 4 deletions Source/SwiftCloudant/HTTP/RequestExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,11 @@ class OperationRequestExecutor: InterceptableSessionDelegate {
let httpInfo: HTTPInfo?

if let response = response {
#if os(Linux)
httpInfo = HTTPInfo(statusCode: response.statusCode, headers: response.allHeaderFields)
#else
var headers:[String: String] = [:]
for (key, value) in response.allHeaderFields {
headers["\(key)"] = "\(value)"
}
httpInfo = HTTPInfo(statusCode: response.statusCode, headers: headers)
#endif
} else {
httpInfo = nil
}
Expand Down
27 changes: 12 additions & 15 deletions Source/SwiftCloudant/HTTP/URLSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,10 @@ internal class InterceptableSession: NSObject, URLSessionDelegate, URLSessionTas
// URLSession instance when it is required.
internal lazy var session: URLSession = { () -> URLSession in
let config = URLSessionConfiguration.default
#if os(Linux)
config.httpAdditionalHeaders = ["User-Agent".bridge() : InterceptableSession.userAgent().bridge()]
#else
config.httpAdditionalHeaders = [("User-Agent" as NSString) as AnyHashable: InterceptableSession.userAgent()]
#endif
config.httpAdditionalHeaders = ["User-Agent" as AnyHashable: InterceptableSession.userAgent()]
config.httpCookieAcceptPolicy = .onlyFromMainDocumentDomain
config.httpCookieStorage = .shared

return URLSession(configuration: config, delegate: self, delegateQueue: nil) }()

private var taskDict: [Foundation.URLSessionTask: URLSessionTask] = [:]
Expand Down Expand Up @@ -460,18 +457,18 @@ internal class InterceptableSession: NSObject, URLSessionDelegate, URLSessionTas
#else
let platform = "Unknown";
#endif
let frameworkBundle = Bundle(for: InterceptableSession.self)
var bundleDisplayName = frameworkBundle.object(forInfoDictionaryKey: "CFBundleName")
var bundleVersionString = frameworkBundle.object(forInfoDictionaryKey: "CFBundleShortVersionString")

if bundleDisplayName == nil {
bundleDisplayName = "SwiftCloudant"
}
if bundleVersionString == nil {
bundleVersionString = "Unknown"
}
var bundleDisplayName = "SwiftCloudant"
var bundleVersionString = "0.7.0"

#if !os(Linux)
// Bundle(for:) is not yet supported on Linux
let frameworkBundle = Bundle(for: InterceptableSession.self)
bundleDisplayName = frameworkBundle.object(forInfoDictionaryKey: "CFBundleName") as! String
bundleVersionString = frameworkBundle.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String
#endif

return "\(bundleDisplayName!)/\(bundleVersionString!)/\(platform)/\(osVersion))"
return "\(bundleDisplayName)/\(bundleVersionString)/\(platform)/\(osVersion))"

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ public class PutDocumentOperation: CouchDatabaseOperation, JSONOperation {
public let body: [String: Any]

public func validate() -> Bool {
#if os(Linux)
return NSJSONSerialization.isValidJSONObject(body!.bridge())
#else
return JSONSerialization.isValidJSONObject(body)
#endif
return JSONSerialization.isValidJSONObject(body)
}

public var method: String {
Expand Down Expand Up @@ -113,11 +109,7 @@ public class PutDocumentOperation: CouchDatabaseOperation, JSONOperation {
}

public func serialise() throws {
#if os(Linux)
data = try NSJSONSerialization.data(withJSONObject: body.bridge(), options: NSJSONWritingOptions())
#else
data = try JSONSerialization.data(withJSONObject: body)
#endif
data = try JSONSerialization.data(withJSONObject: body)
}

}
148 changes: 42 additions & 106 deletions Source/SwiftCloudant/Operations/Query/CreateQueryIndexOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,44 +96,24 @@ public class CreateJSONQueryIndexOperation: CouchDatabaseOperation, MangoOperati

public func serialise() throws {

#if os(Linux)
var jsonDict: [String:AnyObject] = ["type": "json".bridge()]
#else
var jsonDict: [String: Any] = ["type": "json"]
#endif
var jsonDict: [String: Any] = ["type": "json"]

var index: [String: Any] = [:]
#if os(Linux)
index["fields"] = transform(sortArray: fields).bridge()
jsonDict["index"] = index.bridge()
#else
index["fields"] = transform(sortArray: fields)
jsonDict["index"] = index
#endif
index["fields"] = transform(sortArray: fields)
jsonDict["index"] = index



if let name = name {
#if os(Linux)
jsonDict["name"] = name.bridge()
#else
jsonDict["name"] = name
#endif
jsonDict["name"] = name
}

if let designDocumentID = designDocumentID {
#if os(Linux)
jsonDict["ddoc"] = designDocumentID.bridge()
#else
jsonDict["ddoc"] = designDocumentID
#endif
jsonDict["ddoc"] = designDocumentID
}

#if os(Linux)
jsonData = try NSJSONSerialization.data(withJSONObject:jsonDict.bridge())
#else
jsonData = try JSONSerialization.data(withJSONObject: jsonDict)
#endif
jsonData = try JSONSerialization.data(withJSONObject: jsonDict)

}

}
Expand Down Expand Up @@ -285,11 +265,7 @@ public class CreateTextQueryIndexOperation: CouchDatabaseOperation, MangoOperati
public func validate() -> Bool {

if let selector = selector {
#if os(Linux)
return NSJSONSerialization.isValidJSONObject(selector.bridge())
#else
return JSONSerialization.isValidJSONObject(selector as NSDictionary)
#endif
return JSONSerialization.isValidJSONObject(selector)
}

return true
Expand All @@ -302,81 +278,41 @@ public class CreateTextQueryIndexOperation: CouchDatabaseOperation, MangoOperati
var index: [String: Any] = [:]
var defaultField: [String: Any] = [:]

#if os(Linux)
jsonDict["type"] = "text".bridge()

if let name = name {
jsonDict["name"] = name.bridge()
}

if let fields = fields {
index["fields"] = transform(fields: fields)
}

if let defaultFieldEnabled = defaultFieldEnabled {
defaultField["enabled"] = NSNumber(value: defaultFieldEnabled)
}

if let defaultFieldAnalyzer = defaultFieldAnalyzer {
defaultField["analyzer"] = defaultFieldAnalyzer.bridge()
}

if let designDocumentID = designDocumentID {
jsonDict["ddoc"] = designDocumentID.bridge()
}

if let selector = selector {
index["selector"] = selector.bridge()
}

if defaultField.count > 0 {
index["default_field"] = defaultField.bridge()
}

if index.count > 0 {
jsonDict["index"] = index.bridge()
}

self.jsonData = try NSJSONSerialization.data(withJSONObject:jsonDict.bridge())
#else
jsonDict["type"] = "text"

if let name = name {
jsonDict["name"] = name as NSString
}

if let fields = fields {
index["fields"] = transform(fields: fields)
}

if let defaultFieldEnabled = defaultFieldEnabled {
defaultField["enabled"] = defaultFieldEnabled as NSNumber
}

if let defaultFieldAnalyzer = defaultFieldAnalyzer {
defaultField["analyzer"] = defaultFieldAnalyzer as NSString
}

if let designDocumentID = designDocumentID {
jsonDict["ddoc"] = designDocumentID as NSString
}

if let selector = selector {
index["selector"] = selector as NSDictionary
}

if defaultField.count > 0 {
index["default_field"] = defaultField as NSDictionary
}

if index.count > 0 {
jsonDict["index"] = index as NSDictionary
}

self.jsonData = try JSONSerialization.data(withJSONObject:jsonDict as NSDictionary)
#endif
jsonDict["type"] = "text"


if let name = name {
jsonDict["name"] = name
}

if let fields = fields {
index["fields"] = transform(fields: fields)
}

if let defaultFieldEnabled = defaultFieldEnabled {
defaultField["enabled"] = defaultFieldEnabled
}

if let defaultFieldAnalyzer = defaultFieldAnalyzer {
defaultField["analyzer"] = defaultFieldAnalyzer
}

if let designDocumentID = designDocumentID {
jsonDict["ddoc"] = designDocumentID
}

if let selector = selector {
index["selector"] = selector
}

if defaultField.count > 0 {
index["default_field"] = defaultField
}

if index.count > 0 {
jsonDict["index"] = index
}

self.jsonData = try JSONSerialization.data(withJSONObject:jsonDict)

}

Expand Down
Loading

0 comments on commit 5d28657

Please sign in to comment.