Skip to content

Commit

Permalink
add subscript labels to pipeline stages, since this is causing so man…
Browse files Browse the repository at this point in the history
…y mistakes. also move update statements towards functional API
  • Loading branch information
tayloraswift committed Feb 6, 2024
1 parent 93f54dd commit a59c9cf
Show file tree
Hide file tree
Showing 47 changed files with 1,840 additions and 938 deletions.
2 changes: 1 addition & 1 deletion Sources/BSONABI/Fields/BSON.Key.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ extension BSON.Key
///
/// Some applications that use BSON, such as MongoDB, consider `.`
/// characters significant.
@available(*, deprecated, message: "Prefer 'Mongo.KeyPath' instead.")
@available(*, deprecated, message: "Prefer 'Mongo.AnyKeyPath' instead.")
@inlinable public static
func / (self:Self, next:Self) -> Self
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import BSON

extension Mongo
{
@available(*, deprecated, renamed: "AnyKeyPath")
public
typealias KeyPath = AnyKeyPath

@frozen public
struct KeyPath:Equatable, Hashable, Sendable
struct AnyKeyPath:Equatable, Hashable, Sendable
{
public
var stem:BSON.Key
Expand All @@ -15,34 +19,37 @@ extension Mongo
}
}
}
extension Mongo.KeyPath
extension Mongo.AnyKeyPath:RawRepresentable
{
/// See ``rawValue``.
@inlinable public
init(rawValue:String)
{
self.init(stem: .init(rawValue: rawValue))
}

/// The key path stem, which is the entire key path minus the leading `$` character that
/// would normally appear when encoding the key path in an expression.
@inlinable public
var rawValue:String { self.stem.rawValue }
}
extension Mongo.KeyPath:ExpressibleByStringLiteral
extension Mongo.AnyKeyPath:ExpressibleByStringLiteral
{
@inlinable public
init(stringLiteral:String)
{
self.init(rawValue: stringLiteral)
}
}
extension Mongo.KeyPath:CustomStringConvertible
extension Mongo.AnyKeyPath:CustomStringConvertible
{
@inlinable public
var description:String { "$\(self.stem)" }
}
extension Mongo.KeyPath:BSONStringEncodable
extension Mongo.AnyKeyPath:BSONStringEncodable
{
}
extension Mongo.KeyPath
extension Mongo.AnyKeyPath
{
/// Creates a key path by joining two key paths with a `.` character.
@inlinable public static
Expand Down
4 changes: 2 additions & 2 deletions Sources/MongoABI/Mongo.Variable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ extension Mongo
extension Mongo.Variable where T:MongoMasterCodingModel
{
@inlinable public
subscript(key:T.CodingKey) -> Mongo.KeyPath
subscript(key:T.CodingKey) -> Mongo.AnyKeyPath
{
// When the key path is encoded, ``Mongo.KeyPath``
// When the key path is encoded, ``Mongo.AnyKeyPath``
// will add an additional prefixed dollar sign.
.init(rawValue: "$\(self.name).\(key.rawValue)")
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/MongoABI/MongoMasterCodingModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protocol MongoMasterCodingModel<CodingKey>
extension MongoMasterCodingModel
{
@inlinable public static
subscript(key:CodingKey) -> Mongo.KeyPath
subscript(key:CodingKey) -> Mongo.AnyKeyPath
{
.init(rawValue: key.rawValue)
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/MongoBuiltins/BSONEncodable (ext).swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import BSON
extension BSONEncodable where Self == Mongo.Expression
{
@inlinable public static
func expr(with populate:(inout Self) throws -> ()) rethrows -> Self
func expr(with populate:(inout Mongo.ExpressionEncoder) throws -> ()) rethrows -> Self
{
var expr:Self = .init(.init())
try populate(&expr)
return expr
var document:BSON.Document = .init()
try populate(&document.output[as: Mongo.ExpressionEncoder.self])
return .init(document)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Mongo
extension Mongo.BucketOutputDocument
{
@inlinable public
subscript(path:Mongo.KeyPath) -> Mongo.Accumulator?
subscript(path:Mongo.AnyKeyPath) -> Mongo.Accumulator?
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Mongo
extension Mongo.FacetDocument
{
@inlinable public
subscript(path:Mongo.KeyPath) -> Mongo.Pipeline?
subscript(path:Mongo.AnyKeyPath) -> Mongo.Pipeline?
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Mongo
extension Mongo.GroupDocument
{
@inlinable public
subscript(path:Mongo.KeyPath) -> Mongo.Accumulator?
subscript(path:Mongo.AnyKeyPath) -> Mongo.Accumulator?
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Mongo
extension Mongo.LetDocument
{
@inlinable public
subscript<Encodable>(path:Mongo.KeyPath) -> Encodable?
subscript<Encodable>(path:Mongo.AnyKeyPath) -> Encodable?
where Encodable:BSONEncodable
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Mongo
extension Mongo.LookupDocument
{
@inlinable public
subscript(key:Field) -> Mongo.KeyPath?
subscript(key:Field) -> Mongo.AnyKeyPath?
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension Mongo.MergeDocument
}

@inlinable public
subscript(key:On) -> Mongo.KeyPath?
subscript(key:On) -> Mongo.AnyKeyPath?
{
get
{
Expand Down
5 changes: 5 additions & 0 deletions Sources/MongoBuiltins/Documents/Mongo.AnyKeyPath (ext).swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import MongoABI

extension Mongo.AnyKeyPath:_MongoExpressionRestrictedEncodable
{
}
5 changes: 0 additions & 5 deletions Sources/MongoBuiltins/Documents/Mongo.KeyPath (ext).swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extension Mongo.PredicateDocument
/// ``PredicateOperator`` has no subscripts that accept string
/// literals, so it will never conflict with ``BSON.Document``.
@inlinable public
subscript(path:Mongo.KeyPath) -> Mongo.PredicateOperator?
subscript(path:Mongo.AnyKeyPath) -> Mongo.PredicateOperator?
{
get
{
Expand All @@ -42,7 +42,7 @@ extension Mongo.PredicateDocument
}
}
@inlinable public
subscript(path:Mongo.KeyPath) -> Self?
subscript(path:Mongo.AnyKeyPath) -> Self?
{
get
{
Expand All @@ -57,7 +57,7 @@ extension Mongo.PredicateDocument
// ``_MongoExpressionRestrictedEncodable``-gated diagnostic subscript.
@_disfavoredOverload
@inlinable public
subscript<Encodable>(path:Mongo.KeyPath) -> Encodable? where Encodable:BSONEncodable
subscript<Encodable>(path:Mongo.AnyKeyPath) -> Encodable? where Encodable:BSONEncodable
{
get
{
Expand All @@ -76,7 +76,7 @@ extension Mongo.PredicateDocument
even in a '$match' stage of an aggregation pipeline.
""")
public
subscript<Expression>(path:Mongo.KeyPath) -> Expression?
subscript<Expression>(path:Mongo.AnyKeyPath) -> Expression?
where Expression:_MongoExpressionRestrictedEncodable
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension Mongo.ProjectionDocument
/// ``ProjectionOperator`` has no subscripts that accept string
/// literals, so it will never conflict with ``BSON.Document``.
@inlinable public
subscript(path:Mongo.KeyPath) -> Mongo.ProjectionOperator?
subscript(path:Mongo.AnyKeyPath) -> Mongo.ProjectionOperator?
{
get
{
Expand All @@ -36,7 +36,7 @@ extension Mongo.ProjectionDocument
}
}
@inlinable public
subscript<Encodable>(path:Mongo.KeyPath) -> Encodable? where Encodable:BSONEncodable
subscript<Encodable>(path:Mongo.AnyKeyPath) -> Encodable? where Encodable:BSONEncodable
{
get
{
Expand Down
4 changes: 2 additions & 2 deletions Sources/MongoBuiltins/Documents/Set/Mongo.SetDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ extension Mongo
extension Mongo.SetDocument
{
@inlinable public
subscript<Encodable>(path:Mongo.KeyPath) -> Encodable? where Encodable:BSONEncodable
subscript<Encodable>(path:Mongo.AnyKeyPath) -> Encodable? where Encodable:BSONEncodable
{
get
{
nil
}
set(value)
{
value?.encode(to: &self.bson[with: path.stem])
value?.encode(to: &self.bson[with: path])
}
}
}
8 changes: 4 additions & 4 deletions Sources/MongoBuiltins/Documents/Sort/Mongo.SortDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension Mongo.SortDocument
@available(*, unavailable,
message: "pass the `(+)` or `(-)` operator functions to specify sort direction.")
@inlinable public
subscript(path:Mongo.KeyPath) -> Int?
subscript(path:Mongo.AnyKeyPath) -> Int?
{
get
{
Expand Down Expand Up @@ -50,7 +50,7 @@ extension Mongo.SortDocument
}
}
@inlinable public
subscript(path:Mongo.KeyPath) -> ((Mongo.SortAscending) -> Never)?
subscript(path:Mongo.AnyKeyPath) -> ((Mongo.SortAscending) -> Never)?
{
get
{
Expand Down Expand Up @@ -81,7 +81,7 @@ extension Mongo.SortDocument
}
}
@inlinable public
subscript(path:Mongo.KeyPath) -> ((Mongo.SortDescending) -> Never)?
subscript(path:Mongo.AnyKeyPath) -> ((Mongo.SortDescending) -> Never)?
{
get
{
Expand All @@ -96,7 +96,7 @@ extension Mongo.SortDocument
}
}
@inlinable public
subscript(path:Mongo.KeyPath) -> Mongo.SortOperator?
subscript(path:Mongo.AnyKeyPath) -> Mongo.SortOperator?
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension Mongo
extension Mongo.UnwindDocument
{
@inlinable public
subscript(key:Field) -> Mongo.KeyPath?
subscript(key:Field) -> Mongo.AnyKeyPath?
{
get
{
Expand Down
Loading

0 comments on commit a59c9cf

Please sign in to comment.