Skip to content

Commit afa61b5

Browse files
authored
SWIFT-935 Fix API breakages from old BSON library (#45)
1 parent b8d953a commit afa61b5

File tree

7 files changed

+20
-14
lines changed

7 files changed

+20
-14
lines changed

Sources/SwiftBSON/BSONBinary.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public struct BSONBinary: Equatable, Hashable {
1010
public let subtype: Subtype
1111

1212
/// Subtypes for BSON Binary values.
13-
public struct Subtype: Equatable, Hashable, RawRepresentable {
13+
public struct Subtype: Equatable, Hashable, RawRepresentable, Codable {
1414
// swiftlint:disable force_unwrapping
1515
/// Generic binary subtype
1616
public static let generic = Subtype(rawValue: 0x00)!

Sources/SwiftBSON/BSONDocument+Collection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extension BSONDocument: Collection {
3838
// criticism also applies to key-based subscripting via `String`.
3939
// See SWIFT-250.
4040
self.failIndexCheck(position)
41-
var iter = BSONDocumentIterator(over: self)
41+
let iter = BSONDocumentIterator(over: self)
4242

4343
for pos in 0..<position {
4444
guard (try? iter.nextThrowing()) != nil else {

Sources/SwiftBSON/BSONDocument.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public struct BSONDocument {
259259
return
260260
}
261261

262-
var iter = BSONDocumentIterator(over: self.storage.buffer)
262+
let iter = BSONDocumentIterator(over: self.storage.buffer)
263263

264264
guard let range = iter.findByteRange(for: key) else {
265265
throw BSONError.InternalError(message: "Cannot find \(key) to delete")
@@ -342,7 +342,7 @@ public struct BSONDocument {
342342
)
343343
}
344344

345-
var iter = BSONDocumentIterator(over: self.buffer)
345+
let iter = BSONDocumentIterator(over: self.buffer)
346346
// Implicitly validate with iterator
347347
do {
348348
while let (_, value) = try iter.nextThrowing() {

Sources/SwiftBSON/BSONDocumentIterator.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import NIO
33

44
/// Iterator over a `BSONDocument`. This type is not meant to be used directly; please use `Sequence` protocol methods
55
/// instead.
6-
public struct BSONDocumentIterator: IteratorProtocol {
6+
public class BSONDocumentIterator: IteratorProtocol {
77
/// The buffer we are iterating over.
88
private var buffer: ByteBuffer
99
private var exhausted: Bool
@@ -15,12 +15,12 @@ public struct BSONDocumentIterator: IteratorProtocol {
1515
self.buffer.moveReaderIndex(to: 4)
1616
}
1717

18-
internal init(over doc: BSONDocument) {
19-
self = BSONDocumentIterator(over: doc.buffer)
18+
internal convenience init(over doc: BSONDocument) {
19+
self.init(over: doc.buffer)
2020
}
2121

2222
/// Advances to the next element and returns it, or nil if no next element exists.
23-
public mutating func next() -> BSONDocument.KeyValuePair? {
23+
public func next() -> BSONDocument.KeyValuePair? {
2424
// The only time this would crash is when the document is incorrectly formatted
2525
do {
2626
return try self.nextThrowing()
@@ -34,7 +34,7 @@ public struct BSONDocumentIterator: IteratorProtocol {
3434
* - Throws:
3535
* - `InternalError` if the underlying buffer contains invalid BSON
3636
*/
37-
internal mutating func nextThrowing() throws -> BSONDocument.KeyValuePair? {
37+
internal func nextThrowing() throws -> BSONDocument.KeyValuePair? {
3838
guard self.buffer.readableBytes != 0 else {
3939
// Iteration has been exhausted
4040
guard self.exhausted else {
@@ -88,7 +88,7 @@ public struct BSONDocumentIterator: IteratorProtocol {
8888

8989
/// Finds the key in the underlying buffer, and returns the [startIndex, endIndex) containing the corresponding
9090
/// element.
91-
internal mutating func findByteRange(for searchKey: String) -> Range<Int>? {
91+
internal func findByteRange(for searchKey: String) -> Range<Int>? {
9292
while true {
9393
let startIndex = self.buffer.readerIndex
9494
guard let (key, _) = self.next() else {
@@ -116,7 +116,7 @@ public struct BSONDocumentIterator: IteratorProtocol {
116116
fatalError("endIndex must be >= startIndex")
117117
}
118118

119-
var iter = BSONDocumentIterator(over: doc)
119+
let iter = BSONDocumentIterator(over: doc)
120120

121121
var excludedKeys: [String] = []
122122

Sources/SwiftBSON/BSONTimestamp.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ public struct BSONTimestamp: BSONValue, Equatable, Hashable {
1818
self.increment = inc
1919
}
2020

21+
/// Initializes a new `BSONTimestamp` with the provided `timestamp` and `increment` values. Assumes
22+
/// the values can successfully be converted to `UInt32`s without loss of precision.
23+
public init(timestamp: Int, inc: Int) {
24+
self.init(timestamp: UInt32(timestamp), inc: UInt32(inc))
25+
}
26+
2127
/*
2228
* Initializes a `BSONTimestamp` from ExtendedJSON.
2329
*

Tests/SwiftBSONTests/BSONDocument+SequenceTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class Document_SequenceTests: BSONTestCase {
2121
]
2222

2323
// create and use iter manually
24-
var iter = doc.makeIterator()
24+
let iter = doc.makeIterator()
2525

2626
let stringTup = iter.next()!
2727
expect(stringTup.key).to(equal("string"))

Tests/SwiftBSONTests/BSONDocumentIteratorTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import XCTest
66
final class DocumentIteratorTests: BSONTestCase {
77
func testFindByteRangeEmpty() {
88
let d: BSONDocument = [:]
9-
var iter = d.makeIterator()
9+
let iter = d.makeIterator()
1010
let range = iter.findByteRange(for: "item")
1111
expect(range).to(beNil())
1212
}
1313

1414
func testFindByteRangeItemsInt32() {
1515
let d: BSONDocument = ["item0": .int32(32), "item1": .int32(32)]
16-
var iter = d.makeIterator()
16+
let iter = d.makeIterator()
1717
let maybeRange = iter.findByteRange(for: "item1")
1818

1919
expect(maybeRange).toNot(beNil())

0 commit comments

Comments
 (0)