Skip to content

Commit

Permalink
final touches on iteration #1 of compatibility warnings for aggregati…
Browse files Browse the repository at this point in the history
…on stages vs. connection MongoDB version
  • Loading branch information
linus-hologram committed Feb 4, 2022
1 parent c2e02f8 commit 7bfa630
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Sources/MongoKitten/Aggregate.swift
Expand Up @@ -66,9 +66,9 @@ public struct AggregateBuilderPipeline: QueryCursor {

#if DEBUG

let minimalVersionRequired = stages.compactMap { \.minimalVersionRequired }.max()
let minimalVersionRequired = stages.compactMap(\.minimalVersionRequired).max()

if let actualVersion = connection.wireVersion, let minimalVersion = minimalVersionRequired, actualVersion < minimalVersion {
if let actualVersion = await connection.wireVersion, let minimalVersion = minimalVersionRequired, actualVersion < minimalVersion {
connection.logger.warning("Aggregation might fail since one or more aggregation stages require a higher MongoDB version than provided by the current connection.")
}

Expand Down
17 changes: 1 addition & 16 deletions Sources/MongoKitten/AggregateStage.swift
Expand Up @@ -39,24 +39,9 @@ public struct AggregateBuilderStage {
}

public static func project(_ projection: Projection) -> AggregateBuilderStage {
func valueUnwrapper(_ value: Primitive) -> WireVersion?{
switch value {
case let value as Int32:
(value == 0) ? return .mongo3_4 : return nil // indicates excluded fields
case let value as String:
(value == "$$REMOVE") ? return .mongo3_6 : return nil // indicates conditionally excluded fields
case let value as Document:
return valueUnwrapper(value)
default:
return nil
}
}

let minimalVersionRequired: WireVersion? = projection.document.values.compactMap{ valueUnwrapper($0) }.min()

return AggregateBuilderStage(document: [
"$project": projection.document
], minimalVersion: minimalVersionRequired)
], minimalVersion: projection.minimalVersion)
}

public static func project(_ fields: String...) -> AggregateBuilderStage {
Expand Down
19 changes: 19 additions & 0 deletions Sources/MongoKittenCore/QueryPrimitives/Projection.swift
Expand Up @@ -2,9 +2,28 @@ import BSON

public struct Projection: Encodable, ExpressibleByDictionaryLiteral {
public internal(set) var document: Document
public internal(set) var minimalVersion: WireVersion? {
func valueUnwrapper(_ value: Primitive) -> WireVersion?{
switch value {
case let value as Int32:
return value == 0 ? .mongo3_4 : nil // indicates excluded fields
case let value as String:
return value == "$$REMOVE" ? .mongo3_6 : nil // indicates conditionally excluded fields
case let value as Document:
return value.values.compactMap(valueUnwrapper).max()
default:
return nil
}
}

return self.document.values.compactMap(valueUnwrapper).max()
}

/// An expression that can be specified to either include or exclude a field (or some custom value)
public enum ProjectionExpression: ExpressibleByBooleanLiteral, ExpressibleByStringLiteral, ExpressibleByDictionaryLiteral, PrimitiveConvertible {
/// Conditional exclusion of a field from the projection
public static let remove = ProjectionExpression.custom("$$REMOVE")

/// Creates a BSON.Primitive of this ProjectionExpression for easy embedding in Documents
public func makePrimitive() -> Primitive? {
switch self {
Expand Down

0 comments on commit 7bfa630

Please sign in to comment.