Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix operator expansion #91

Merged
merged 7 commits into from
Dec 15, 2016
Merged
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
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)
}