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.
This commit is contained in:
Clinton Nkwocha authored and GitHub committed 2024-11-26 16:04:41 +00:00
1 parent c2e79ce44c
commit 6e527aa16a
12 files changed
+222

No files matched your search

@@ -72,6 +72,9 @@ extension BareInnerListDecoder: UnkeyedDecodingContainer {
} else if type is Decimal.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Decimal.self) as! T
} else if type is Date.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Date.self) as! T
} else {
return try type.init(from: self.decoder)
}
@@ -133,6 +133,14 @@ extension BareItemDecoder: SingleValueDecodingContainer {
)
}
func decode(_: Date.Type) throws -> Date {
guard case .date(let date) = self.item else {
throw StructuredHeaderError.invalidTypeForItem
}
return Date(timeIntervalSince1970: Double(date))
}
func decodeNil() -> Bool {
// Items are never nil.
false
@@ -172,6 +180,8 @@ extension BareItemDecoder: SingleValueDecodingContainer {
return try self.decode(Data.self) as! T
case is Decimal.Type:
return try self.decode(Decimal.self) as! T
case is Date.Type:
return try self.decode(Date.self) as! T
default:
throw StructuredHeaderError.invalidTypeForItem
}
@@ -55,6 +55,9 @@ extension DictionaryKeyedContainer: KeyedDecodingContainerProtocol {
} else if type is Decimal.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Decimal.self) as! T
} else if type is Date.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Date.self) as! T
} else {
return try type.init(from: self.decoder)
}
@@ -60,6 +60,9 @@ extension KeyedInnerListDecoder: KeyedDecodingContainerProtocol {
} else if type is Decimal.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Decimal.self) as! T
} else if type is Date.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Date.self) as! T
} else {
return try type.init(from: self.decoder)
}
@@ -60,6 +60,9 @@ extension KeyedItemDecoder: KeyedDecodingContainerProtocol {
} else if type is Decimal.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Decimal.self) as! T
} else if type is Date.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Date.self) as! T
} else {
return try type.init(from: self.decoder)
}
@@ -60,6 +60,9 @@ extension KeyedTopLevelListDecoder: KeyedDecodingContainerProtocol {
} else if type is Decimal.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Decimal.self) as! T
} else if type is Date.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Date.self) as! T
} else {
return try type.init(from: self.decoder)
}
@@ -55,6 +55,9 @@ extension ParametersDecoder: KeyedDecodingContainerProtocol {
} else if type is Decimal.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Decimal.self) as! T
} else if type is Date.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Date.self) as! T
} else {
return try type.init(from: self.decoder)
}
@@ -117,6 +117,9 @@ extension StructuredFieldValueDecoder {
} else if type is Decimal.Type {
let container = try decoder.singleValueContainer()
return try container.decode(Decimal.self) as! StructuredField
} else if type is Date.Type {
let container = try decoder.singleValueContainer()
return try container.decode(Date.self) as! StructuredField
} else {
return try type.init(from: decoder)
}
@@ -72,6 +72,9 @@ extension TopLevelListDecoder: UnkeyedDecodingContainer {
} else if type is Decimal.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Decimal.self) as! T
} else if type is Date.Type {
let container = try self.decoder.singleValueContainer()
return try container.decode(Date.self) as! T
} else {
return try type.init(from: self.decoder)
}
@@ -160,6 +160,8 @@ class _StructuredFieldEncoder {
try self.encode(value)
} else if let value = data as? Decimal {
try self.encode(value)
} else if let value = data as? Date {
try self.encode(value)
} else {
try data.encode(to: self)
}
@@ -309,6 +311,11 @@ extension _StructuredFieldEncoder: SingleValueEncodingContainer {
try self.currentStackEntry.storage.insertBareItem(.decimal(pd))
}
func encode(_ data: Date) throws {
let date = Int(data.timeIntervalSince1970)
try self.currentStackEntry.storage.insertBareItem(.date(date))
}
func encode<T>(_ value: T) throws where T: Encodable {
switch value {
case let value as UInt8:
@@ -343,6 +350,8 @@ extension _StructuredFieldEncoder: SingleValueEncodingContainer {
try self.encode(value)
case let value as Decimal:
try self.encode(value)
case let value as Date:
try self.encode(value)
default:
throw StructuredHeaderError.invalidTypeForItem
}
@@ -466,6 +475,11 @@ extension _StructuredFieldEncoder {
try self.currentStackEntry.storage.appendBareItem(.decimal(pd))
}
func append(_ value: Date) throws {
let date = Int(value.timeIntervalSince1970)
try self.currentStackEntry.storage.appendBareItem(.date(date))
}
func append<T>(_ value: T) throws where T: Encodable {
switch value {
case let value as UInt8:
@@ -500,6 +514,8 @@ extension _StructuredFieldEncoder {
try self.append(value)
case let value as Decimal:
try self.append(value)
case let value as Date:
try self.append(value)
default:
// Some other codable type.
switch self.currentStackEntry.storage {
@@ -636,6 +652,12 @@ extension _StructuredFieldEncoder {
try self.currentStackEntry.storage.insertBareItem(.decimal(pd), atKey: key)
}
func encode(_ value: Date, forKey key: String) throws {
let key = self.sanitizeKey(key)
let date = Int(value.timeIntervalSince1970)
try self.currentStackEntry.storage.insertBareItem(.date(date), atKey: key)
}
func encode<T>(_ value: T, forKey key: String) throws where T: Encodable {
let key = self.sanitizeKey(key)
@@ -672,6 +694,8 @@ extension _StructuredFieldEncoder {
try self.encode(value, forKey: key)
case let value as Decimal:
try self.encode(value, forKey: key)
case let value as Date:
try self.encode(value, forKey: key)
default:
// Ok, we don't know what this is. This can only happen for a dictionary, or
// for anything with parameters, or for lists, or for inner lists.