Skip to content

Commit

Permalink
Merge pull request #91 from KingOfBrian/bugfix/OperatorExpansion
Browse files Browse the repository at this point in the history
Bug fix operator expansion
  • Loading branch information
jarsen authored Dec 15, 2016
2 parents a3dc2d0 + 0d1ca86 commit c067723
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
15 changes: 14 additions & 1 deletion MarshalTests/MarshalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,17 @@ class MarshalTests: XCTestCase {
let dead = try! !person.value(for: "living")
XCTAssertTrue(dead)

let result: [String: Bool] = try! json.value(for: "result")
var result: [String: Bool] = try! json.value(for: "result")
XCTAssertEqual(result.count, 1)
XCTAssertEqual(result["ok"], true)
do {
// Check the optional getter
result = try json.value(for: "result") ?? [:]
result = try json.value(for: "emptyKey") ?? [:]
}
catch {
XCTFail()
}
}

func testOptionalDictionary() {
Expand Down Expand Up @@ -412,6 +420,7 @@ class MarshalTests: XCTestCase {
"huge": Int.max,
"decimal": 1.2,
"array": [ "a", "b", "c" ],
"boolDictionary": [ "a": true, "b": false, "c": true ],
"nested": [
"key": "value"
]
Expand All @@ -428,6 +437,8 @@ class MarshalTests: XCTestCase {
let huge: Int = try result <| "huge"
let decimal: Float = try result <| "decimal"
let array: [String] = try result <| "array"
let boolDictionary: [String: Bool] = try result <| "boolDictionary"
let optBoolDictionary: [String: Bool]? = try result <| "boolDictionary"
let nested: [String:Any] = try result <| "nested"

XCTAssertEqual(string, "A String")
Expand All @@ -440,6 +451,8 @@ class MarshalTests: XCTestCase {
XCTAssertEqual(decimal, 1.2)
XCTAssertEqual(array, [ "a", "b", "c" ])
XCTAssertEqual(nested as! [String:String], [ "key": "value" ])
XCTAssertEqual(boolDictionary, [ "a": true, "b": false, "c": true ])
XCTAssertEqual(boolDictionary, optBoolDictionary ?? [:])
} catch {
XCTFail("Error converting MarshalDictionary: \(error)")
}
Expand Down
1 change: 1 addition & 0 deletions Sources/MarshaledObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public extension MarshaledObject {

public func value<A: ValueType>(for key: KeyType) throws -> [String: A]? {
do {
let any = try self.any(for: key)
return try [String: A].value(from: any)
}
catch MarshalError.keyNotFound {
Expand Down
6 changes: 6 additions & 0 deletions Sources/Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,9 @@ public func <| (dictionary: MarshaledObject, key: String) throws -> [JSONObject]
public func <| (dictionary: MarshaledObject, key: String) throws -> [JSONObject]? {
return try dictionary.value(for: key)
}
public func <| <A: ValueType>(dictionary: MarshaledObject, key: String) throws -> [String: A] {
return try dictionary.value(for: key)
}
public func <| <A: ValueType>(dictionary: MarshaledObject, key: String) throws -> [String: A]? {
return try dictionary.value(for: key)
}

0 comments on commit c067723

Please sign in to comment.