-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathLossyArray.swift
More file actions
61 lines (52 loc) · 1.79 KB
/
LossyArray.swift
File metadata and controls
61 lines (52 loc) · 1.79 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
61
/// Decodes Arrays filtering invalid values if applicable
///
/// `@LossyArray` decodes Arrays and filters invalid values if the Decoder is unable to decode the value.
///
/// This is useful if the Array is intended to contain non-optional types.
@propertyWrapper
public struct LossyArray<T> {
public var wrappedValue: [T]
public var projectedValue: Self { self }
public init(wrappedValue: [T]) {
self.wrappedValue = wrappedValue
}
public struct FailedDecode {
var codingPath: [CodingKey]
var error: Error
}
public var failedDecodes: [FailedDecode] = []
}
extension LossyArray: Decodable where T: Decodable {
private struct AnyDecodableValue: Decodable {}
public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
var elements: [T] = []
var failedDecodes: [FailedDecode] = []
while !container.isAtEnd {
do {
let value = try container.decode(T.self)
elements.append(value)
} catch {
failedDecodes.append(FailedDecode(codingPath: container.codingPath, error: error))
_ = try? container.decode(AnyDecodableValue.self)
}
}
self.wrappedValue = elements
self.failedDecodes = failedDecodes
}
}
extension LossyArray: Encodable where T: Encodable {
public func encode(to encoder: Encoder) throws {
try wrappedValue.encode(to: encoder)
}
}
extension LossyArray: Equatable where T: Equatable {
public static func == (lhs: LossyArray<T>, rhs: LossyArray<T>) -> Bool {
lhs.wrappedValue == rhs.wrappedValue
}
}
extension LossyArray: Hashable where T: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(wrappedValue)
}
}