Date was not properly coded

Also, a new date property is implement for ExplorerValue
This commit is contained in:
Alexis Bridoux 2021-04-29 19:01:06 +02:00
parent fa58a0044f
commit 0842f99def
7 changed files with 65 additions and 0 deletions

View File

@ -282,6 +282,11 @@ extension ExplorerValue {
return data
}
public var date: Date? {
guard case let .date(date) = self else { return nil }
return date
}
public var array: ArrayValue? {
switch self {
case .array(let array): return array

View File

@ -126,6 +126,12 @@ extension ExplorerValueDecoder {
.unwrapOrThrow(.typeMismatch(Data.self, codingPath: codingPath + [key]))
}
func decode(_ type: Date.Type, forKey key: Key) throws -> Date {
try valueFor(key: key)
.date
.unwrapOrThrow(.typeMismatch(Date.self, codingPath: codingPath + [key]))
}
func decode<T>(_ type: T.Type, forKey key: Key) throws -> T where T: Decodable {
let value = try valueFor(key: key)
@ -133,6 +139,10 @@ extension ExplorerValueDecoder {
return try decode(Data.self, forKey: key) as! T
}
if T.self == Date.self {
return try decode(Date.self, forKey: key) as! T
}
let decoder = ExplorerValueDecoder(value, codingPath: codingPath + [key])
return try T(from: decoder)
}

View File

@ -74,11 +74,23 @@ extension ExplorerValueDecoder {
try value.data.unwrapOrThrow(.typeMismatch(Data.self, codingPath: codingPath))
}
func decode(_ type: Date.Type) throws -> Date {
try value.date.unwrapOrThrow(.typeMismatch(Date.self, codingPath: codingPath))
}
func decode<T>(_ type: T.Type) throws -> T where T: Decodable {
if T.self == Data.self {
return try decode(Data.self) as! T
}
if T.self == Date.self {
return try decode(Date.self) as! T
}
if T.self == Date.self {
return try decode(Date.self) as! T
}
let decoder = ExplorerValueDecoder(value, codingPath: codingPath)
return try T(from: decoder)
}

View File

@ -107,11 +107,21 @@ extension ExplorerValueDecoder {
return value
}
mutating func decode(_ type: Date.Type) throws -> Date {
let value = try array[currentIndex].date.unwrapOrThrow(.typeMismatch(Date.self, codingPath: codingPath))
currentIndex += 1
return value
}
mutating func decode<T>(_ type: T.Type) throws -> T where T: Decodable {
if T.self == Data.self {
return try decode(Data.self) as! T
}
if T.self == Date.self {
return try decode(Date.self) as! T
}
let decoder = ExplorerValueDecoder(array[currentIndex], codingPath: codingPath)
let decoded = try T(from: decoder)
currentIndex += 1

View File

@ -74,12 +74,21 @@ extension ExplorerValueEncoder {
try encoder.value.add(.data(value), at: path.appending(key.stringValue))
}
mutating func encode(_ value: Date, forKey key: Key) throws {
try encoder.value.add(.date(value), at: path.appending(key.stringValue))
}
mutating func encode<T>(_ value: T, forKey key: Key) throws where T: Encodable {
if let data = value as? Data {
try encode(data, forKey: key)
return
}
if let data = value as? Date {
try encode(data, forKey: key)
return
}
let newEncoder = ExplorerValueEncoder()
try value.encode(to: newEncoder)
try encoder.value.add(newEncoder.value, at: path.appending(key.stringValue))

View File

@ -74,12 +74,21 @@ extension ExplorerValueEncoder {
try encoder.value.set(path, to: .data(value))
}
mutating func encode(_ value: Date) throws {
try encoder.value.set(path, to: .date(value))
}
mutating func encode<T>(_ value: T) throws where T: Encodable {
if let data = value as? Data {
try encode(data)
return
}
if let date = value as? Date {
try encode(date)
return
}
let newEncoder = ExplorerValueEncoder(codingPath: codingPath)
try value.encode(to: encoder)
try encoder.value.set(path, to: newEncoder.value)

View File

@ -91,12 +91,22 @@ extension ExplorerValueEncoder {
count += 1
}
mutating func encode(_ value: Date) throws {
try encoder.value.add(.date(value), at: path.appending(.count))
count += 1
}
mutating func encode<T>(_ value: T) throws where T: Encodable {
if let data = value as? Data {
try encode(data)
return
}
if let date = value as? Date {
try encode(date)
return
}
let newEncoder = ExplorerValueEncoder(codingPath: codingPath)
try value.encode(to: newEncoder)
try encoder.value.add(newEncoder.value, at: path.appending(.count))