Skip to content

Commit

Permalink
Merge pull request #3 from gonzalezreal/empty-dictionary
Browse files Browse the repository at this point in the history
Add EmptyDictionary default value provider
  • Loading branch information
gonzalezreal authored Sep 5, 2020
2 parents 549159c + 5e2737f commit 7417376
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 4 additions & 0 deletions Sources/DefaultCodable/DefaultValueProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public enum Empty<A>: DefaultValueProvider where A: Codable, A: Equatable, A: Ra
public static var `default`: A { A() }
}

public enum EmptyDictionary<K, V>: DefaultValueProvider where K: Hashable & Codable, V: Equatable & Codable {
public static var `default`: [K: V] { Dictionary() }
}

public enum FirstCase<A>: DefaultValueProvider where A: Codable, A: Equatable, A: CaseIterable {
public static var `default`: A { A.allCases.first! }
}
Expand Down
47 changes: 33 additions & 14 deletions Tests/DefaultCodableTests/DefaultTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ final class DefaultTests: XCTestCase {
private struct Thing: Codable, Equatable {
var name: String

@Default<Empty>
var description: String

@Default<True>
var isFoo: Bool

@Default<FirstCase>
var type: ThingType

@Default<ZeroDouble>
var floatingPoint: Double

init(name: String, description: String = "", isFoo: Bool = true, type: ThingType = .foo, floatingPoint: Double = 0) {
@Default<Empty> var description: String
@Default<EmptyDictionary> var entities: [String: String]
@Default<True> var isFoo: Bool
@Default<FirstCase> var type: ThingType
@Default<ZeroDouble> 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
Expand All @@ -36,6 +38,9 @@ final class DefaultTests: XCTestCase {
{
"name": "Any name",
"description": "Any description",
"entities": {
"foo": "bar"
},
"isFoo": false,
"type": "baz",
"floatingPoint": 12.34
Expand All @@ -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)
Expand All @@ -58,6 +64,7 @@ final class DefaultTests: XCTestCase {
{
"name": "Any name",
"description": null,
"entities": null,
"isFoo": null,
"type": null,
"floatingPoint": null
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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",
Expand Down

0 comments on commit 7417376

Please sign in to comment.