Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Add support for invalid values with different types #152

Open
wants to merge 1 commit 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
21 changes: 16 additions & 5 deletions Sources/Unbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,18 @@ extension Array: UnboxableCollection {
public typealias UnboxValue = Element

public static func unbox<T: UnboxCollectionElementTransformer>(value: Any, allowInvalidElements: Bool, transformer: T) throws -> Array? where T.UnboxedElement == UnboxValue {
guard let array = value as? [T.UnboxRawElement] else {
guard let rawArray = value as? [Any] else {
return nil
}

let array = try rawArray.enumerated().map(allowInvalidElements: allowInvalidElements, transform: { item -> T.UnboxRawElement in
if let value = item.element as? T.UnboxRawElement {
return value
} else {
throw UnboxPathError.invalidArrayElement(item.element, item.offset)
}
})

return try array.enumerated().map(allowInvalidElements: allowInvalidElements) { index, element in
try transformer.unbox(element: element, allowInvalidCollectionElements: allowInvalidElements).orThrow(UnboxPathError.invalidArrayElement(element, index))
}
Expand All @@ -440,19 +448,22 @@ extension Dictionary: UnboxableCollection {
public typealias UnboxValue = Value

public static func unbox<T: UnboxCollectionElementTransformer>(value: Any, allowInvalidElements: Bool, transformer: T) throws -> Dictionary? where T.UnboxedElement == UnboxValue {
guard let dictionary = value as? [String : T.UnboxRawElement] else {
guard let dictionary = value as? [String : Any] else {
return nil
}

let keyTransform = try self.makeKeyTransform()

return try dictionary.map(allowInvalidElements: allowInvalidElements) { key, value in
return try dictionary.map(allowInvalidElements: allowInvalidElements) { key, rawValue in
guard let unboxedKey = keyTransform(key) else {
throw UnboxPathError.invalidDictionaryKey(key)
}

guard let unboxedValue = try transformer.unbox(element: value, allowInvalidCollectionElements: allowInvalidElements) else {
throw UnboxPathError.invalidDictionaryValue(value, key)
guard
let value = rawValue as? T.UnboxRawElement,
let unboxedValue = try transformer.unbox(element: value, allowInvalidCollectionElements: allowInvalidElements)
else {
throw UnboxPathError.invalidDictionaryValue(rawValue, key)
}

return (unboxedKey, unboxedValue)
Expand Down
4 changes: 3 additions & 1 deletion Tests/UnboxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ class UnboxTests: XCTestCase {
"nested" : [
["string" : "one"],
["invalid" : "element"],
"null",
["string" : "two"]
]
]
Expand Down Expand Up @@ -879,7 +880,8 @@ class UnboxTests: XCTestCase {
"nested" : [
"one" : ["string" : "one"],
"two" : ["invalid" : "element"],
"three" : ["string" : "two"]
"three" : ["string" : "two"],
"four" : "null"
]
]

Expand Down