Skip to content

Commit

Permalink
Merge pull request #54 from vapor/context-dictionary
Browse files Browse the repository at this point in the history
fix context-dctionary bug
  • Loading branch information
loganwright authored Mar 3, 2017
2 parents 68f7785 + f82f79a commit 93d7636
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
10 changes: 8 additions & 2 deletions Sources/Node/Core/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@
*/
public protocol Context {}

extension Array: Context {}
extension Dictionary: Context {}
public final class ObjectContext<K: Hashable, V>: Context {
public let object: [K: V]
public init(_ object: [K: V]) {
self.object = object
}
}

public let emptyContext = ObjectContext<String, Int>([:])
4 changes: 3 additions & 1 deletion Sources/Node/Core/Node.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
public struct Node: StructuredDataWrapper {
public static let defaultContext = emptyContext

public var wrapped: StructuredData
public var context: Context

public init(_ wrapped: StructuredData, in context: Context?) {
self.wrapped = wrapped
self.context = context ?? [String: Int]()
self.context = context ?? emptyContext
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/NodeTests/NodeBackedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct JSON: StructuredDataWrapper {
var context: Context
init(_ wrapped: StructuredData, in context: Context?) {
self.wrapped = wrapped
self.context = context ?? [String: Int]()
self.context = context ?? emptyContext
}
}

Expand Down
10 changes: 5 additions & 5 deletions Tests/NodeTests/SequenceConvertibleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ class SequenceConvertibleTests: XCTestCase {
XCTAssertNil(foo1.contextMakeNode)
XCTAssertNil(foo2.contextMakeNode)

let context = ["isContext": true]
let context = ObjectContext(["isContext": true])

let _ = try [foo1, foo2].makeNode(in: context)

guard let foo1Context = foo1.contextMakeNode as? [String: Bool],
let foo2Context = foo1.contextMakeNode as? [String: Bool] else {
guard let foo1Context = foo1.contextMakeNode as? ObjectContext<String, Bool>,
let foo2Context = foo1.contextMakeNode as? ObjectContext<String, Bool> else {
XCTFail()
return
}

XCTAssert(foo1Context == context)
XCTAssert(foo2Context == context)
XCTAssert(foo1Context === context)
XCTAssert(foo2Context === context)

}

Expand Down

0 comments on commit 93d7636

Please sign in to comment.