Skip to content

Commit

Permalink
Merge pull request #90 from derrh/master
Browse files Browse the repository at this point in the history
Missed a couple spots
  • Loading branch information
jarsen authored Dec 15, 2016
2 parents 730f1af + c5e907e commit a3dc2d0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
22 changes: 22 additions & 0 deletions MarshalTests/MarshalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,28 @@ class MarshalTests: XCTestCase {
waitForExpectations(timeout: 1, handler: nil)
}

func testOptionalArrayOfDictionaries() {
let jsonObject: JSONObject = ["not an array": 12]

do {
let optArrayOfObjects: [JSONObject]? = try jsonObject.value(for: "who cares")
XCTAssertNil(optArrayOfObjects)
} catch {
XCTFail()
}

let expectation = self.expectation(description: "type mismatch")
do {
let notAnArray: [JSONObject]? = try jsonObject.value(for: "not an array")
XCTFail("should have thrown instead of returning this: \(notAnArray)")
} 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 @@ -137,6 +137,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(for key: KeyType) throws -> MarshalDictionary {
let any = try self.any(for: key)
Expand Down
6 changes: 6 additions & 0 deletions Sources/Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public func <| <A: RawRepresentable>(dictionary: MarshaledObject, key: String) t
public func <| (dictionary: MarshaledObject, key: String) throws -> JSONObject {
return try dictionary.value(for: key)
}
public func <| (dictionary: MarshaledObject, key: String) throws -> JSONObject? {
return try dictionary.value(for: key)
}
public func <| (dictionary: MarshaledObject, key: String) throws -> [JSONObject] {
return try dictionary.value(for: key)
}
public func <| (dictionary: MarshaledObject, key: String) throws -> [JSONObject]? {
return try dictionary.value(for: key)
}

0 comments on commit a3dc2d0

Please sign in to comment.