-
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.
OrderedDictionary is now a BSON.Keyspace
- Loading branch information
1 parent
bbcd7ad
commit 9fe63fb
Showing
1 changed file
with
16 additions
and
22 deletions.
There are no files selected for viewing
38 changes: 16 additions & 22 deletions
38
Sources/BSON_OrderedCollections/OrderedDictionary (ext).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 |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} | ||
} |