-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2ac23c
commit 61eb967
Showing
3 changed files
with
63 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// | ||
// Predicate.swift | ||
// | ||
// | ||
// Created by Alsey Coleman Miller on 8/21/23. | ||
// | ||
|
||
import Foundation | ||
import CoreModel | ||
import Predicate | ||
import MongoSwift | ||
|
||
public extension BSONDocument { | ||
|
||
init?(predicate: Predicate) { | ||
switch predicate { | ||
case .comparison(let comparison): | ||
self.init(predicate: comparison) | ||
case .compound(let compound): | ||
self.init(predicate: compound) | ||
case .value: | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
public extension BSONDocument { | ||
|
||
init?(predicate: Compound) { | ||
guard predicate.type != .not else { | ||
// { field: { $not: { <operator-expression> } } } | ||
return nil | ||
} | ||
var array = [BSONDocument]() | ||
array.reserveCapacity(predicate.subpredicates.count) | ||
for subpredicate in predicate.subpredicates { | ||
guard let document = BSONDocument(predicate: subpredicate) else { | ||
return nil | ||
} | ||
array.append(document) | ||
} | ||
// { $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] } | ||
// { $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] } | ||
self = [LogicalQueryOperator(predicate: predicate.type).rawValue: .array(array.map { .document($0) })] | ||
} | ||
} | ||
|
||
public extension BSONDocument { | ||
|
||
init?(predicate: Comparison) { | ||
// { <field>: { $eq: <value> } } | ||
return nil | ||
} | ||
} | ||
|
||
public extension BSON { | ||
|
||
init?(predicate: Expression) { | ||
return nil | ||
} | ||
} |