Skip to content

Commit

Permalink
Support decoding [Date]
Browse files Browse the repository at this point in the history
  • Loading branch information
jegnux committed Mar 3, 2021
1 parent ed2854f commit 674f409
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
8 changes: 8 additions & 0 deletions Sources/BackedCodable/Backed+Init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ extension Backed {
public convenience init(_ path: Path? = nil, defaultValue: Value = nil, options: BackingDecoderOptions = [], strategy: DateDecodingStrategy) where Value == Date? {
self.init(path, defaultValue: defaultValue, options: options, decoder: .date(strategy: strategy))
}

public convenience init(_ path: Path? = nil, defaultValue: Value? = nil, options: BackingDecoderOptions = [], strategy: DateDecodingStrategy) where Value == [Date] {
self.init(path, defaultValue: defaultValue, options: options, decoder: .dates(strategy: strategy))
}

public convenience init(_ path: Path? = nil, defaultValue: Value = nil, options: BackingDecoderOptions = [], strategy: DateDecodingStrategy) where Value == [Date]? {
self.init(path, defaultValue: defaultValue, options: options, decoder: .dates(strategy: strategy))
}
}
35 changes: 26 additions & 9 deletions Sources/BackedCodable/Decoders/DateDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,35 @@ public struct DateDecodingStrategy {
}
}

enum DateDecoder {
static func decode(from decoder: Decoder, context: BackingDecoderContext, strategy: DateDecodingStrategy) throws -> Date {
try decoder.decode(at: context.path, options: context.options) { decoder in
try strategy.decode(from: decoder)
}
}
}

extension BackingDecoder {
static func date(strategy: DateDecodingStrategy) -> BackingDecoder<Date> {
BackingDecoder<Date> { (decoder, context) -> Date in
try DateDecoder.decode(from: decoder, context: context, strategy: strategy)
try decoder.decode(at: context.path, options: context.options) { decoder in
try strategy.decode(from: decoder)
}
}
}

static func dates(strategy: DateDecodingStrategy) -> BackingDecoder<[Date]> {
BackingDecoder<[Date]> { (decoder, context) -> [Date] in
try decoder.decode(at: context.path, options: context.options) { pathDecoder in
let container = try pathDecoder.unkeyedContainer()
var dates: [Date] = []
for i in 0 ..< (container.count ?? 0) {
do {
dates.append(
try decoder.decode(at: context.path[i], options: context.options) { pathDecoder in
try strategy.decode(from: pathDecoder)
}
)
} catch {
if context.options.contains(.lossy) == false {
throw error
}
}
}
return dates
}
}
}
}
5 changes: 5 additions & 0 deletions Stubs/BackedStub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public struct BackedStub: BackedDecodable, Equatable {
name: String,
startDate: Date,
endDate: Date,
dates: [Date],
values: [String],
nestedValues: [String],
nestedInteger: Int,
Expand All @@ -29,6 +30,7 @@ public struct BackedStub: BackedDecodable, Equatable {
self._name = Backed(name)
self._startDate = Backed(startDate)
self._endDate = Backed(endDate)
self._dates = Backed(dates)
self._values = Backed(values)
self._nestedValues = Backed(nestedValues)
self._nestedInteger = Backed(nestedInteger)
Expand Down Expand Up @@ -63,6 +65,9 @@ public struct BackedStub: BackedDecodable, Equatable {
@Backed(Path.end_date, strategy: .secondsSince1970)
public var endDate: Date

@Backed(Path.end_date, options: .lossy, strategy: .secondsSince1970)
public var dates: [Date]

@Backed(Path.values, defaultValue: [], options: .lossy)
public var values: [String]

Expand Down
1 change: 1 addition & 0 deletions Stubs/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public var jsonStub: Data {
"name": "Steve",
"start_date": 1613984296000,
"end_date": 1613984996,
"dates": [1613984296, "N/A", 1613984996],
"values": [12, "34", 56, "78"],
"attributes": {
"values": ["12", 34, "56", 78],
Expand Down
4 changes: 4 additions & 0 deletions Tests/BackedCodableTests/BackedCodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ final class BackedCodableTests: XCTestCase {
name: "Steve",
startDate: Date(timeIntervalSince1970: 1_613_984_296),
endDate: Date(timeIntervalSince1970: 1_613_984_996),
dates: [
Date(timeIntervalSince1970: 1_613_984_296),
Date(timeIntervalSince1970: 1_613_984_996),
],
values: ["34", "78"],
nestedValues: ["12", "56"],
nestedInteger: 34,
Expand Down

0 comments on commit 674f409

Please sign in to comment.