Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failedDecodes to LossyArray #56

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions Sources/BetterCodable/LossyArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
@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 {
Expand All @@ -19,16 +27,19 @@ extension LossyArray: Decodable where T: Decodable {
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
}
}

Expand All @@ -38,5 +49,13 @@ extension LossyArray: Encodable where T: Encodable {
}
}

extension LossyArray: Equatable where T: Equatable { }
extension LossyArray: Hashable where T: Hashable { }
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)
}
}
4 changes: 4 additions & 0 deletions Tests/BetterCodableTests/LossyArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ class LossyArrayTests: XCTestCase {
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
XCTAssertEqual(fixture.values, [1, 3, 4])
XCTAssertEqual(fixture.nonPrimitiveValues, [])
XCTAssertEqual(fixture.$values.failedDecodes.count, 1)
XCTAssertEqual(fixture.$nonPrimitiveValues.failedDecodes.count, 1)
}

func testDecodingLossyArrayIgnoresLossyElements() throws {
let jsonData = #"{ "values": [1, null, "3", false, 4], "nonPrimitiveValues": [null] }"#.data(using: .utf8)!
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
XCTAssertEqual(fixture.values, [1, 4])
XCTAssertEqual(fixture.nonPrimitiveValues, [])
XCTAssertEqual(fixture.$values.failedDecodes.count, 3)
XCTAssertEqual(fixture.$nonPrimitiveValues.failedDecodes.count, 1)
}

func testEncodingDecodedLossyArrayIgnoresFailableElements() throws {
Expand Down