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

Commit

Permalink
Merge pull request #164 from hartbit/set-unboxable-collection
Browse files Browse the repository at this point in the history
Make set into an unboxable collection
  • Loading branch information
JohnSundell authored Feb 16, 2017
2 parents f460b0d + e3a3927 commit ba42e01
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Sources/Unbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,19 @@ extension Array: UnboxableCollection {
}
}

/// Extension making Set an unboxable collection
extension Set: UnboxableCollection {
public typealias UnboxValue = Element

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

return Set(array)
}
}

/// Extension making Dictionary an unboxable collection
extension Dictionary: UnboxableCollection {
public typealias UnboxValue = Value
Expand Down
27 changes: 27 additions & 0 deletions Tests/UnboxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,33 @@ class UnboxTests: XCTestCase {
XCTFail("\(error)")
}
}

func testSets() {
struct Model: Unboxable {
let optional: Set<String>?
let required: Set<String>

init(unboxer: Unboxer) throws {
self.optional = unboxer.unbox(key: "optional")
self.required = try unboxer.unbox(key: "required")
}
}

let dictionary: UnboxableDictionary = [
"optional" : ["A", "A", "B"],
"required" : ["A"]
]

do {
let unboxed: Model = try unbox(dictionary: dictionary)
XCTAssertEqual(unboxed.optional?.count, 2)
XCTAssertTrue(unboxed.optional?.contains("A") ?? false)
XCTAssertTrue(unboxed.optional?.contains("B") ?? false)
XCTAssertEqual(unboxed.required, ["A"])
} catch {
XCTFail("\(error)")
}
}
}

private func UnboxTestDictionaryWithAllRequiredKeysWithValidValues(nested: Bool) -> UnboxableDictionary {
Expand Down

0 comments on commit ba42e01

Please sign in to comment.