forked from mongodb/swift-bson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBSONDocument+Collection.swift
60 lines (52 loc) · 2.37 KB
/
BSONDocument+Collection.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import Foundation
/// An extension of `BSONDocument` to make it conform to the `Collection` protocol.
/// This gives guarantees on non-destructive iteration, and offers an indexed
/// ordering to the key-value pairs in the document.
extension BSONDocument: Collection {
/// The index type of a document.
public typealias Index = Int
/// Returns the start index of the Document.
public var startIndex: Index {
0
}
/// Returns the end index of the Document.
public var endIndex: Index {
self.count
}
private func failIndexCheck(_ i: Index) {
let invalidIndexMsg = "Index \(i) is invalid"
guard !self.isEmpty && self.startIndex...self.endIndex - 1 ~= i else {
fatalError(invalidIndexMsg)
}
}
/// Returns the index after the given index for this Document.
public func index(after i: Index) -> Index {
// Index must be a valid one, meaning it must exist somewhere in self.keys.
self.failIndexCheck(i)
return i + 1
}
/// Allows access to a `KeyValuePair` from the `BSONDocument`, given the position of the desired `KeyValuePair` held
/// within. This method does not guarantee constant-time (O(1)) access.
public subscript(position: Index) -> KeyValuePair {
// TODO: This method _should_ guarantee constant-time O(1) access, and it is possible to make it do so. This
// criticism also applies to key-based subscripting via `String`.
// See SWIFT-250.
self.failIndexCheck(position)
let iter = BSONDocumentIterator(over: self)
for pos in 0..<position {
guard (try? iter.nextThrowing()) != nil else {
fatalError("Failed to advance iterator to position \(pos)")
}
}
guard let (k, v) = iter.next() else {
fatalError("Failed get current key and value at \(position)")
}
return (k, v)
}
/// Allows access to a `KeyValuePair` from the `BSONDocument`, given a range of indices of the desired
/// `KeyValuePair`'s held within. This method does not guarantee constant-time (O(1)) access.
public subscript(bounds: Range<Index>) -> BSONDocument {
// TODO: SWIFT-252 should provide a more efficient implementation for this.
BSONDocumentIterator.subsequence(of: self, startIndex: bounds.lowerBound, endIndex: bounds.upperBound)
}
}