From 5e2737f8425f8cfba04e21c104f5bd995215b6f3 Mon Sep 17 00:00:00 2001 From: Guille Gonzalez Date: Sat, 5 Sep 2020 21:42:28 +0200 Subject: [PATCH] Add EmptyDictionary default value provider --- README.md | 3 ++ .../DefaultCodable/DefaultValueProvider.swift | 4 ++ Tests/DefaultCodableTests/DefaultTests.swift | 47 +++++++++++++------ 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 59ce1fb..796bc7b 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,9 @@ protocol DefaultValueProvider { ### `Empty` It provides an empty instance of a `String`, `Array` or any type that implements `RangeReplaceableCollection`. +### `EmptyDictionary` +It provides an empty instance of a `Dictionary`. + ### `True` and `False` Provide `true` and `false` respectively for `Bool` properties. diff --git a/Sources/DefaultCodable/DefaultValueProvider.swift b/Sources/DefaultCodable/DefaultValueProvider.swift index a0a425c..8a4b1e4 100644 --- a/Sources/DefaultCodable/DefaultValueProvider.swift +++ b/Sources/DefaultCodable/DefaultValueProvider.swift @@ -18,6 +18,10 @@ public enum Empty: DefaultValueProvider where A: Codable, A: Equatable, A: Ra public static var `default`: A { A() } } +public enum EmptyDictionary: DefaultValueProvider where K: Hashable & Codable, V: Equatable & Codable { + public static var `default`: [K: V] { Dictionary() } +} + public enum FirstCase: DefaultValueProvider where A: Codable, A: Equatable, A: CaseIterable { public static var `default`: A { A.allCases.first! } } diff --git a/Tests/DefaultCodableTests/DefaultTests.swift b/Tests/DefaultCodableTests/DefaultTests.swift index bbfb719..67cb15a 100644 --- a/Tests/DefaultCodableTests/DefaultTests.swift +++ b/Tests/DefaultCodableTests/DefaultTests.swift @@ -9,21 +9,23 @@ final class DefaultTests: XCTestCase { private struct Thing: Codable, Equatable { var name: String - @Default - var description: String - - @Default - var isFoo: Bool - - @Default - var type: ThingType - - @Default - var floatingPoint: Double - - init(name: String, description: String = "", isFoo: Bool = true, type: ThingType = .foo, floatingPoint: Double = 0) { + @Default var description: String + @Default var entities: [String: String] + @Default var isFoo: Bool + @Default var type: ThingType + @Default var floatingPoint: Double + + init( + name: String, + description: String = "", + entities: [String: String] = [:], + isFoo: Bool = true, + type: ThingType = .foo, + floatingPoint: Double = 0 + ) { self.name = name self.description = description + self.entities = entities self.isFoo = isFoo self.type = type self.floatingPoint = floatingPoint @@ -36,6 +38,9 @@ final class DefaultTests: XCTestCase { { "name": "Any name", "description": "Any description", + "entities": { + "foo": "bar" + }, "isFoo": false, "type": "baz", "floatingPoint": 12.34 @@ -47,6 +52,7 @@ final class DefaultTests: XCTestCase { // then XCTAssertEqual("Any description", result.description) + XCTAssertEqual(["foo": "bar"], result.entities) XCTAssertFalse(result.isFoo) XCTAssertEqual(ThingType.baz, result.type) XCTAssertEqual(result.floatingPoint, 12.34) @@ -58,6 +64,7 @@ final class DefaultTests: XCTestCase { { "name": "Any name", "description": null, + "entities": null, "isFoo": null, "type": null, "floatingPoint": null @@ -69,6 +76,7 @@ final class DefaultTests: XCTestCase { // then XCTAssertEqual("", result.description) + XCTAssertEqual([:], result.entities) XCTAssertTrue(result.isFoo) XCTAssertEqual(ThingType.foo, result.type) XCTAssertEqual(result.floatingPoint, 0) @@ -87,6 +95,7 @@ final class DefaultTests: XCTestCase { // then XCTAssertEqual("", result.description) + XCTAssertEqual([:], result.entities) XCTAssertTrue(result.isFoo) XCTAssertEqual(ThingType.foo, result.type) XCTAssertEqual(result.floatingPoint, 0) @@ -111,10 +120,20 @@ final class DefaultTests: XCTestCase { @available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *) func testValueEncodesToActualValue() throws { // given - let thing = Thing(name: "Any name", description: "Any description", isFoo: false, type: .baz, floatingPoint: 12.34) + let thing = Thing( + name: "Any name", + description: "Any description", + entities: ["foo": "bar"], + isFoo: false, + type: .baz, + floatingPoint: 12.34 + ) let expected = """ { "description" : "Any description", + "entities" : { + "foo" : "bar" + }, "floatingPoint" : 12.34, "isFoo" : false, "name" : "Any name",