-
Notifications
You must be signed in to change notification settings - Fork 34
/
DeepLinking.swift
272 lines (226 loc) · 10.5 KB
/
DeepLinking.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
//
// DeepLinking.swift
// Created by Joshua Smith on 5/22/17.
// Copyright (c) 2017 iJoshSmith. Licensed under the MIT License.
//
import Foundation
// MARK: - DeepLink
/// Adopted by a type whose values are matched and extracted from a URL.
public protocol DeepLink {
/// Returns a template that describes how to match and extract values from a URL.
static var template: DeepLinkTemplate { get }
/// Initializes a new instance with values extracted from a URL.
/// - Parameter values: Data values from a URL, whose keys are the names specified in a `DeepLinkTemplate`.
init(values: DeepLinkValues)
}
// MARK: - DeepLinkValues
/// Data values extracted from a URL by a deep link template.
public struct DeepLinkValues {
/// Values in the URL's path, whose keys are the names specified in a deep link template.
public let path: [String: Any]
/// Values in the URL's query string, whose keys are the names specified in a deep link template.
public let query: [String: Any]
/// The URL's fragment (i.e. text following a # symbol), if available.
public let fragment: String?
fileprivate init(path: [String: Any], query: [String: Any], fragment: String?) {
self.path = path
self.query = query
self.fragment = fragment
}
}
// MARK: - DeepLinkTemplate
/// Describes how to extract a deep link's values from a URL.
/// A template is considered to match a URL if all of its required values are found in the URL.
public struct DeepLinkTemplate {
// MARK: - Public API
public init() {
self.init(pathParts: [], parameters: [])
}
/// A matching URL must include this constant string at the correct location in its path.
public func term(_ symbol: String) -> DeepLinkTemplate {
return appending(pathPart: .term(symbol: symbol))
}
/// A matching URL must include a string at the correct location in its path.
/// - Parameter name: The key of this string in the `path` dictionary of `DeepLinkValues`.
public func string(named name: String) -> DeepLinkTemplate {
return appending(pathPart: .string(name: name))
}
/// A matching URL must include an integer at the correct location in its path.
/// - Parameter name: The key of this integer in the `path` dictionary of `DeepLinkValues`.
public func int(named name: String) -> DeepLinkTemplate {
return appending(pathPart: .int(name: name))
}
/// A matching URL must include a double at the correct location in its path.
/// - Parameter name: The key of this double in the `path` dictionary of `DeepLinkValues`.
public func double(named name: String) -> DeepLinkTemplate {
return appending(pathPart: .double(name: name))
}
/// A matching URL must include a boolean at the correct location in its path.
/// - Parameter name: The key of this boolean in the `path` dictionary of `DeepLinkValues`.
public func bool(named name: String) -> DeepLinkTemplate {
return appending(pathPart: .bool(name: name))
}
/// An unordered set of query string parameters.
/// - Parameter queryStringParameters: A set of parameters that may be required or optional.
public func queryStringParameters(_ queryStringParameters: Set<QueryStringParameter>) -> DeepLinkTemplate {
return DeepLinkTemplate(pathParts: pathParts, parameters: queryStringParameters)
}
/// A named value in a URL's query string.
public enum QueryStringParameter {
case requiredInt(named: String), optionalInt(named: String)
case requiredBool(named: String), optionalBool(named: String)
case requiredDouble(named: String), optionalDouble(named: String)
case requiredString(named: String), optionalString(named: String)
}
// MARK: - Private creation methods
private init(pathParts: [PathPart], parameters: Set<QueryStringParameter>) {
self.pathParts = pathParts
self.parameters = parameters
}
private func appending(pathPart: PathPart) -> DeepLinkTemplate {
return DeepLinkTemplate(pathParts: pathParts + [pathPart], parameters: parameters)
}
// MARK: - State
fileprivate enum PathPart {
case int(name: String)
case bool(name: String)
case string(name: String)
case double(name: String)
case term(symbol: String)
}
fileprivate let pathParts: [PathPart]
fileprivate let parameters: Set<QueryStringParameter>
}
// MARK: - DeepLinkRecognizer
/// Creates a deep link object that matches a URL.
public struct DeepLinkRecognizer {
private let deepLinkTypes: [DeepLink.Type]
/// Initializes a new recognizer with a list of available deep link types.
/// - Parameter deepLinkTypes: An array of deep link types which can be created based on a URL.
/// The template of each type is evaluated in the order the types appear in this array.
public init(deepLinkTypes: [DeepLink.Type]) {
self.deepLinkTypes = deepLinkTypes
}
/// Returns a new `DeepLink` object whose template matches the specified URL, if possible.
public func deepLink(matching url: URL) -> DeepLink? {
for deepLinkType in deepLinkTypes {
if let values = DeepLinkRecognizer.extractValues(in: deepLinkType.template, from: url) {
return deepLinkType.init(values: values)
}
}
return nil
}
// MARK: - URL value extraction
private static func extractValues(in template: DeepLinkTemplate, from url: URL) -> DeepLinkValues? {
guard let pathValues = extractPathValues(in: template, from: url) else { return nil }
guard let queryValues = extractQueryValues(in: template, from: url) else { return nil }
return DeepLinkValues(path: pathValues, query: queryValues, fragment: url.fragment)
}
private static func extractPathValues(in template: DeepLinkTemplate, from url: URL) -> [String: Any]? {
let allComponents = url.host.map { [$0] + url.pathComponents } ?? url.pathComponents
let components = allComponents
.filter { $0 != "/" }
.map { $0.removingPercentEncoding ?? "" }
guard components.count == template.pathParts.count else { return nil }
var values = [String: Any]()
for (pathPart, component) in zip(template.pathParts, components) {
switch pathPart {
case let .int(name):
guard let value = Int(component) else { return nil }
values[name] = value
case let .bool(name):
guard let value = Bool(component) else { return nil }
values[name] = value
case let .double(name):
guard let value = Double(component) else { return nil }
values[name] = value
case let .string(name):
values[name] = component
case let .term(symbol):
guard symbol == component else { return nil }
}
}
return values
}
private static func extractQueryValues(in template: DeepLinkTemplate, from url: URL) -> [String: Any]? {
if template.parameters.isEmpty {
return url.query == nil ? [:] : nil
}
let requiredParameters = template.parameters.filter { $0.isRequired }
let optionalParameters = template.parameters.subtracting(requiredParameters)
guard let query = url.query else {
return requiredParameters.isEmpty ? [:] : nil
}
let queryMap = createMap(of: query)
var values = [String: Any]()
for parameter in requiredParameters {
guard let value = value(of: parameter, in: queryMap) else { return nil }
values[parameter.name] = value
}
for parameter in optionalParameters {
if let value = value(of: parameter, in: queryMap) {
values[parameter.name] = value
}
}
return values
}
private typealias QueryMap = [String: String]
private static func createMap(of query: String) -> QueryMap {
// Transforms "a=b&c=d" to [(a, b), (c, d)]
let keyValuePairs = query
.components(separatedBy: "&")
.map { $0.components(separatedBy: "=") }
.filter { $0.count == 2 }
.map { ($0[0], $0[1]) }
var queryMap = QueryMap()
for (key, value) in keyValuePairs {
queryMap[key] = value
}
return queryMap
}
private static func value(of parameter: DeepLinkTemplate.QueryStringParameter, in queryMap: QueryMap) -> Any? {
guard let value: String = queryMap[parameter.name] else { return nil }
switch parameter.type {
case .int: return Int(value)
case .bool: return Bool(value)
case .double: return Double(value)
case .string: return value.removingPercentEncoding ?? ""
}
}
}
// MARK: - QueryStringParameter extension
extension DeepLinkTemplate.QueryStringParameter: Hashable, Equatable {
public var hashValue: Int {
return name.hashValue
}
public static func ==(lhs: DeepLinkTemplate.QueryStringParameter, rhs: DeepLinkTemplate.QueryStringParameter) -> Bool {
return lhs.name == rhs.name
}
fileprivate var name: String {
switch self {
case let .requiredInt(name): return name
case let .requiredBool(name): return name
case let .requiredDouble(name): return name
case let .requiredString(name): return name
case let .optionalInt(name): return name
case let .optionalBool(name): return name
case let .optionalDouble(name): return name
case let .optionalString(name): return name
}
}
fileprivate enum ParameterType { case string, int, double, bool }
fileprivate var type: ParameterType {
switch self {
case .requiredInt, .optionalInt: return .int
case .requiredBool, .optionalBool: return .bool
case .requiredDouble, .optionalDouble: return .double
case .requiredString, .optionalString: return .string
}
}
fileprivate var isRequired: Bool {
switch self {
case .requiredInt, .requiredBool, .requiredDouble, .requiredString: return true
case .optionalInt, .optionalBool, .optionalDouble, .optionalString: return false
}
}
}