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

Added support for default value to overcome decoding errors when we h… #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions Sources/Codextended/Codextended.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ public extension Data {
}
}

public extension KeyedDecodingContainer {
func decodeWrapper<T>(key: K, defaultValue: T) throws -> T
where T : Decodable {
return try decodeIfPresent(T.self, forKey: key) ?? defaultValue
}
}

public extension Decoder {
/// Decode a singular value from the underlying data.
func decodeSingleValue<T: Decodable>(as type: T.Type = T.self) throws -> T {
Expand All @@ -97,12 +104,23 @@ public extension Decoder {
return try decode(AnyCodingKey(key), as: type)
}

/// Decode a value for a given key, specified as a string with default value.
func decode<T: Decodable>(_ key: String, as type: T.Type = T.self, defaultValue: T) throws -> T {
return try decode(AnyCodingKey(key), as: type, defaultValue: defaultValue)
}

/// Decode a value for a given key, specified as a `CodingKey`.
func decode<T: Decodable, K: CodingKey>(_ key: K, as type: T.Type = T.self) throws -> T {
let container = try self.container(keyedBy: K.self)
return try container.decode(type, forKey: key)
}

/// Decode a value for a given key, specified as a `CodingKey` with a default value.
func decode<T: Decodable, K: CodingKey>(_ key: K, as type: T.Type = T.self, defaultValue: T) throws -> T {
let container = try self.container(keyedBy: K.self)
return try container.decodeWrapper(key: key, defaultValue: defaultValue)
}

/// Decode an optional value for a given key, specified as a string. Throws an error if the
/// specified key exists but is not able to be decoded as the inferred type.
func decodeIfPresent<T: Decodable>(_ key: String, as type: T.Type = T.self) throws -> T? {
Expand Down
54 changes: 53 additions & 1 deletion Tests/CodextendedTests/CodextendedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,56 @@ final class CodextendedTests: XCTestCase {
}
}

func testDecodeUsingStringAsKeyWithDefaultValueOptional() {
struct Value: Codable {
var string: String?

init(string: String) {
self.string = string
}

init(from decoder: Decoder) throws {
string = try decoder.decode("key", defaultValue: self.string)
}

func encode(to encoder: Encoder) throws {
try encoder.encode(string, for: "key")
}
}

let empty = "{}".data(using: .utf8)!
XCTAssertNoThrow(try empty.decoded() as Value)
let dataChangedKey = "{\"changedKey\":\"Hello, world!\"}".data(using: .utf8)!
XCTAssertNoThrow(try dataChangedKey.decoded() as Value)
let dataNull = "{\"string\":null}".data(using: .utf8)!
XCTAssertNoThrow(try dataNull.decoded() as Value)
}

func testDecodeUsingStringAsKeyWithDefaultValue() {
struct Value: Codable {
var string: String = "Hello, world!"

init(string: String) {
self.string = string
}

init(from decoder: Decoder) throws {
string = try decoder.decode("key", defaultValue: self.string)
}

func encode(to encoder: Encoder) throws {
try encoder.encode(string, for: "key")
}
}

let empty = "{}".data(using: .utf8)!
XCTAssertNoThrow(try empty.decoded() as Value)
let dataChangedKey = "{\"changedKey\":\"Hello, world!\"}".data(using: .utf8)!
XCTAssertNoThrow(try dataChangedKey.decoded() as Value)
let dataNull = "{\"string\":null}".data(using: .utf8)!
XCTAssertNoThrow(try dataNull.decoded() as Value)
}

func testSingleValue() throws {
struct Value: Codable, Equatable {
let string: String
Expand Down Expand Up @@ -99,7 +149,7 @@ final class CodextendedTests: XCTestCase {
let valueB = try data.decoded() as Value
XCTAssertEqual(valueA, valueB)
}

func testUsingCodingKey() throws {
struct Value: Codable, Equatable {
enum CodingKeys: CodingKey {
Expand Down Expand Up @@ -222,6 +272,8 @@ extension CodextendedTests: LinuxTestable {
("testEncodingAndDecoding", testEncodingAndDecoding),
("testDecodeIfPresent", testDecodeIfPresent),
("testDecodeIfPresentTypeMismatch", testDecodeIfPresentTypeMismatch),
("testDecodeUsingStringAsKeyWithDefaultValueOptional",testDecodeUsingStringAsKeyWithDefaultValueOptional),
("testDecodeUsingStringAsKeyWithDefaultValue",testDecodeUsingStringAsKeyWithDefaultValue),
("testSingleValue", testSingleValue),
("testUsingStringAsKey", testUsingStringAsKey),
("testUsingCodingKey", testUsingCodingKey),
Expand Down