-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathField.swift
108 lines (100 loc) · 3.23 KB
/
Field.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
/// Parses a named field's value with a string parser.
///
/// Useful for incrementally parsing values from various request fields, including ``Query``
/// parameters, ``Headers`` and ``Cookies``, and ``FormData``.
///
/// For example, a search endpoint may include a few query items, which can be specified as fields:
///
/// ```swift
/// Query {
/// Field("q", .string, default: "")
/// Field("page", default: 1) {
/// Digits()
/// }
/// Field("per_page", default: 20) {
/// Digits()
/// }
/// }
/// ```
public struct Field<Value: Parser>: Parser where Value.Input == Substring {
@usableFromInline
let defaultValue: Value.Output?
@usableFromInline
let name: String
@usableFromInline
let valueParser: Value
/// Initializes a named field parser.
///
/// - Parameters:
/// - name: The name of the field.
/// - defaultValue: A default value if the field is absent. Prefer specifying a default over
/// applying `Parser.replaceError(with:)` if parsing should fail for invalid values.
/// - value: A parser that parses the field's substring value into something more
/// well-structured.
@inlinable
public init(
_ name: String,
default defaultValue: Value.Output? = nil,
@ParserBuilder _ value: () -> Value
) {
self.defaultValue = defaultValue
self.name = name
self.valueParser = value()
}
/// Initializes a named field parser.
///
/// - Parameters:
/// - name: The name of the field.
/// - value: A conversion that transforms the field's substring value into something more
/// well-structured.
/// - defaultValue: A default value if the field is absent. Prefer specifying a default over
/// applying `Parser.replaceError(with:)` if parsing should fail for invalid values.
@inlinable
public init<C>(
_ name: String,
_ value: C,
default defaultValue: Value.Output? = nil
) where Value == Parsers.MapConversion<Parsers.ReplaceError<Rest<Substring>>, C> {
self.defaultValue = defaultValue
self.name = name
self.valueParser = Rest().replaceError(with: "").map(value)
}
@inlinable
public init(
_ name: String,
default defaultValue: Value.Output? = nil
)
where
Value == Parsers.MapConversion<
Parsers.ReplaceError<Rest<Substring>>, Conversions.SubstringToString
>
{
self.defaultValue = defaultValue
self.name = name
self.valueParser = Rest().replaceError(with: "").map(.string)
}
@inlinable
public func parse(_ input: inout URLRequestData.Fields) throws -> Value.Output {
guard
let wrapped = input[self.name]?.first,
var value = wrapped
else {
guard let defaultValue = self.defaultValue
else { throw RoutingError() }
return defaultValue
}
let output = try self.valueParser.parse(&value)
input[self.name]?.removeFirst()
if input[self.name]?.isEmpty ?? true {
input[self.name] = nil
}
return output
}
}
extension Field: ParserPrinter where Value: ParserPrinter {
@inlinable
public func print(_ output: Value.Output, into input: inout URLRequestData.Fields) rethrows {
if let defaultValue = self.defaultValue, isEqual(output, defaultValue) { return }
input[self.name, default: []].prepend(try valueParser.print(output))
}
}