-
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.
rewrite projection API to use ProjectionEncoder and ProjectionEncodable
- Loading branch information
1 parent
a67e2cd
commit 377202e
Showing
18 changed files
with
423 additions
and
180 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
8 changes: 8 additions & 0 deletions
8
Sources/MongoBuiltins/Documents/Predicate/Mongo.PredicateEncodable.swift
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,8 @@ | ||
extension Mongo | ||
{ | ||
public | ||
protocol PredicateEncodable | ||
{ | ||
func encode(to predicate:inout PredicateEncoder) | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
Sources/MongoBuiltins/Documents/Projection/Mongo.ProjectionEncodable.swift
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,8 @@ | ||
extension Mongo | ||
{ | ||
public | ||
protocol ProjectionEncodable | ||
{ | ||
func encode(to projection:inout ProjectionEncoder) | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
Sources/MongoBuiltins/Documents/Projection/Mongo.ProjectionEncoder.swift
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,138 @@ | ||
import BSON | ||
import MongoABI | ||
|
||
extension Mongo | ||
{ | ||
@frozen public | ||
struct ProjectionEncoder:Sendable | ||
{ | ||
@usableFromInline | ||
var bson:BSON.DocumentEncoder<Mongo.AnyKeyPath> | ||
|
||
@inlinable internal | ||
init(bson:BSON.DocumentEncoder<Mongo.AnyKeyPath>) | ||
{ | ||
self.bson = bson | ||
} | ||
} | ||
} | ||
extension Mongo.ProjectionEncoder:BSON.Encoder | ||
{ | ||
@inlinable public | ||
init(_ output:consuming BSON.Output) { self.init(bson: .init(output)) } | ||
|
||
@inlinable public consuming | ||
func move() -> BSON.Output { self.bson.move() } | ||
|
||
@inlinable public static | ||
var type:BSON.AnyType { .document } | ||
} | ||
|
||
extension Mongo.ProjectionEncoder | ||
{ | ||
@available(*, unavailable, message: "Use the boolean subscript instead.") | ||
@inlinable public | ||
subscript(path:Mongo.AnyKeyPath) -> Int? | ||
{ | ||
get | ||
{ | ||
nil | ||
} | ||
set(value) | ||
{ | ||
value?.encode(to: &self.bson[with: path]) | ||
} | ||
} | ||
|
||
@inlinable public | ||
subscript(path:Mongo.AnyKeyPath) -> Bool? | ||
{ | ||
get | ||
{ | ||
nil | ||
} | ||
set(value) | ||
{ | ||
value?.encode(to: &self.bson[with: path]) | ||
} | ||
} | ||
|
||
/// Encodes a nested projection document. | ||
@inlinable public | ||
subscript(path:Mongo.AnyKeyPath, yield:(inout Mongo.ProjectionEncoder) -> ()) -> Void | ||
{ | ||
mutating get | ||
{ | ||
yield(&self.bson[with: path][as: Mongo.ProjectionEncoder.self]) | ||
} | ||
} | ||
|
||
/// Encodes a projection expression. | ||
@inlinable public | ||
subscript(path:Mongo.AnyKeyPath, yield:(inout Mongo.ExpressionEncoder) -> ()) -> Void | ||
{ | ||
mutating get | ||
{ | ||
yield(&self.bson[with: path][as: Mongo.ExpressionEncoder.self]) | ||
} | ||
} | ||
|
||
/// Encodes a projection operator. | ||
@inlinable public | ||
subscript(path:Mongo.AnyKeyPath, | ||
yield:(inout Mongo.ProjectionOperatorEncoder) -> ()) -> Void | ||
{ | ||
mutating get | ||
{ | ||
yield(&self.bson[with: path][as: Mongo.ProjectionOperatorEncoder.self]) | ||
} | ||
} | ||
|
||
/// Encodes a projection operator from a model type. | ||
@inlinable public | ||
subscript<Operator>(path:Mongo.AnyKeyPath) -> Operator? | ||
where Operator:Mongo.ProjectionOperatorEncodable | ||
{ | ||
get | ||
{ | ||
nil | ||
} | ||
set(value) | ||
{ | ||
value?.encode(to: &self.bson[with: path][as: Mongo.ProjectionOperatorEncoder.self]) | ||
} | ||
} | ||
} | ||
@available(*, deprecated) | ||
extension Mongo.ProjectionEncoder | ||
{ | ||
/// Encodes a ``ProjectionOperator``. | ||
/// | ||
/// This does not require `@_disfavoredOverload`, because ``ProjectionOperator`` has no | ||
/// subscripts that accept string literals, so it will never conflict with | ||
/// ``BSON.Document``. | ||
@inlinable public | ||
subscript(path:Mongo.AnyKeyPath) -> Mongo.ProjectionOperator? | ||
{ | ||
get | ||
{ | ||
nil | ||
} | ||
set(value) | ||
{ | ||
value?.encode(to: &self.bson[with: path.stem]) | ||
} | ||
} | ||
@inlinable public | ||
subscript<Encodable>(path:Mongo.AnyKeyPath) -> Encodable? where Encodable:BSONEncodable | ||
{ | ||
get | ||
{ | ||
nil | ||
} | ||
set(value) | ||
{ | ||
value?.encode(to: &self.bson[with: path.stem]) | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
Sources/MongoBuiltins/Documents/Projection/Mongo.ProjectionMetadata.swift
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,14 @@ | ||
import BSON | ||
|
||
extension Mongo | ||
{ | ||
@frozen public | ||
enum ProjectionMetadata:String, Hashable, Sendable | ||
{ | ||
case textScore | ||
case indexKey | ||
} | ||
} | ||
extension Mongo.ProjectionMetadata:BSONDecodable, BSONEncodable | ||
{ | ||
} |
17 changes: 0 additions & 17 deletions
17
Sources/MongoBuiltins/Documents/Projection/Mongo.ProjectionOperator.First.swift
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
Sources/MongoBuiltins/Documents/Projection/Mongo.ProjectionOperator.Meta.swift
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
Sources/MongoBuiltins/Documents/Projection/Mongo.ProjectionOperator.Metadata.swift
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
Sources/MongoBuiltins/Documents/Projection/Mongo.ProjectionOperator.Slice.swift
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.