Skip to content

Commit

Permalink
Merge pull request #127 from tayloraswift/update-array-operators
Browse files Browse the repository at this point in the history
support array update operators
  • Loading branch information
tayloraswift authored May 14, 2024
2 parents a74c64d + f9d1122 commit b20c0b4
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 80 deletions.
3 changes: 3 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,10 @@ for target:PackageDescription.Target in package.targets

settings.append(.enableUpcomingFeature("BareSlashRegexLiterals"))
settings.append(.enableUpcomingFeature("ConciseMagicFile"))
settings.append(.enableUpcomingFeature("DeprecateApplicationMain"))
settings.append(.enableUpcomingFeature("ExistentialAny"))
settings.append(.enableUpcomingFeature("GlobalConcurrency"))
settings.append(.enableUpcomingFeature("IsolatedDefaultValues"))
settings.append(.enableExperimentalFeature("StrictConcurrency"))

$0 = settings
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div align="center">

***`mongodb`***<br>`0.14`
***`mongodb`***<br>`0.15`

[![ci status](https://github.com/tayloraswift/swift-mongodb/actions/workflows/ci.yml/badge.svg)](https://github.com/tayloraswift/swift-mongodb/actions/workflows/ci.yml)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension Mongo.PredicateOperatorEncoder:BSON.Encoder
extension Mongo.PredicateOperatorEncoder
{
@frozen public
enum Binary:String, Hashable, Sendable
enum Binary:String, Sendable
{
case bitsAllClear = "$bitsAllClear"
case bitsAllSet = "$bitsAllSet"
Expand Down Expand Up @@ -67,7 +67,7 @@ extension Mongo.PredicateOperatorEncoder
extension Mongo.PredicateOperatorEncoder
{
@frozen public
enum Exists:String, Hashable, Sendable
enum Exists:String, Sendable
{
case exists = "$exists"
}
Expand All @@ -88,7 +88,7 @@ extension Mongo.PredicateOperatorEncoder
extension Mongo.PredicateOperatorEncoder
{
@frozen public
enum Metatype:String, Hashable, Sendable
enum Metatype:String, Sendable
{
case type = "$type"
}
Expand Down Expand Up @@ -121,7 +121,7 @@ extension Mongo.PredicateOperatorEncoder
extension Mongo.PredicateOperatorEncoder
{
@frozen public
enum Mod:String, Hashable, Sendable
enum Mod:String, Sendable
{
case mod = "$mod"
}
Expand Down Expand Up @@ -154,7 +154,7 @@ extension Mongo.PredicateOperatorEncoder
extension Mongo.PredicateOperator
{
@frozen public
enum Recursive:String, Hashable, Sendable
enum Recursive:String, Sendable
{
case not = "$not"
case any = "$elemMatch"
Expand All @@ -180,7 +180,7 @@ extension Mongo.PredicateOperator
extension Mongo.PredicateOperatorEncoder
{
@frozen public
enum Regex:String, Hashable, Sendable
enum Regex:String, Sendable
{
case regex = "$regex"
}
Expand All @@ -201,7 +201,7 @@ extension Mongo.PredicateOperatorEncoder
extension Mongo.PredicateOperatorEncoder
{
@frozen public
enum Variadic:String, Hashable, Sendable
enum Variadic:String, Sendable
{
case all = "$all"
case `in` = "$in"
Expand Down
22 changes: 22 additions & 0 deletions Sources/MongoBuiltins/Documents/Update/Mongo.UpdateArray.swift
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
}
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])
}
}
}
36 changes: 27 additions & 9 deletions Sources/MongoBuiltins/Documents/Update/Mongo.UpdateEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extension Mongo.UpdateEncoder
extension Mongo.UpdateEncoder
{
@frozen public
enum Assignment:String, Hashable, Sendable
enum Assignment:String, Sendable, Mongo.UpdateValueOperator
{
case set = "$set"
case setOnInsert = "$setOnInsert"
Expand Down Expand Up @@ -86,7 +86,7 @@ extension Mongo.UpdateEncoder
extension Mongo.UpdateEncoder
{
@frozen public
enum Bit:String, Hashable, Sendable
enum Bit:String, Sendable
{
case bit = "$bit"
}
Expand All @@ -104,7 +104,7 @@ extension Mongo.UpdateEncoder
extension Mongo.UpdateEncoder
{
@frozen public
enum CurrentDate:String, Hashable, Sendable
enum CurrentDate:String, Sendable
{
case currentDate = "$currentDate"
}
Expand All @@ -122,7 +122,7 @@ extension Mongo.UpdateEncoder
extension Mongo.UpdateEncoder
{
@frozen public
enum Pop:String, Hashable, Sendable
enum Pop:String, Sendable
{
case pop = "$pop"
}
Expand All @@ -140,7 +140,7 @@ extension Mongo.UpdateEncoder
extension Mongo.UpdateEncoder
{
@frozen public
enum Pull:String, Hashable, Sendable
enum Pull:String, Sendable, Mongo.UpdateValueOperator
{
case pull = "$pull"
}
Expand All @@ -158,9 +158,8 @@ extension Mongo.UpdateEncoder
extension Mongo.UpdateEncoder
{
@frozen public
enum Reduction:String, Hashable, Sendable
enum Reduction:String, Sendable, Mongo.UpdateValueOperator
{
case addToSet = "$addToSet"
case max = "$max"
case min = "$min"
// $pullAll is a reduction, it only accepts field values that form
Expand All @@ -181,7 +180,7 @@ extension Mongo.UpdateEncoder
extension Mongo.UpdateEncoder
{
@frozen public
enum Rename:String, Hashable, Sendable
enum Rename:String, Sendable
{
case rename = "$rename"
}
Expand All @@ -197,12 +196,31 @@ extension Mongo.UpdateEncoder
}
}
extension Mongo.UpdateEncoder
{
@frozen public
enum ArrayUnion:String, Sendable, Mongo.UpdateValueOperator
{
case addToSet = "$addToSet"
case push = "$push"
}

@inlinable public
subscript(key:ArrayUnion,
yield:(inout Mongo.UpdateFieldsEncoder<ArrayUnion>) -> ()) -> Void
{
mutating get
{
yield(&self.bson[with: key][as: Mongo.UpdateFieldsEncoder<ArrayUnion>.self])
}
}
}
extension Mongo.UpdateEncoder
{
/// Takes a document and removes the specified fields.
/// Not to be confused with the ``Mongo.Pipeline.Unset/unset``
/// aggregation pipeline stage, which can take a field path directly.
@frozen public
enum Unset:String, Hashable, Sendable
enum Unset:String, Sendable
{
case unset = "$unset"
}
Expand Down
Loading

0 comments on commit b20c0b4

Please sign in to comment.