-
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.
Merge pull request #127 from tayloraswift/update-array-operators
support array update operators
- Loading branch information
Showing
10 changed files
with
215 additions
and
80 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
22 changes: 22 additions & 0 deletions
22
Sources/MongoBuiltins/Documents/Update/Mongo.UpdateArray.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,22 @@ | ||
import BSON | ||
|
||
extension Mongo | ||
{ | ||
@frozen public | ||
struct UpdateArray:BSONRepresentable, BSONDecodable, BSONEncodable, Sendable | ||
{ | ||
public | ||
var bson:BSON.Document | ||
|
||
@inlinable public | ||
init(_ bson:BSON.Document) | ||
{ | ||
self.bson = bson | ||
} | ||
} | ||
} | ||
extension Mongo.UpdateArray:Mongo.EncodableDocument | ||
{ | ||
public | ||
typealias Encoder = Mongo.UpdateArrayEncoder | ||
} |
92 changes: 92 additions & 0 deletions
92
Sources/MongoBuiltins/Documents/Update/Mongo.UpdateArrayEncoder.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,92 @@ | ||
import BSON | ||
|
||
extension Mongo | ||
{ | ||
@frozen public | ||
struct UpdateArrayEncoder:Sendable | ||
{ | ||
@usableFromInline | ||
var bson:BSON.DocumentEncoder<BSON.Key> | ||
|
||
@inlinable internal | ||
init(bson:BSON.DocumentEncoder<BSON.Key>) | ||
{ | ||
self.bson = bson | ||
} | ||
} | ||
} | ||
extension Mongo.UpdateArrayEncoder: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.UpdateArrayEncoder | ||
{ | ||
@frozen public | ||
enum Each:String, Sendable | ||
{ | ||
case each = "$each" | ||
} | ||
|
||
@inlinable public | ||
subscript<Array>(key:Each) -> Array? where Array:BSONEncodable | ||
{ | ||
get | ||
{ | ||
nil | ||
} | ||
set(value) | ||
{ | ||
value?.encode(to: &self.bson[with: key]) | ||
} | ||
} | ||
} | ||
extension Mongo.UpdateArrayEncoder | ||
{ | ||
@frozen public | ||
enum Index:String, Sendable | ||
{ | ||
case position = "$position" | ||
case sort = "$sort" | ||
} | ||
|
||
@inlinable public | ||
subscript(key:Index) -> Int? | ||
{ | ||
get | ||
{ | ||
nil | ||
} | ||
set(value) | ||
{ | ||
value?.encode(to: &self.bson[with: key]) | ||
} | ||
} | ||
} | ||
extension Mongo.UpdateArrayEncoder | ||
{ | ||
@frozen public | ||
enum Sort:String, Sendable | ||
{ | ||
case sort = "$sort" | ||
} | ||
|
||
@inlinable public | ||
subscript(key:Sort, yield:(inout Mongo.SortEncoder) -> ()) -> Void | ||
{ | ||
mutating get | ||
{ | ||
yield(&self.bson[with: key][as: Mongo.SortEncoder.self]) | ||
} | ||
} | ||
} |
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.