forked from apollographql/apollo-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSON.swift
More file actions
60 lines (49 loc) · 1.45 KB
/
JSON.swift
File metadata and controls
60 lines (49 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import Foundation
public typealias JSONValue = Any
public typealias JSONObject = [String: JSONValue]
public protocol JSONDecodable {
init(jsonValue value: JSONValue) throws
}
public protocol JSONEncodable: GraphQLInputValue {
var jsonValue: JSONValue { get }
}
public enum JSONDecodingError: Error, LocalizedError {
case missingValue
case nullValue
case wrongType
case couldNotConvert(value: Any, to: Any.Type)
public var errorDescription: String? {
switch self {
case .missingValue:
return "Missing value"
case .nullValue:
return "Unexpected null value"
case .wrongType:
return "Wrong type"
case .couldNotConvert(let value, let expectedType):
return "Could not convert \"\(value)\" to \(expectedType)"
}
}
}
extension JSONDecodingError: Matchable {
public typealias Base = Error
public static func ~=(pattern: JSONDecodingError, value: Error) -> Bool {
guard let value = value as? JSONDecodingError else {
return false
}
switch (value, pattern) {
case (.missingValue, .missingValue), (.nullValue, .nullValue), (.wrongType, .wrongType), (.couldNotConvert, .couldNotConvert):
return true
default:
return false
}
}
}
// MARK: Helpers
func equals(_ lhs: Any, _ rhs: Any) -> Bool {
if let lhs = lhs as? Reference, let rhs = rhs as? Reference {
return lhs == rhs
}
let lhs = lhs as AnyObject, rhs = rhs as AnyObject
return lhs.isEqual(rhs)
}