Skip to content

Commit

Permalink
add contains operator support (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 authored Jul 23, 2020
1 parent 4b3ee2b commit 4f3c1c3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
19 changes: 16 additions & 3 deletions Sources/FluentMongoDriver/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,22 @@ enum FluentMongoError: Error, DatabaseError {
var isConstraintFailure: Bool { false }
var isConnectionClosed: Bool { false }

case missingHosts, noTargetDatabaseSpecified, unsupportedJoin, unsupportedOperator, invalidIndexKey
case unsupportedField, unsupportedDefaultValue, insertFailed, unsupportedFilter
case unsupportedCustomLimit, unsupportedCustomFilter, unsupportedCustomValue, unsupportedCustomAction, unsupportedCustomSort, unsupportedCustomAggregate
case missingHosts
case noTargetDatabaseSpecified
case unsupportedJoin
case unsupportedOperator
case unsupportedFilterValue
case invalidIndexKey
case unsupportedField
case unsupportedDefaultValue
case insertFailed
case unsupportedFilter
case unsupportedCustomLimit
case unsupportedCustomFilter
case unsupportedCustomValue
case unsupportedCustomAction
case unsupportedCustomSort
case unsupportedCustomAggregate
case notMongoDB, fileNotFound
}

Expand Down
24 changes: 22 additions & 2 deletions Sources/FluentMongoDriver/Filter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,29 @@ extension DatabaseQuery.Filter {
path = try field.makeMongoPath()
}

let filterOperator = try operation.makeMongoOperator()
var filter = Document()
try filter[path][filterOperator] = value.makePrimitive()
switch operation {
case .contains(let inverse, let contains):
guard case .bind(let bind) = value, let string = bind as? String else {
throw FluentMongoError.unsupportedFilterValue
}
let pattern: String
switch contains {
case .anywhere:
pattern = ".*\(string).*"
case .prefix:
pattern = "\(string).*"
case .suffix:
pattern = ".*\(string)"
}
if inverse {
filter[path]["$not"]["$regex"] = pattern
} else {
filter[path]["$regex"] = pattern
}
default:
try filter[path][operation.makeMongoOperator()] = value.makePrimitive()
}
return filter
case .field(let a, let operation, let b):
var filter = Document()
Expand Down
2 changes: 2 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#error("Please test with `swift test --enable-test-discovery`")

0 comments on commit 4f3c1c3

Please sign in to comment.