Skip to content

Commit 7417376

Browse files
authored
Merge pull request #3 from gonzalezreal/empty-dictionary
Add EmptyDictionary default value provider
2 parents 549159c + 5e2737f commit 7417376

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ protocol DefaultValueProvider {
7373
### `Empty`
7474
It provides an empty instance of a `String`, `Array` or any type that implements `RangeReplaceableCollection`.
7575

76+
### `EmptyDictionary`
77+
It provides an empty instance of a `Dictionary`.
78+
7679
### `True` and `False`
7780
Provide `true` and `false` respectively for `Bool` properties.
7881

Sources/DefaultCodable/DefaultValueProvider.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public enum Empty<A>: DefaultValueProvider where A: Codable, A: Equatable, A: Ra
1818
public static var `default`: A { A() }
1919
}
2020

21+
public enum EmptyDictionary<K, V>: DefaultValueProvider where K: Hashable & Codable, V: Equatable & Codable {
22+
public static var `default`: [K: V] { Dictionary() }
23+
}
24+
2125
public enum FirstCase<A>: DefaultValueProvider where A: Codable, A: Equatable, A: CaseIterable {
2226
public static var `default`: A { A.allCases.first! }
2327
}

Tests/DefaultCodableTests/DefaultTests.swift

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,23 @@ final class DefaultTests: XCTestCase {
99
private struct Thing: Codable, Equatable {
1010
var name: String
1111

12-
@Default<Empty>
13-
var description: String
14-
15-
@Default<True>
16-
var isFoo: Bool
17-
18-
@Default<FirstCase>
19-
var type: ThingType
20-
21-
@Default<ZeroDouble>
22-
var floatingPoint: Double
23-
24-
init(name: String, description: String = "", isFoo: Bool = true, type: ThingType = .foo, floatingPoint: Double = 0) {
12+
@Default<Empty> var description: String
13+
@Default<EmptyDictionary> var entities: [String: String]
14+
@Default<True> var isFoo: Bool
15+
@Default<FirstCase> var type: ThingType
16+
@Default<ZeroDouble> var floatingPoint: Double
17+
18+
init(
19+
name: String,
20+
description: String = "",
21+
entities: [String: String] = [:],
22+
isFoo: Bool = true,
23+
type: ThingType = .foo,
24+
floatingPoint: Double = 0
25+
) {
2526
self.name = name
2627
self.description = description
28+
self.entities = entities
2729
self.isFoo = isFoo
2830
self.type = type
2931
self.floatingPoint = floatingPoint
@@ -36,6 +38,9 @@ final class DefaultTests: XCTestCase {
3638
{
3739
"name": "Any name",
3840
"description": "Any description",
41+
"entities": {
42+
"foo": "bar"
43+
},
3944
"isFoo": false,
4045
"type": "baz",
4146
"floatingPoint": 12.34
@@ -47,6 +52,7 @@ final class DefaultTests: XCTestCase {
4752

4853
// then
4954
XCTAssertEqual("Any description", result.description)
55+
XCTAssertEqual(["foo": "bar"], result.entities)
5056
XCTAssertFalse(result.isFoo)
5157
XCTAssertEqual(ThingType.baz, result.type)
5258
XCTAssertEqual(result.floatingPoint, 12.34)
@@ -58,6 +64,7 @@ final class DefaultTests: XCTestCase {
5864
{
5965
"name": "Any name",
6066
"description": null,
67+
"entities": null,
6168
"isFoo": null,
6269
"type": null,
6370
"floatingPoint": null
@@ -69,6 +76,7 @@ final class DefaultTests: XCTestCase {
6976

7077
// then
7178
XCTAssertEqual("", result.description)
79+
XCTAssertEqual([:], result.entities)
7280
XCTAssertTrue(result.isFoo)
7381
XCTAssertEqual(ThingType.foo, result.type)
7482
XCTAssertEqual(result.floatingPoint, 0)
@@ -87,6 +95,7 @@ final class DefaultTests: XCTestCase {
8795

8896
// then
8997
XCTAssertEqual("", result.description)
98+
XCTAssertEqual([:], result.entities)
9099
XCTAssertTrue(result.isFoo)
91100
XCTAssertEqual(ThingType.foo, result.type)
92101
XCTAssertEqual(result.floatingPoint, 0)
@@ -111,10 +120,20 @@ final class DefaultTests: XCTestCase {
111120
@available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *)
112121
func testValueEncodesToActualValue() throws {
113122
// given
114-
let thing = Thing(name: "Any name", description: "Any description", isFoo: false, type: .baz, floatingPoint: 12.34)
123+
let thing = Thing(
124+
name: "Any name",
125+
description: "Any description",
126+
entities: ["foo": "bar"],
127+
isFoo: false,
128+
type: .baz,
129+
floatingPoint: 12.34
130+
)
115131
let expected = """
116132
{
117133
"description" : "Any description",
134+
"entities" : {
135+
"foo" : "bar"
136+
},
118137
"floatingPoint" : 12.34,
119138
"isFoo" : false,
120139
"name" : "Any name",

0 commit comments

Comments
 (0)