Skip to content

SWIFT-154: Int should encode as Int32 or Int64 as needed #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions Sources/MongoSwift/BSON/BsonValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -377,19 +377,18 @@ extension Double: BsonValue {
}
}

/// An extension of `Int` to represent the BSON Int32 type.
/// While the bitwidth of Int is machine-dependent, we assume for simplicity
/// that it is always 32 bits. Use `Int64` if 64 bits are needed.
/// An extension of `Int` to represent the BSON Int32 or Int64 type.
/// The `Int` will be encoded as an Int32 if possible, or an Int64 if necessary.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please update the doc comment just above this extension to clarify the new behavior?

Done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

extension Int: BsonValue {
public var bsonType: BsonType { return .int32 }
public func encode(to data: UnsafeMutablePointer<bson_t>, forKey key: String) throws {
guard let int32 = Int32(exactly: self) else {
throw MongoError.bsonEncodeError(message:
"`Int` value \(self) does not fit in an `Int32`. Use an `Int64` instead")
if let int32 = Int32(exactly: self) {
return try int32.encode(to: data, forKey: key)
}
if !bson_append_int32(data, key, Int32(key.count), int32) {
throw bsonEncodeError(value: self, forKey: key)
if let int64 = Int64(exactly: self) {
return try int64.encode(to: data, forKey: key)
}
throw MongoError.bsonEncodeError(message: "`Int` value \(self) could not be encoded as `Int32` or `Int64`")
}

public static func from(iter: inout bson_iter_t) -> BsonValue {
Expand Down
31 changes: 26 additions & 5 deletions Tests/MongoSwiftTests/DocumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class DocumentTests: XCTestCase {
("testEquatable", testEquatable),
("testRawBSON", testRawBSON),
("testValueBehavior", testValueBehavior),
("testInvalidInt", testInvalidInt),
("testIntEncodesAsInt32OrInt64", testIntEncodesAsInt32OrInt64),
("testBSONCorpus", testBSONCorpus),
("testMerge", testMerge)
]
Expand Down Expand Up @@ -198,11 +198,32 @@ final class DocumentTests: XCTestCase {
XCTAssertNotEqual(doc1, doc2)
}

func testInvalidInt() {
let doc1 = Document()
let v = Int(Int32.max) + 1
expect(try v.encode(to: doc1.data, forKey: "x")).to(throwError())
func testIntEncodesAsInt32OrInt64() {
/* Skip this test on 32-bit platforms. Use MemoryLayout instead of
* Int.bitWidth to avoid a compiler warning.
* See: https://forums.swift.org/t/how-can-i-condition-on-the-size-of-int/9080/4 */
if MemoryLayout<Int>.size == 4 {
return
}

let int32min_sub1 = Int64(Int32.min) - Int64(1)
let int32max_add1 = Int64(Int32.max) + Int64(1)

var doc: Document = [
"int32min": Int(Int32.min),
"int32max": Int(Int32.max),
"int32min-1": Int(int32min_sub1),
"int32max+1": Int(int32max_add1),
"int64min": Int(Int64.min),
"int64max": Int(Int64.max)
]

expect(doc["int32min"] as? Int).to(equal(Int(Int32.min)))
expect(doc["int32max"] as? Int).to(equal(Int(Int32.max)))
expect(doc["int32min-1"] as? Int64).to(equal(int32min_sub1))
expect(doc["int32max+1"] as? Int64).to(equal(int32max_add1))
expect(doc["int64min"] as? Int64).to(equal(Int64.min))
expect(doc["int64max"] as? Int64).to(equal(Int64.max))
}

// swiftlint:disable:next cyclomatic_complexity
Expand Down