Skip to content

Commit

Permalink
fixed nested arrays (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriomazzeo authored and vzsg committed Oct 24, 2018
1 parent f36c1fe commit 13aa3d7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Sources/MongoDriver/Conversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ extension StructuredData : Convertible {

return string.convert(to: type)
case .array(let array):
return array.convert(to: type)
return array.flatMap { $0.convert(to: type) } as? DT.SupportedValue
case .object(let object):
return object.convert(to: type)
case .bytes(let bytes):
Expand Down
34 changes: 31 additions & 3 deletions Tests/MongoDriverTests/DriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class DriverTests: XCTestCase {
return [
("testInsertAndFind", testInsertAndFind),
("testArray", testArray),
("testArrayOfArrays", testArrayOfArrays),
("testOuterJoin", testOuterJoin),
("testSiblingsCount", testSiblingsCount),
("testMax", testMax),
Expand Down Expand Up @@ -44,9 +45,9 @@ class DriverTests: XCTestCase {

let recordStore = RecordStore(
vinyls: [
Vinyl(name: "Thriller", year: 1982),
Vinyl(name: "Back in Black", year: 1980),
Vinyl(name: "The Dark Side of the Moon", year: 1973)
Vinyl(authors: ["Michael Jackson"], name: "Thriller", year: 1982),
Vinyl(authors: ["AC/DC"], name: "Back in Black", year: 1980),
Vinyl(authors: ["Pink Floyd"], name: "The Dark Side of the Moon", year: 1973)
]
)

Expand All @@ -62,6 +63,33 @@ class DriverTests: XCTestCase {
XCTAssert(foundStore.vinyls.filter({ return $0.name == "Thriller" }).count == 1)
}

func testArrayOfArrays() throws {
try driver.drop()
let db = Fluent.Database(driver)

NodeEntity.database = db

let node: Node = [
"key1": "value1",
"key2": [
["k2i0v1", "k2i0v2"],
["k2i1v1", "k2i1v2"]
]
]

let entity = NodeEntity(node: node)
try entity.save()

guard let foundNode = try NodeEntity.makeQuery().all().first else {
XCTFail()
return
}

XCTAssertEqual(foundNode.node["key1"]?.string, "value1")
XCTAssertEqual(foundNode.node["key2"]!.array!.first!.array!.flatMap { $0.string }, ["k2i0v1", "k2i0v2"])
XCTAssertEqual(foundNode.node["key2"]!.array!.last!.array!.flatMap { $0.string }, ["k2i1v1", "k2i1v2"])
}

func testOuterJoin() throws {
try driver.drop()
let db = Fluent.Database(driver)
Expand Down
33 changes: 33 additions & 0 deletions Tests/MongoDriverTests/NodeEntity.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Foundation
import Fluent

final class NodeEntity: Entity {

public static var entity: String {
return "nodeEntities"
}

public static var name: String {
return "nodeEntity"
}

public var node: Node

public init(node: Node) {
self.node = node
}

// MARK: Storable

public let storage = Storage()

// MARK: RowConvertible

public convenience init(row: Row) throws {
self.init(node: row.makeNode(in: nil))
}

public func makeRow() throws -> Row {
return Row(node: self.node)
}
}
10 changes: 7 additions & 3 deletions Tests/MongoDriverTests/Vinyl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ import Foundation
import Fluent

public final class Vinyl: NodeRepresentable {


public var authors: [String]
public var name: String
public var year: Int

enum Keys: String {
case
authors,
name,
year
}

public init(name: String, year: Int) {

public init(authors: [String] = [], name: String, year: Int) {

self.authors = authors
self.name = name
self.year = year

Expand All @@ -23,6 +26,7 @@ public final class Vinyl: NodeRepresentable {

return Node.object(
[
Keys.authors.rawValue: authors.makeNode(),
Keys.name.rawValue: name.makeNode(),
Keys.year.rawValue: year.makeNode()
]
Expand Down

0 comments on commit 13aa3d7

Please sign in to comment.