Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a @LossyOptional property wrapper #30

Merged
merged 5 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Sources/BetterCodable/LossyOptional.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public struct DefaultNilStrategy<T: Codable>: DefaultCodableStrategy {
public static var defaultValue: T? { nil }
}

/// Decodes optional types, defaulting to `nil` instead of throwing an error, if applicable.
///
/// `@LossyOptional` decodes optionals, and defaults to `nil` in cases where the decoder fails (e.g. decoding a non-url string to the `URL` type)
public typealias LossyOptional<T> = DefaultCodable<DefaultNilStrategy<T>> where T: Codable

68 changes: 68 additions & 0 deletions Tests/BetterCodableTests/LossyOptionalTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import XCTest
import BetterCodable

class DefaultNilTests: XCTestCase {
/// This test demonstrates the problem that `@LossyOptional` solves. When decoding
/// optional types, it often the case that we end up with an error instead of
/// defaulting back to `nil`.
func testDecodingBadUrlAsOptionalWithoutDefaultNil() {
struct Fixture: Codable {
var a: URL?
}

let jsonData = #"{"a":"https://website .test"}"#.data(using: .utf8)!

XCTAssertThrowsError(try JSONDecoder().decode(Fixture.self, from: jsonData))
}

func testDecodingWithUrlConversions() throws {
struct Fixture: Codable {
@LossyOptional var a: URL?
@LossyOptional var b: URL?
}

let badUrlString = "https://website .test"
let goodUrlString = "https://website.test"

let jsonData = #"{"a":"\#(badUrlString)", "b":"\#(goodUrlString)"}"#.data(using: .utf8)!
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)

XCTAssertNil(fixture.a)
XCTAssertEqual(fixture.b, URL(string: goodUrlString))
}

func testDecodingWithIntegerConversions() throws {
struct Fixture: Codable {
@LossyOptional var a: Int?
@LossyOptional var b: Int?
}

let jsonData = #"{ "a": 3.14, "b": 3 }"#.data(using: .utf8)!
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)

XCTAssertNil(fixture.a)
XCTAssertEqual(fixture.b, 3)
}

func testDecodingWithNullValue() throws {
struct Fixture: Codable {
@LossyOptional var a: String?
}

let jsonData = #"{"a":null}"#.data(using: .utf8)!
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)

XCTAssertNil(fixture.a)
}

func testDecodingWithMissingKey() throws {
struct Fixture: Codable {
@LossyOptional var a: String?
}

let jsonData = "{}".data(using: .utf8)!
let fixture = try JSONDecoder().decode(Fixture.self, from: jsonData)

XCTAssertNil(fixture.a)
}
}