Skip to content

Commit

Permalink
OrderedDictionary is now a BSON.Keyspace
Browse files Browse the repository at this point in the history
  • Loading branch information
tayloraswift committed Jan 14, 2025
1 parent bbcd7ad commit 9fe63fb
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions Sources/BSON_OrderedCollections/OrderedDictionary (ext).swift
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
import BSON
import OrderedCollections

extension OrderedDictionary:@retroactive BSONDecodable
where Key == BSON.Key, Value:BSONDecodable
extension OrderedDictionary:@retroactive BSONEncodable, @retroactive BSONDocumentEncodable
where Key:BSON.Keyspace, Value:BSONEncodable
{
@inlinable public
init(bson:BSON.AnyValue) throws
func encode(to bson:inout BSON.DocumentEncoder<Key>)
{
try self.init(bson: try .init(bson: consume bson))
}
@inlinable public
init(bson:BSON.Document) throws
{
self.init()
try bson.parse
for (key, value):(Key, Value) in self.elements
{
(field:BSON.FieldDecoder<BSON.Key>) in

if case _? = self.updateValue(try field.decode(to: Value.self),
forKey: field.key)
{
throw BSON.DocumentKeyError<BSON.Key>.duplicate(field.key)
}
value.encode(to: &bson[key])
}
}
}
extension OrderedDictionary:@retroactive BSONDocumentEncodable, @retroactive BSONEncodable
where Key == BSON.Key, Value:BSONEncodable
extension OrderedDictionary:@retroactive BSONDecodable, @retroactive BSONKeyspaceDecodable
where Key:BSON.Keyspace, Value:BSONDecodable
{
@inlinable public
func encode(to bson:inout BSON.DocumentEncoder<BSON.Key>)
init(bson:consuming BSON.KeyspaceDecoder<Key>) throws
{
for (key, value):(Key, Value) in self.elements
self.init()
while let field:BSON.FieldDecoder<Key> = try bson[+]
{
bson[key] = value
guard
case nil = self.updateValue(try field.decode(to: Value.self), forKey: field.key)
else
{
throw BSON.DocumentKeyError<Key>.duplicate(field.key)
}
}
}
}

0 comments on commit 9fe63fb

Please sign in to comment.