Skip to content

Commit

Permalink
Merge pull request #40 from paulrolfe/pr/operators
Browse files Browse the repository at this point in the history
Operators:  `.contains`, `.startsWith`, `.endsWith`
  • Loading branch information
Obbut authored Sep 9, 2016
2 parents 6e471c7 + 23e6173 commit c16e2f5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Sources/MongoKitten/QueryBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ public indirect enum AQT {
return ["$or": .array(Document(array: expressions)) ]
case .not(let aqt):
return ["$not": ~aqt.document]
case .contains(let key, let val):
return [key: .regularExpression(pattern: val, options: "")]
case .startsWith(let key, let val):
return [key: .regularExpression(pattern: "^\(val)", options: "m")]
case .endsWith(let key, let val):
return [key: .regularExpression(pattern: "\(val)$", options: "m")]
case .nothing:
return []
}
Expand Down Expand Up @@ -281,6 +287,15 @@ public indirect enum AQT {

/// Whether nothing needs to be matched. Is always true and just a placeholder
case nothing

/// Whether the String value within the `key` contains this `String`.
case contains(key: String, val: String)

/// Whether the String value within the `key` starts with this `String`.
case startsWith(key: String, val: String)

/// Whether the String value within the `key` ends with this `String`.
case endsWith(key: String, val: String)
}

/// The protocol all queries need to comply to
Expand Down Expand Up @@ -507,6 +522,27 @@ extension Document {
return false
case .not(let aqt):
return !self.matches(query: Query(aqt: aqt))
case .contains(let key, let val):
switch doc[key] {
case .string(let stringVal):
return stringVal.contains(val)
default:
return false
}
case .startsWith(let key, let val):
switch doc[key] {
case .string(let stringVal):
return stringVal.hasPrefix(val)
default:
return false
}
case .endsWith(let key, let val):
switch doc[key] {
case .string(let stringVal):
return stringVal.hasSuffix(val)
default:
return false
}
case .nothing:
return true
}
Expand Down
22 changes: 22 additions & 0 deletions Tests/MongoKittenTests/CollectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ class CollectionTests: XCTestCase {
XCTAssertEqual(response.count, 2)

XCTAssertEqual(response.first, response2)

runContainsQuery()
runStartsWithQuery()
runEndsWithQuery()
}

private func runContainsQuery() {
let query = Query(aqt: .contains(key: "username", val: "ar"))
let response = Array(try! TestManager.wcol.find(matching: query))
XCTAssert(response.count == 2)
}

private func runStartsWithQuery() {
let query = Query(aqt: .startsWith(key: "username", val: "har"))
let response = Array(try! TestManager.wcol.find(matching: query))
XCTAssert(response.count == 2)
}

private func runEndsWithQuery() {
let query = Query(aqt: .endsWith(key: "username", val: "rrie"))
let response = Array(try! TestManager.wcol.find(matching: query))
XCTAssert(response.count == 2)
}

func testAggregate() {
Expand Down

0 comments on commit c16e2f5

Please sign in to comment.