Skip to content

Commit 3b91cd0

Browse files
authored
Merge pull request #17 from serjooo/master
Add support to DefaultCodable to default when key is not available
2 parents b746807 + 87d9e08 commit 3b91cd0

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

Sources/BetterCodable/DefaultCodable.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,18 @@ public struct DefaultCodable<Default: DefaultCodableStrategy>: Codable {
3030

3131
extension DefaultCodable: Equatable where Default.RawValue: Equatable { }
3232
extension DefaultCodable: Hashable where Default.RawValue: Hashable { }
33+
34+
// MARK: - KeyedDecodingContainer
35+
public extension KeyedDecodingContainer {
36+
37+
/// Default implementation of decoding a DefaultCodable
38+
///
39+
/// Decodes successfully if key is available if not fallsback to the default value provided.
40+
func decode<P>(_: DefaultCodable<P>.Type, forKey key: Key) throws -> DefaultCodable<P> {
41+
if let value = try decodeIfPresent(DefaultCodable<P>.self, forKey: key) {
42+
return value
43+
} else {
44+
return DefaultCodable(wrappedValue: P.defaultValue)
45+
}
46+
}
47+
}

Tests/BetterCodableTests/DefaulEmptyDictionaryTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ class DefaultEmptyDictionaryTests: XCTestCase {
1111
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
1212
XCTAssertEqual(fixture.stringToInt, [:])
1313
}
14+
15+
func testDecodingKeyNotPresentDefaultsToEmptyDictionary() throws {
16+
let jsonData = #"{}"#.data(using: .utf8)!
17+
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
18+
XCTAssertEqual(fixture.stringToInt, [:])
19+
}
1420

1521
func testEncodingDecodedFailableDictionaryDefaultsToEmptyDictionary() throws {
1622
let jsonData = #"{ "stringToInt": null }"#.data(using: .utf8)!

Tests/BetterCodableTests/DefaultEmptyArrayTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ class DefaultEmptyArrayTests: XCTestCase {
1818
XCTAssertEqual(fixture.values, [])
1919
XCTAssertEqual(fixture.nonPrimitiveValues, [])
2020
}
21+
22+
func testDecodingKeyNotPresentDefaultsToEmptyArray() throws {
23+
let jsonData = #"{}"#.data(using: .utf8)!
24+
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
25+
XCTAssertEqual(fixture.values, [])
26+
XCTAssertEqual(fixture.nonPrimitiveValues, [])
27+
}
2128

2229
func testEncodingDecodedFailableArrayDefaultsToEmptyArray() throws {
2330
let jsonData = #"{ "values": null, "nonPrimitiveValues": null }"#.data(using: .utf8)!

Tests/BetterCodableTests/DefaultFalseTests.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ class DefaultFalseTests: XCTestCase {
66
@DefaultFalse var truthy: Bool
77
}
88

9-
func testDecodingFailableArrayDefaultsToEmptyArray() throws {
9+
func testDecodingFailableArrayDefaultsToFalse() throws {
1010
let jsonData = #"{ "truthy": null }"#.data(using: .utf8)!
1111
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
1212
XCTAssertEqual(fixture.truthy, false)
1313
}
14+
15+
func testDecodingKeyNotPresentDefaultsToFalse() throws {
16+
let jsonData = #"{}"#.data(using: .utf8)!
17+
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
18+
XCTAssertEqual(fixture.truthy, false)
19+
}
1420

15-
func testEncodingDecodedFailableArrayDefaultsToEmptyArray() throws {
21+
func testEncodingDecodedFailableArrayDefaultsToFalse() throws {
1622
let jsonData = #"{ "truthy": null }"#.data(using: .utf8)!
1723
var _fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
1824

@@ -23,7 +29,7 @@ class DefaultFalseTests: XCTestCase {
2329
XCTAssertEqual(fixture.truthy, true)
2430
}
2531

26-
func testEncodingDecodedFulfillableArrayRetainsContents() throws {
32+
func testEncodingDecodedFulfillableBoolRetainsValue() throws {
2733
let jsonData = #"{ "truthy": true }"#.data(using: .utf8)!
2834
let _fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
2935
let fixtureData = try JSONEncoder().encode(_fixture)

0 commit comments

Comments
 (0)