Skip to content

Commit

Permalink
Date Encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Jul 28, 2023
1 parent 08a6ad5 commit 11d3a41
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Sources/KeyValueEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,12 @@ extension KeyValueEncoder {

extension KeyValueEncoder.NilEncodingStrategy {

func isNull(_ value: Any?) -> Bool {
public func isNull(_ value: Any?) -> Bool {
guard let value else { return true }
return isNull(value)
}

func isNull(_ value: Any) -> Bool {
public func isNull(_ value: Any) -> Bool {
switch self {
case .removed:
return Self.isOptionalNone(value)
Expand Down Expand Up @@ -187,8 +187,11 @@ private extension KeyValueEncoder {
}

func encodeToValue<T>(_ value: T) throws -> EncodedValue where T: Encodable {
try value.encode(to: self)
return try getEncodedValue()
guard let encoded = EncodedValue(value) else {
try value.encode(to: self)
return try getEncodedValue()
}
return encoded
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions Tests/KeyValueDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,11 @@ final class KeyValueDecoderTests: XCTestCase {
try KeyValueDecoder.makePlistCompatible().decode([Int?].self, from: plistAny),
[1, 2, Int?.none, 4]
)

XCTAssertEqual(
try KeyValueDecoder.makePlistCompatible().decode(String?.self, from: "$null"),
nil
)
}

func testJSONCompatibleDecoder() throws {
Expand Down
8 changes: 8 additions & 0 deletions Tests/KeyValueEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,14 @@ final class KeyValueEncodedTests: XCTestCase {
)
}

func testEncoder_Encodes_Dates() {
let date = Date.now
XCTAssertEqual(
try KeyValueEncoder().encode(date) as? Date,
date
)
}

func testJSONCompatibleEncoder() throws {
let keyValueAny = try KeyValueEncoder.makeJSONCompatible().encode([1, 2, Int?.none, 4])
XCTAssertEqual(
Expand Down

0 comments on commit 11d3a41

Please sign in to comment.