-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
QueryItemValue.swift
143 lines (101 loc) · 3.2 KB
/
QueryItemValue.swift
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
public protocol QueryItemValue {
var serializedQueryItem: String? { get }
var isCollection: Bool { get }
func iterateCollection(_ iterator: (QueryItemValue) -> Void)
}
// MARK: - Array + QueryItemValue
extension Array: QueryItemValue where Element: QueryItemValue {
public var serializedQueryItem: String? {
fatalError("This method should not be called. Multiple query items should be added to the query individually")
}
public var isCollection: Bool {
return true
}
public func iterateCollection(_ iterator: (QueryItemValue) -> Void) {
forEach(iterator)
}
}
// MARK: - Bool + QueryItemValue
extension Bool: QueryItemValue {
public var serializedQueryItem: String? {
self ? "true" : "false"
}
public var isCollection: Bool { false }
public func iterateCollection(_ iterator: (QueryItemValue) -> Void) {
fatalError("Not supported")
}
}
// MARK: - Double + QueryItemValue
extension Double: QueryItemValue {
public var serializedQueryItem: String? {
description
}
public var isCollection: Bool { false }
public func iterateCollection(_ iterator: (QueryItemValue) -> Void) {
fatalError("Not supported")
}
}
// MARK: - Int + QueryItemValue
extension Int: QueryItemValue {
public var serializedQueryItem: String? {
description
}
public var isCollection: Bool { false }
public func iterateCollection(_ iterator: (QueryItemValue) -> Void) {
fatalError("Not supported")
}
}
// MARK: - String + QueryItemValue
extension String: QueryItemValue {
public var serializedQueryItem: String? {
self
}
public var isCollection: Bool { false }
public func iterateCollection(_ iterator: (QueryItemValue) -> Void) {
fatalError("Not supported")
}
}
// MARK: - Optional + QueryItemValue
extension Optional: QueryItemValue where Wrapped: QueryItemValue {
public var serializedQueryItem: String? {
guard let value = self else {
return nil
}
return value.serializedQueryItem
}
public var isCollection: Bool {
guard let value = self else {
return false
}
return value.isCollection
}
public func iterateCollection(_ iterator: (QueryItemValue) -> Void) {
guard let value = self else {
return
}
return value.iterateCollection(iterator)
}
}
// MARK: - Set + QueryItemValue
extension Set: QueryItemValue where Element: QueryItemValue {
public var serializedQueryItem: String? {
fatalError("This method should not be called. Multiple query items should be added to the query individually")
}
public var isCollection: Bool {
return true
}
public func iterateCollection(_ iterator: (QueryItemValue) -> Void) {
forEach(iterator)
}
}
public extension QueryItemValue where Self: RawRepresentable, RawValue: QueryItemValue {
var serializedQueryItem: String? {
rawValue.serializedQueryItem
}
var isCollection: Bool {
rawValue.isCollection
}
func iterateCollection(_ iterator: (QueryItemValue) -> Void) {
rawValue.iterateCollection(iterator)
}
}