Skip to content

Commit

Permalink
Merge pull request #134 from tayloraswift/convert-let
Browse files Browse the repository at this point in the history
convert LetDocument to LetEncoder
  • Loading branch information
tayloraswift committed Jun 23, 2024
2 parents 4cda88e + 3aa3f0c commit 7400dd6
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 86 deletions.
32 changes: 4 additions & 28 deletions Sources/MongoBuiltins/Documents/Let/Mongo.LetDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import MongoABI
extension Mongo
{
@frozen public
struct LetDocument:Mongo.EncodableDocument, Sendable
struct LetDocument:Sendable
{
public
var bson:BSON.Document
Expand All @@ -16,32 +16,8 @@ extension Mongo
}
}
}
extension Mongo.LetDocument
extension Mongo.LetDocument:Mongo.EncodableDocument
{
@inlinable public
subscript<Encodable>(path:Mongo.AnyKeyPath) -> Encodable?
where Encodable:BSONEncodable
{
get
{
nil
}
set(value)
{
value?.encode(to: &self.bson[with: path.stem])
}
}
@inlinable public
subscript<Encodable>(`let` binding:Mongo.Variable<some Any>) -> Encodable?
where Encodable:BSONEncodable
{
get
{
nil
}
set(value)
{
value?.encode(to: &self.bson[with: binding.name])
}
}
public
typealias Encoder = Mongo.LetEncoder
}
55 changes: 55 additions & 0 deletions Sources/MongoBuiltins/Documents/Let/Mongo.LetEncoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import BSON
import MongoABI

extension Mongo
{
@frozen public
struct LetEncoder:Sendable
{
@usableFromInline
var bson:BSON.DocumentEncoder<BSON.Key>

@inlinable internal
init(bson:BSON.DocumentEncoder<BSON.Key>)
{
self.bson = bson
}
}
}
extension Mongo.LetEncoder: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 frame:BSON.DocumentFrame { .document }
}
extension Mongo.LetEncoder
{
@inlinable public
subscript(`let` binding:Mongo.Variable<some Any>,
yield:(inout Mongo.ExpressionEncoder) -> ()) -> Void
{
mutating get
{
yield(&self.bson[with: binding.name][as: Mongo.ExpressionEncoder.self])
}
}

@inlinable public
subscript<Encodable>(`let` binding:Mongo.Variable<some Any>) -> Encodable?
where Encodable:BSONEncodable
{
get { nil }
set (value)
{
value?.encode(to: &self.bson[with: binding.name])
}
}
}
19 changes: 11 additions & 8 deletions Sources/MongoBuiltins/Documents/Lookup/Mongo.LookupEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,26 @@ extension Mongo.LookupEncoder
extension Mongo.LookupEncoder
{
@frozen public
enum Let:String, Hashable, Sendable
enum Let:String, Sendable
{
case `let`
}

@inlinable public
subscript(key:Let) -> Mongo.LetDocument?
subscript(key:Let, yield:(inout Mongo.LetEncoder) -> ()) -> Void
{
get
{
nil
}
set(value)
mutating get
{
value?.encode(to: &self.bson[with: key])
yield(&self.bson[with: key][as: Mongo.LetEncoder.self])
}
}

@available(*, unavailable)
@inlinable public
subscript(key:Let) -> Mongo.LetDocument?
{
nil
}
}
extension Mongo.LookupEncoder
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extension Mongo.MergeDocument
{
@frozen public
enum Let:String, Hashable, Sendable
enum Let:String, Sendable
{
case `let`
}
Expand Down
17 changes: 10 additions & 7 deletions Sources/MongoBuiltins/Documents/Merge/Mongo.MergeDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,21 @@ extension Mongo.MergeDocument
}

@inlinable public
subscript(key:Let) -> Mongo.LetDocument?
subscript(key:Let, yield:(inout Mongo.LetEncoder) -> ()) -> Void
{
get
{
nil
}
set(value)
mutating get
{
value?.encode(to: &self.bson[with: key])
yield(&self.bson[with: key][as: Mongo.LetEncoder.self])
}
}

@available(*, unavailable)
@inlinable public
subscript(key:Let) -> Mongo.LetDocument?
{
nil
}

@inlinable public
subscript(key:WhenMatched) -> Mongo.Pipeline?
{
Expand Down
19 changes: 11 additions & 8 deletions Sources/MongoQL/Commands/Aggregate/Mongo.Aggregate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,24 @@ extension Mongo.Aggregate
extension Mongo.Aggregate
{
@frozen public
enum Let:String, Hashable, Sendable
enum Let:String, Sendable
{
case `let`
}

@inlinable public
subscript(key:Let) -> Mongo.LetDocument?
subscript(key:Let, yield:(inout Mongo.LetEncoder) -> ()) -> Void
{
get
mutating get
{
nil
}
set(value)
{
value?.encode(to: &self.fields[with: key])
yield(&self.fields[with: key][as: Mongo.LetEncoder.self])
}
}

@available(*, unavailable)
@inlinable public
subscript(key:Let) -> Mongo.LetDocument?
{
nil
}
}
19 changes: 11 additions & 8 deletions Sources/MongoQL/Commands/Delete/Mongo.Delete.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,24 @@ extension Mongo.Delete
extension Mongo.Delete
{
@frozen public
enum Let:String, Equatable, Hashable, Sendable
enum Let:String, Sendable
{
case `let`
}

@inlinable public
subscript(key:Let) -> Mongo.LetDocument?
subscript(key:Let, yield:(inout Mongo.LetEncoder) -> ()) -> Void
{
get
{
nil
}
set(value)
mutating get
{
value?.encode(to: &self.fields[with: key])
yield(&self.fields[with: key][as: Mongo.LetEncoder.self])
}
}

@available(*, unavailable)
@inlinable public
subscript(key:Let) -> Mongo.LetDocument?
{
nil
}
}
19 changes: 11 additions & 8 deletions Sources/MongoQL/Commands/Find/Mongo.Find.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,26 @@ extension Mongo.Find
extension Mongo.Find
{
@frozen public
enum Let:String, Hashable, Sendable
enum Let:String, Sendable
{
case `let`
}

@inlinable public
subscript(key:Let) -> Mongo.LetDocument?
subscript(key:Let, yield:(inout Mongo.LetEncoder) -> ()) -> Void
{
get
mutating get
{
nil
}
set(value)
{
value?.encode(to: &self.fields[with: key])
yield(&self.fields[with: key][as: Mongo.LetEncoder.self])
}
}

@available(*, unavailable)
@inlinable public
subscript(key:Let) -> Mongo.LetDocument?
{
nil
}
}
extension Mongo.Find
{
Expand Down
19 changes: 11 additions & 8 deletions Sources/MongoQL/Commands/FindAndModify/Mongo.FindAndModify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,26 @@ extension Mongo.FindAndModify
extension Mongo.FindAndModify
{
@frozen public
enum Let:String, Hashable, Sendable
enum Let:String, Sendable
{
case `let`
}

@inlinable public
subscript(key:Let) -> Mongo.LetDocument?
subscript(key:Let, yield:(inout Mongo.LetEncoder) -> ()) -> Void
{
get
mutating get
{
nil
}
set(value)
{
value?.encode(to: &self.fields[with: key])
yield(&self.fields[with: key][as: Mongo.LetEncoder.self])
}
}

@available(*, unavailable)
@inlinable public
subscript(key:Let) -> Mongo.LetDocument?
{
nil
}
}
extension Mongo.FindAndModify
{
Expand Down
19 changes: 11 additions & 8 deletions Sources/MongoQL/Commands/Update/Mongo.Update.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,24 @@ extension Mongo.Update
extension Mongo.Update
{
@frozen public
enum Let:String, Hashable, Sendable
enum Let:String, Sendable
{
case `let`
}

@inlinable public
subscript(key:Let) -> Mongo.LetDocument?
subscript(key:Let, yield:(inout Mongo.LetEncoder) -> ()) -> Void
{
get
{
nil
}
set(value)
mutating get
{
value?.encode(to: &self.fields[with: key])
yield(&self.fields[with: key][as: Mongo.LetEncoder.self])
}
}

@available(*, unavailable)
@inlinable public
subscript(key:Let) -> Mongo.LetDocument?
{
nil
}
}
13 changes: 11 additions & 2 deletions Sources/MongoQL/Commands/Update/Mongo.UpdateStatementEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,20 @@ extension Mongo.UpdateStatementEncoder
case c
}

@inlinable public
subscript(key:C, yield:(inout Mongo.LetEncoder) -> ()) -> Void
{
mutating get
{
yield(&self.bson[with: key][as: Mongo.LetEncoder.self])
}
}

@available(*, unavailable)
@inlinable public
subscript(key:C) -> Mongo.LetDocument?
{
get { nil }
set (value) { value?.encode(to: &self.bson[with: key]) }
nil
}
}
extension Mongo.UpdateStatementEncoder
Expand Down

0 comments on commit 7400dd6

Please sign in to comment.