Skip to content

Commit 8bc30d2

Browse files
authored
Merge pull request #1 from IuriiIaremenko/feature/double-type
Add ZeroDouble provider
2 parents 6afea22 + e98da6f commit 8bc30d2

3 files changed

Lines changed: 21 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ Provide `0` and `1` respectively for `Int` properties.
8282
### `FirstCase`
8383
It provides the first case of an `enum` type as the default value. The `enum` must implement the `CaseIterable` protocol.
8484

85+
### `ZeroDouble`
86+
Provide `0` for `Double` properties.
87+
8588
## Default values for custom types
8689
Your custom type must implement the `DefaultValueProvider` protocol to be compatible with the `@Default` property wrapper.
8790

Sources/DefaultCodable/DefaultValueProvider.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ public enum Zero: DefaultValueProvider {
2929
public enum One: DefaultValueProvider {
3030
public static let `default` = 1
3131
}
32+
33+
public enum ZeroDouble: DefaultValueProvider {
34+
public static let `default`: Double = 0
35+
}

Tests/DefaultCodableTests/DefaultTests.swift

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ final class DefaultTests: XCTestCase {
1717

1818
@Default<FirstCase>
1919
var type: ThingType
20+
21+
@Default<ZeroDouble>
22+
var floatingPoint: Double
2023
}
2124

2225
func testValueDecodesToActualValue() throws {
@@ -26,7 +29,8 @@ final class DefaultTests: XCTestCase {
2629
"name": "Any name",
2730
"description": "Any description",
2831
"isFoo": false,
29-
"type": "baz"
32+
"type": "baz",
33+
"floatingPoint": 12.34
3034
}
3135
""".data(using: .utf8)!
3236

@@ -37,6 +41,7 @@ final class DefaultTests: XCTestCase {
3741
XCTAssertEqual("Any description", result.description)
3842
XCTAssertFalse(result.isFoo)
3943
XCTAssertEqual(ThingType.baz, result.type)
44+
XCTAssertEqual(result.floatingPoint, 12.34)
4045
}
4146

4247
func testNullDecodesToDefaultValue() throws {
@@ -46,7 +51,8 @@ final class DefaultTests: XCTestCase {
4651
"name": "Any name",
4752
"description": null,
4853
"isFoo": null,
49-
"type": null
54+
"type": null,
55+
"floatingPoint": null
5056
}
5157
""".data(using: .utf8)!
5258

@@ -57,6 +63,7 @@ final class DefaultTests: XCTestCase {
5763
XCTAssertEqual("", result.description)
5864
XCTAssertTrue(result.isFoo)
5965
XCTAssertEqual(ThingType.foo, result.type)
66+
XCTAssertEqual(result.floatingPoint, 0)
6067
}
6168

6269
func testNotPresentValueDecodesToDefaultValue() throws {
@@ -74,6 +81,7 @@ final class DefaultTests: XCTestCase {
7481
XCTAssertEqual("", result.description)
7582
XCTAssertTrue(result.isFoo)
7683
XCTAssertEqual(ThingType.foo, result.type)
84+
XCTAssertEqual(result.floatingPoint, 0)
7785
}
7886

7987
func testTypeMismatchThrows() {
@@ -83,7 +91,8 @@ final class DefaultTests: XCTestCase {
8391
"name": "Any name",
8492
"description": ["nope"],
8593
"isFoo": 5500,
86-
"type": [1, 2, 3]
94+
"type": [1, 2, 3],
95+
"floatingPoint": "point"
8796
}
8897
""".data(using: .utf8)!
8998

@@ -94,10 +103,11 @@ final class DefaultTests: XCTestCase {
94103
@available(OSX 10.13, iOS 11.0, watchOS 4.0, tvOS 11.0, *)
95104
func testValueEncodesToActualValue() throws {
96105
// given
97-
let thing = Thing(name: "Any name", description: "Any description", isFoo: false, type: .baz)
106+
let thing = Thing(name: "Any name", description: "Any description", isFoo: false, type: .baz, floatingPoint: 12.34)
98107
let expected = """
99108
{
100109
"description" : "Any description",
110+
"floatingPoint" : 12.34,
101111
"isFoo" : false,
102112
"name" : "Any name",
103113
"type" : "baz"

0 commit comments

Comments
 (0)