Skip to content

Commit 6e527aa

Browse files
authored
Add support for Date type to StructuredFieldValues (#49)
Motivation: The parser and serializer for the Date type has been implemented. The high-level encoder and decoder should follow. Modifications: - Implement the encoder and decoder for Date. Result: The StructuredFieldValues module will support the Date type.
1 parent c2e79ce commit 6e527aa

12 files changed

+222
-0
lines changed

Sources/StructuredFieldValues/Decoder/BareInnerListDecoder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ extension BareInnerListDecoder: UnkeyedDecodingContainer {
7272
} else if type is Decimal.Type {
7373
let container = try self.decoder.singleValueContainer()
7474
return try container.decode(Decimal.self) as! T
75+
} else if type is Date.Type {
76+
let container = try self.decoder.singleValueContainer()
77+
return try container.decode(Date.self) as! T
7578
} else {
7679
return try type.init(from: self.decoder)
7780
}

Sources/StructuredFieldValues/Decoder/BareItemDecoder.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ extension BareItemDecoder: SingleValueDecodingContainer {
133133
)
134134
}
135135

136+
func decode(_: Date.Type) throws -> Date {
137+
guard case .date(let date) = self.item else {
138+
throw StructuredHeaderError.invalidTypeForItem
139+
}
140+
141+
return Date(timeIntervalSince1970: Double(date))
142+
}
143+
136144
func decodeNil() -> Bool {
137145
// Items are never nil.
138146
false
@@ -172,6 +180,8 @@ extension BareItemDecoder: SingleValueDecodingContainer {
172180
return try self.decode(Data.self) as! T
173181
case is Decimal.Type:
174182
return try self.decode(Decimal.self) as! T
183+
case is Date.Type:
184+
return try self.decode(Date.self) as! T
175185
default:
176186
throw StructuredHeaderError.invalidTypeForItem
177187
}

Sources/StructuredFieldValues/Decoder/DictionaryKeyedContainer.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ extension DictionaryKeyedContainer: KeyedDecodingContainerProtocol {
5555
} else if type is Decimal.Type {
5656
let container = try self.decoder.singleValueContainer()
5757
return try container.decode(Decimal.self) as! T
58+
} else if type is Date.Type {
59+
let container = try self.decoder.singleValueContainer()
60+
return try container.decode(Date.self) as! T
5861
} else {
5962
return try type.init(from: self.decoder)
6063
}

Sources/StructuredFieldValues/Decoder/KeyedInnerListDecoder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ extension KeyedInnerListDecoder: KeyedDecodingContainerProtocol {
6060
} else if type is Decimal.Type {
6161
let container = try self.decoder.singleValueContainer()
6262
return try container.decode(Decimal.self) as! T
63+
} else if type is Date.Type {
64+
let container = try self.decoder.singleValueContainer()
65+
return try container.decode(Date.self) as! T
6366
} else {
6467
return try type.init(from: self.decoder)
6568
}

Sources/StructuredFieldValues/Decoder/KeyedItemDecoder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ extension KeyedItemDecoder: KeyedDecodingContainerProtocol {
6060
} else if type is Decimal.Type {
6161
let container = try self.decoder.singleValueContainer()
6262
return try container.decode(Decimal.self) as! T
63+
} else if type is Date.Type {
64+
let container = try self.decoder.singleValueContainer()
65+
return try container.decode(Date.self) as! T
6366
} else {
6467
return try type.init(from: self.decoder)
6568
}

Sources/StructuredFieldValues/Decoder/KeyedTopLevelListDecoder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ extension KeyedTopLevelListDecoder: KeyedDecodingContainerProtocol {
6060
} else if type is Decimal.Type {
6161
let container = try self.decoder.singleValueContainer()
6262
return try container.decode(Decimal.self) as! T
63+
} else if type is Date.Type {
64+
let container = try self.decoder.singleValueContainer()
65+
return try container.decode(Date.self) as! T
6366
} else {
6467
return try type.init(from: self.decoder)
6568
}

Sources/StructuredFieldValues/Decoder/ParametersDecoder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ extension ParametersDecoder: KeyedDecodingContainerProtocol {
5555
} else if type is Decimal.Type {
5656
let container = try self.decoder.singleValueContainer()
5757
return try container.decode(Decimal.self) as! T
58+
} else if type is Date.Type {
59+
let container = try self.decoder.singleValueContainer()
60+
return try container.decode(Date.self) as! T
5861
} else {
5962
return try type.init(from: self.decoder)
6063
}

Sources/StructuredFieldValues/Decoder/StructuredFieldValueDecoder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ extension StructuredFieldValueDecoder {
117117
} else if type is Decimal.Type {
118118
let container = try decoder.singleValueContainer()
119119
return try container.decode(Decimal.self) as! StructuredField
120+
} else if type is Date.Type {
121+
let container = try decoder.singleValueContainer()
122+
return try container.decode(Date.self) as! StructuredField
120123
} else {
121124
return try type.init(from: decoder)
122125
}

Sources/StructuredFieldValues/Decoder/TopLevelListDecoder.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ extension TopLevelListDecoder: UnkeyedDecodingContainer {
7272
} else if type is Decimal.Type {
7373
let container = try self.decoder.singleValueContainer()
7474
return try container.decode(Decimal.self) as! T
75+
} else if type is Date.Type {
76+
let container = try self.decoder.singleValueContainer()
77+
return try container.decode(Date.self) as! T
7578
} else {
7679
return try type.init(from: self.decoder)
7780
}

Sources/StructuredFieldValues/Encoder/StructuredFieldValueEncoder.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ class _StructuredFieldEncoder {
160160
try self.encode(value)
161161
} else if let value = data as? Decimal {
162162
try self.encode(value)
163+
} else if let value = data as? Date {
164+
try self.encode(value)
163165
} else {
164166
try data.encode(to: self)
165167
}
@@ -309,6 +311,11 @@ extension _StructuredFieldEncoder: SingleValueEncodingContainer {
309311
try self.currentStackEntry.storage.insertBareItem(.decimal(pd))
310312
}
311313

314+
func encode(_ data: Date) throws {
315+
let date = Int(data.timeIntervalSince1970)
316+
try self.currentStackEntry.storage.insertBareItem(.date(date))
317+
}
318+
312319
func encode<T>(_ value: T) throws where T: Encodable {
313320
switch value {
314321
case let value as UInt8:
@@ -343,6 +350,8 @@ extension _StructuredFieldEncoder: SingleValueEncodingContainer {
343350
try self.encode(value)
344351
case let value as Decimal:
345352
try self.encode(value)
353+
case let value as Date:
354+
try self.encode(value)
346355
default:
347356
throw StructuredHeaderError.invalidTypeForItem
348357
}
@@ -466,6 +475,11 @@ extension _StructuredFieldEncoder {
466475
try self.currentStackEntry.storage.appendBareItem(.decimal(pd))
467476
}
468477

478+
func append(_ value: Date) throws {
479+
let date = Int(value.timeIntervalSince1970)
480+
try self.currentStackEntry.storage.appendBareItem(.date(date))
481+
}
482+
469483
func append<T>(_ value: T) throws where T: Encodable {
470484
switch value {
471485
case let value as UInt8:
@@ -500,6 +514,8 @@ extension _StructuredFieldEncoder {
500514
try self.append(value)
501515
case let value as Decimal:
502516
try self.append(value)
517+
case let value as Date:
518+
try self.append(value)
503519
default:
504520
// Some other codable type.
505521
switch self.currentStackEntry.storage {
@@ -636,6 +652,12 @@ extension _StructuredFieldEncoder {
636652
try self.currentStackEntry.storage.insertBareItem(.decimal(pd), atKey: key)
637653
}
638654

655+
func encode(_ value: Date, forKey key: String) throws {
656+
let key = self.sanitizeKey(key)
657+
let date = Int(value.timeIntervalSince1970)
658+
try self.currentStackEntry.storage.insertBareItem(.date(date), atKey: key)
659+
}
660+
639661
func encode<T>(_ value: T, forKey key: String) throws where T: Encodable {
640662
let key = self.sanitizeKey(key)
641663

@@ -672,6 +694,8 @@ extension _StructuredFieldEncoder {
672694
try self.encode(value, forKey: key)
673695
case let value as Decimal:
674696
try self.encode(value, forKey: key)
697+
case let value as Date:
698+
try self.encode(value, forKey: key)
675699
default:
676700
// Ok, we don't know what this is. This can only happen for a dictionary, or
677701
// for anything with parameters, or for lists, or for inner lists.

0 commit comments

Comments
 (0)