Skip to content

Commit

Permalink
Merge pull request #89 from derrh/master
Browse files Browse the repository at this point in the history
Fix regression unmarshalling optional subdictionaries
  • Loading branch information
jarsen authored Dec 14, 2016
2 parents 3c41190 + a7d4b85 commit 8995f15
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
25 changes: 25 additions & 0 deletions MarshalTests/MarshalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@ class MarshalTests: XCTestCase {
XCTAssertEqual(result["ok"], true)
}

func testOptionalDictionary() {
let jsonObject: JSONObject = ["not a dictionary": ["strings", "strings and", "more strings"], "also not a dictionary": 12]

do {
let optSubObject: JSONObject? = try jsonObject.value(for: "whatevs")
XCTAssertNil(optSubObject)
} catch {
XCTFail()
}

let expectation = self.expectation(description: "type mismatch")
do {
let totesNotADict: JSONObject? = try jsonObject.value(for: "not a dictionary")
XCTFail("what are you? \(totesNotADict)")

let alsoNotADictionary: JSONObject? = try jsonObject.value(for: "also not a dictionary")
XCTFail("what are you? \(alsoNotADictionary)")
} catch {
if case MarshalError.typeMismatchWithKey = error {
expectation.fulfill()
}
}
waitForExpectations(timeout: 1, handler: nil)
}

func testSimpleArray() {
let path = Bundle(for: type(of: self)).path(forResource: "TestSimpleArray", ofType: "json")!
var data = try! Data(contentsOf: URL(fileURLWithPath: path))
Expand Down
12 changes: 12 additions & 0 deletions Sources/MarshaledObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ public extension MarshaledObject {
}
return object
}

public func value(for key: KeyType) throws -> MarshalDictionary? {
do {
return try value(for: key) as MarshalDictionary
}
catch MarshalError.keyNotFound {
return nil
}
catch MarshalError.nullValue {
return nil
}
}

public func value<A: ValueType>(for key: KeyType) throws -> Set<A> {
let any = try self.any(for: key)
Expand Down

0 comments on commit 8995f15

Please sign in to comment.