Skip to content

Commit

Permalink
Merge pull request #17 from serjooo/master
Browse files Browse the repository at this point in the history
Add support to DefaultCodable to default when key is not available
  • Loading branch information
marksands authored Feb 15, 2020
2 parents b746807 + 87d9e08 commit 3b91cd0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
15 changes: 15 additions & 0 deletions Sources/BetterCodable/DefaultCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@ public struct DefaultCodable<Default: DefaultCodableStrategy>: Codable {

extension DefaultCodable: Equatable where Default.RawValue: Equatable { }
extension DefaultCodable: Hashable where Default.RawValue: Hashable { }

// MARK: - KeyedDecodingContainer
public extension KeyedDecodingContainer {

/// Default implementation of decoding a DefaultCodable
///
/// Decodes successfully if key is available if not fallsback to the default value provided.
func decode<P>(_: DefaultCodable<P>.Type, forKey key: Key) throws -> DefaultCodable<P> {
if let value = try decodeIfPresent(DefaultCodable<P>.self, forKey: key) {
return value
} else {
return DefaultCodable(wrappedValue: P.defaultValue)
}
}
}
6 changes: 6 additions & 0 deletions Tests/BetterCodableTests/DefaulEmptyDictionaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class DefaultEmptyDictionaryTests: XCTestCase {
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
XCTAssertEqual(fixture.stringToInt, [:])
}

func testDecodingKeyNotPresentDefaultsToEmptyDictionary() throws {
let jsonData = #"{}"#.data(using: .utf8)!
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
XCTAssertEqual(fixture.stringToInt, [:])
}

func testEncodingDecodedFailableDictionaryDefaultsToEmptyDictionary() throws {
let jsonData = #"{ "stringToInt": null }"#.data(using: .utf8)!
Expand Down
7 changes: 7 additions & 0 deletions Tests/BetterCodableTests/DefaultEmptyArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class DefaultEmptyArrayTests: XCTestCase {
XCTAssertEqual(fixture.values, [])
XCTAssertEqual(fixture.nonPrimitiveValues, [])
}

func testDecodingKeyNotPresentDefaultsToEmptyArray() throws {
let jsonData = #"{}"#.data(using: .utf8)!
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
XCTAssertEqual(fixture.values, [])
XCTAssertEqual(fixture.nonPrimitiveValues, [])
}

func testEncodingDecodedFailableArrayDefaultsToEmptyArray() throws {
let jsonData = #"{ "values": null, "nonPrimitiveValues": null }"#.data(using: .utf8)!
Expand Down
12 changes: 9 additions & 3 deletions Tests/BetterCodableTests/DefaultFalseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ class DefaultFalseTests: XCTestCase {
@DefaultFalse var truthy: Bool
}

func testDecodingFailableArrayDefaultsToEmptyArray() throws {
func testDecodingFailableArrayDefaultsToFalse() throws {
let jsonData = #"{ "truthy": null }"#.data(using: .utf8)!
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
XCTAssertEqual(fixture.truthy, false)
}

func testDecodingKeyNotPresentDefaultsToFalse() throws {
let jsonData = #"{}"#.data(using: .utf8)!
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
XCTAssertEqual(fixture.truthy, false)
}

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

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

func testEncodingDecodedFulfillableArrayRetainsContents() throws {
func testEncodingDecodedFulfillableBoolRetainsValue() throws {
let jsonData = #"{ "truthy": true }"#.data(using: .utf8)!
let _fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)
let fixtureData = try JSONEncoder().encode(_fixture)
Expand Down

0 comments on commit 3b91cd0

Please sign in to comment.