Skip to content

Commit b66f99d

Browse files
Merge pull request #1256 from apollographql/update/namespacing
Adopt `.apollo.method()` namespacing for extensions
2 parents dee5372 + 83cedf5 commit b66f99d

41 files changed

Lines changed: 390 additions & 171 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Apollo.xcodeproj/project.pbxproj

Lines changed: 49 additions & 16 deletions
Large diffs are not rendered by default.

Sources/Apollo/ApolloStore.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public final class ApolloStore {
5757
self.cacheLock.withWriteLock {
5858
self.cache.clearPromise()
5959
}.andThen {
60-
DispatchQueue.apollo_returnResultAsyncIfNeeded(on: callbackQueue,
60+
DispatchQueue.apollo.returnResultAsyncIfNeeded(on: callbackQueue,
6161
action: completion,
6262
result: .success(()))
6363
}
@@ -115,12 +115,12 @@ public final class ApolloStore {
115115
Promise(fulfilled: try body($0))
116116
}
117117
.andThen { object in
118-
DispatchQueue.apollo_returnResultAsyncIfNeeded(on: callbackQueue,
118+
DispatchQueue.apollo.returnResultAsyncIfNeeded(on: callbackQueue,
119119
action: completion,
120120
result: .success(object))
121121
}
122122
.catch { error in
123-
DispatchQueue.apollo_returnResultAsyncIfNeeded(on: callbackQueue,
123+
DispatchQueue.apollo.returnResultAsyncIfNeeded(on: callbackQueue,
124124
action: completion,
125125
result: .failure(error))
126126
}
@@ -153,12 +153,12 @@ public final class ApolloStore {
153153
Promise(fulfilled: try body($0))
154154
}
155155
.andThen { object in
156-
DispatchQueue.apollo_returnResultAsyncIfNeeded(on: callbackQueue,
156+
DispatchQueue.apollo.returnResultAsyncIfNeeded(on: callbackQueue,
157157
action: completion,
158158
result: .success(object))
159159
}
160160
.catch { error in
161-
DispatchQueue.apollo_returnResultAsyncIfNeeded(on: callbackQueue,
161+
DispatchQueue.apollo.returnResultAsyncIfNeeded(on: callbackQueue,
162162
action: completion,
163163
result: .failure(error))
164164
}

Sources/Apollo/Bundle+Helpers.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import Foundation
2+
import ApolloCore
23

3-
extension Bundle {
4+
extension Bundle: ApolloCompatible {}
5+
6+
extension ApolloExtension where Base == Bundle {
47

58
/// Type-safe getter for info dictionary key objects
69
///
710
/// - Parameter key: The key to try to grab an object for
811
/// - Returns: The object of the desired type, or nil if it is not present or of the incorrect type.
912
func bundleValue<T>(forKey key: String) -> T? {
10-
return object(forInfoDictionaryKey: key) as? T
13+
return base.object(forInfoDictionaryKey: key) as? T
1114
}
1215

1316
/// The bundle identifier of this bundle, or nil if not present.

Sources/Apollo/Collection+Helpers.swift

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,5 @@
11
import Foundation
22

3-
// MARK: - Emptiness + Optionality
4-
5-
extension Collection {
6-
7-
/// Convenience helper to make `guard` statements more readable
8-
///
9-
/// - returns: `true` if the collection has contents.
10-
var isNotEmpty: Bool {
11-
return !self.isEmpty
12-
}
13-
}
14-
15-
extension Optional where Wrapped: Collection {
16-
17-
/// - returns: `true` if the collection is empty or nil
18-
var isEmptyOrNil: Bool {
19-
switch self {
20-
case .none:
21-
return true
22-
case .some(let collection):
23-
return collection.isEmpty
24-
}
25-
}
26-
27-
/// - returns: `true` if the collection is non-nil AND has contents.
28-
var isNotEmpty: Bool {
29-
switch self {
30-
case .none:
31-
return false
32-
case .some(let collection):
33-
return collection.isNotEmpty
34-
}
35-
}
36-
}
37-
383
// MARK: - Unzipping
394
// MARK: Arrays of tuples to tuples of arrays
405

Sources/Apollo/DispatchQueue+Optional.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import Foundation
2+
import ApolloCore
23

3-
public extension DispatchQueue {
4+
extension DispatchQueue: ApolloCompatible {}
45

5-
static func apollo_performAsyncIfNeeded(on callbackQueue: DispatchQueue?, action: @escaping () -> Void) {
6+
public extension ApolloExtension where Base == DispatchQueue {
7+
8+
static func performAsyncIfNeeded(on callbackQueue: DispatchQueue?, action: @escaping () -> Void) {
69
if let callbackQueue = callbackQueue {
710
// A callback queue was provided, perform the action on that queue
811
callbackQueue.async {
@@ -14,14 +17,14 @@ public extension DispatchQueue {
1417
}
1518
}
1619

17-
static func apollo_returnResultAsyncIfNeeded<T>(on callbackQueue: DispatchQueue?,
18-
action: ((Result<T, Error>) -> Void)?,
19-
result: Result<T, Error>) {
20+
static func returnResultAsyncIfNeeded<T>(on callbackQueue: DispatchQueue?,
21+
action: ((Result<T, Error>) -> Void)?,
22+
result: Result<T, Error>) {
2023
guard let action = action else {
2124
return
2225
}
2326

24-
self.apollo_performAsyncIfNeeded(on: callbackQueue) {
27+
self.performAsyncIfNeeded(on: callbackQueue) {
2528
action(result)
2629
}
2730
}

Sources/Apollo/GraphQLHTTPResponseError.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public struct GraphQLHTTPResponseError: Error, LocalizedError {
5555
return "[Empty response body]"
5656
}
5757

58-
guard let description = String(data: body, encoding: response.textEncoding ?? .utf8) else {
58+
guard let description = String(data: body, encoding: response.apollo.textEncoding ?? .utf8) else {
5959
return "[Unreadable response body]"
6060
}
6161

@@ -69,7 +69,7 @@ public struct GraphQLHTTPResponseError: Error, LocalizedError {
6969

7070
return "\(kind.description): \(description)"
7171
} else {
72-
return "\(kind.description) (\(response.statusCode) \(response.statusCodeDescription)): \(bodyDescription)"
72+
return "\(kind.description) (\(response.statusCode) \(response.apollo.statusCodeDescription)): \(bodyDescription)"
7373
}
7474
}
7575
}

Sources/Apollo/GraphQLSelectionSet.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import ApolloCore
2+
13
public typealias ResultMap = [String: Any?]
24

35
public protocol GraphQLSelectionSet {
@@ -56,7 +58,7 @@ public struct GraphQLField: GraphQLSelection {
5658
func cacheKey(with variables: [String: JSONEncodable]?) throws -> String {
5759
if
5860
let argumentValues = try arguments?.evaluate(with: variables),
59-
argumentValues.isNotEmpty {
61+
argumentValues.apollo.isNotEmpty {
6062
let argumentsKey = orderIndependentKey(for: argumentValues)
6163
return "\(name)(\(argumentsKey))"
6264
} else {

Sources/Apollo/HTTPNetworkTransport.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public class HTTPNetworkTransport {
173173
response: nil,
174174
completionHandler: completionHandler)
175175
case .success(let (data, httpResponse)):
176-
guard httpResponse.isSuccessful == true else {
176+
guard httpResponse.apollo.isSuccessful == true else {
177177
let unsuccessfulError = GraphQLHTTPResponseError(body: data,
178178
response: httpResponse,
179179
kind: .errorResponse)
@@ -226,7 +226,7 @@ public class HTTPNetworkTransport {
226226
guard
227227
let delegate = self.delegate as? HTTPNetworkTransportGraphQLErrorDelegate,
228228
let graphQLErrors = response.parseErrorsOnlyFast(),
229-
graphQLErrors.isNotEmpty else {
229+
graphQLErrors.apollo.isNotEmpty else {
230230
completionHandler(.success(response))
231231
return
232232
}
@@ -391,7 +391,7 @@ public class HTTPNetworkTransport {
391391
do {
392392
if
393393
let files = files,
394-
files.isNotEmpty {
394+
files.apollo.isNotEmpty {
395395
let formData = try requestCreator.requestMultipartFormData(
396396
for: operation,
397397
files: files,

Sources/Apollo/HTTPURLResponse+Helpers.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import Foundation
2+
import ApolloCore
23

3-
extension HTTPURLResponse {
4+
extension HTTPURLResponse: ApolloCompatible {}
5+
6+
extension ApolloExtension where Base == HTTPURLResponse {
47
var isSuccessful: Bool {
5-
return (200..<300).contains(statusCode)
8+
return (200..<300).contains(base.statusCode)
69
}
710

811
var statusCodeDescription: String {
9-
return HTTPURLResponse.localizedString(forStatusCode: statusCode)
12+
return HTTPURLResponse.localizedString(forStatusCode: base.statusCode)
1013
}
1114

1215
var textEncoding: String.Encoding? {
13-
guard let encodingName = textEncodingName else { return nil }
16+
guard let encodingName = base.textEncodingName else { return nil }
1417

1518
return String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding(encodingName as CFString)))
1619
}

Sources/Apollo/InMemoryNormalizedCache.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public final class InMemoryNormalizedCache: NormalizedCache {
1414
self.recordsLock.lock()
1515
let records = keys.map { self.records[$0] }
1616
self.recordsLock.unlock()
17-
DispatchQueue.apollo_returnResultAsyncIfNeeded(on: callbackQueue,
17+
DispatchQueue.apollo.returnResultAsyncIfNeeded(on: callbackQueue,
1818
action: completion,
1919
result: .success(records))
2020
}
@@ -25,7 +25,7 @@ public final class InMemoryNormalizedCache: NormalizedCache {
2525
self.recordsLock.lock()
2626
let cacheKeys = self.records.merge(records: records)
2727
self.recordsLock.unlock()
28-
DispatchQueue.apollo_returnResultAsyncIfNeeded(on: callbackQueue,
28+
DispatchQueue.apollo.returnResultAsyncIfNeeded(on: callbackQueue,
2929
action: completion,
3030
result: .success(cacheKeys))
3131
}
@@ -38,7 +38,7 @@ public final class InMemoryNormalizedCache: NormalizedCache {
3838
return
3939
}
4040

41-
DispatchQueue.apollo_returnResultAsyncIfNeeded(on: callbackQueue,
41+
DispatchQueue.apollo.returnResultAsyncIfNeeded(on: callbackQueue,
4242
action: completion,
4343
result: .success(()))
4444
}

0 commit comments

Comments
 (0)