Skip to content
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
6 changes: 3 additions & 3 deletions Sources/MongoSwift/BSON/BSONDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ extension BSONDocument {

/// Returns the relaxed extended JSON representation of this `BSONDocument`.
/// On error, an empty string will be returned.
public var extendedJSON: String {
public func toExtendedJSONString() -> String {
Copy link
Contributor

Choose a reason for hiding this comment

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

I was going to say it would be better to throw than to return an empty string now that these are methods, but OTOH i think in the new library we don't expect JSON serialization to fail unless it's explicitly unvalidated BSON. in which case i think fatal error / empty string is acceptable

let result = self.withBSONPointer { ptr in
bson_as_relaxed_extended_json(ptr, nil)
}
Expand All @@ -314,7 +314,7 @@ extension BSONDocument {

/// Returns the canonical extended JSON representation of this `BSONDocument`.
/// On error, an empty string will be returned.
public var canonicalExtendedJSON: String {
public func toCanonicalExtendedJSONString() -> String {
let result = self.withBSONPointer { ptr in
bson_as_canonical_extended_json(ptr, nil)
}
Expand Down Expand Up @@ -541,7 +541,7 @@ extension BSONDocument: CustomStringConvertible {
/// Returns the relaxed extended JSON representation of this `BSONDocument`.
/// On error, an empty string will be returned.
public var description: String {
self.extendedJSON
self.toExtendedJSONString()
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/TestsCommon/CommonTestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private func clean(json: String?) -> String {
}
do {
let doc = try BSONDocument(fromJSON: str.data(using: .utf8)!)
return doc.extendedJSON
return doc.toExtendedJSONString()
} catch {
print("Failed to clean string: \(str)")
return String()
Expand Down
14 changes: 7 additions & 7 deletions Tests/BSONTests/BSONCorpusTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,16 @@ final class BSONCorpusTests: MongoSwiftTestCase {
expect(docFromNative.toData()).to(equal(cBData))

// native_to_canonical_extended_json( bson_to_native(cB) ) = cEJ
expect(docFromCB.canonicalExtendedJSON).to(cleanEqual(test.canonicalExtJSON))
expect(docFromCB.toCanonicalExtendedJSONString()).to(cleanEqual(test.canonicalExtJSON))

// native_to_relaxed_extended_json( bson_to_native(cB) ) = rEJ (if rEJ exists)
if let rEJ = test.relaxedExtJSON {
expect(try BSONDocument(fromBSON: cBData).extendedJSON).to(cleanEqual(rEJ))
expect(try BSONDocument(fromBSON: cBData).toExtendedJSONString()).to(cleanEqual(rEJ))
}

// for cEJ input:
// native_to_canonical_extended_json( json_to_native(cEJ) ) = cEJ
expect(try BSONDocument(fromJSON: cEJData).canonicalExtendedJSON)
expect(try BSONDocument(fromJSON: cEJData).toCanonicalExtendedJSONString())
.to(cleanEqual(test.canonicalExtJSON))

// native_to_bson( json_to_native(cEJ) ) = cB (unless lossy)
Expand All @@ -176,19 +176,19 @@ final class BSONCorpusTests: MongoSwiftTestCase {
}

// bson_to_canonical_extended_json(dB) = cEJ
expect(try BSONDocument(fromBSON: dBData).canonicalExtendedJSON)
expect(try BSONDocument(fromBSON: dBData).toCanonicalExtendedJSONString())
.to(cleanEqual(test.canonicalExtJSON))

// bson_to_relaxed_extended_json(dB) = rEJ (if rEJ exists)
if let rEJ = test.relaxedExtJSON {
expect(try BSONDocument(fromBSON: dBData).extendedJSON).to(cleanEqual(rEJ))
expect(try BSONDocument(fromBSON: dBData).toExtendedJSONString()).to(cleanEqual(rEJ))
}
}

// for dEJ input (if it exists):
if let dEJ = test.degenerateExtJSON {
// native_to_canonical_extended_json( json_to_native(dEJ) ) = cEJ
expect(try BSONDocument(fromJSON: dEJ).canonicalExtendedJSON)
expect(try BSONDocument(fromJSON: dEJ).toCanonicalExtendedJSONString())
.to(cleanEqual(test.canonicalExtJSON))

// native_to_bson( json_to_native(dEJ) ) = cB (unless lossy)
Expand All @@ -200,7 +200,7 @@ final class BSONCorpusTests: MongoSwiftTestCase {
// for rEJ input (if it exists):
if let rEJ = test.relaxedExtJSON {
// native_to_relaxed_extended_json( json_to_native(rEJ) ) = rEJ
expect(try BSONDocument(fromJSON: rEJ).extendedJSON).to(cleanEqual(rEJ))
expect(try BSONDocument(fromJSON: rEJ).toExtendedJSONString()).to(cleanEqual(rEJ))
}
}
}
Expand Down
47 changes: 30 additions & 17 deletions Tests/BSONTests/CodecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -533,15 +533,15 @@ final class CodecTests: MongoSwiftTestCase {
let bsonDoc = BSON.document(doc)
expect(try encoder.encode(bsonDoc)).to(equal(doc))
expect(try decoder.decode(BSON.self, from: doc)).to(equal(bsonDoc))
expect(try decoder.decode(BSON.self, from: doc.canonicalExtendedJSON)).to(equal(bsonDoc))
expect(try decoder.decode(BSON.self, from: doc.toCanonicalExtendedJSONString())).to(equal(bsonDoc))
// doc wrapped in a struct

let wrappedDoc: BSONDocument = ["x": bsonDoc]
expect(try encoder.encode(AnyBSONStruct(bsonDoc))).to(equal(wrappedDoc))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedDoc).x).to(equal(bsonDoc))
expect(try decoder.decode(
AnyBSONStruct.self,
from: wrappedDoc.canonicalExtendedJSON
from: wrappedDoc.toCanonicalExtendedJSONString()
).x).to(equal(bsonDoc))

// values wrapped in an `AnyBSONStruct`
Expand All @@ -551,7 +551,8 @@ final class CodecTests: MongoSwiftTestCase {
let wrappedDouble: BSONDocument = ["x": double]
expect(try encoder.encode(AnyBSONStruct(double))).to(equal(wrappedDouble))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedDouble).x).to(equal(double))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedDouble.canonicalExtendedJSON).x).to(equal(double))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedDouble.toCanonicalExtendedJSONString()).x)
.to(equal(double))

// string
let string: BSON = "hi"
Expand All @@ -560,7 +561,8 @@ final class CodecTests: MongoSwiftTestCase {
let wrappedString: BSONDocument = ["x": string]
expect(try encoder.encode(AnyBSONStruct(string))).to(equal(wrappedString))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedString).x).to(equal(string))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedString.canonicalExtendedJSON).x).to(equal(string))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedString.toCanonicalExtendedJSONString()).x)
.to(equal(string))

// array
let array: BSON = [1, 2, "hello"]
Expand Down Expand Up @@ -596,7 +598,7 @@ final class CodecTests: MongoSwiftTestCase {
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedBinary).x).to(equal(binary))
expect(try decoder.decode(
AnyBSONStruct.self,
from: wrappedBinary.canonicalExtendedJSON
from: wrappedBinary.toCanonicalExtendedJSONString()
).x).to(equal(binary))

// BSONObjectID
Expand All @@ -608,7 +610,8 @@ final class CodecTests: MongoSwiftTestCase {
let wrappedOid: BSONDocument = ["x": bsonOid]
expect(try encoder.encode(AnyBSONStruct(bsonOid))).to(equal(wrappedOid))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedOid).x).to(equal(bsonOid))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedOid.canonicalExtendedJSON).x).to(equal(bsonOid))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedOid.toCanonicalExtendedJSONString()).x)
.to(equal(bsonOid))

// bool
let bool: BSON = true
Expand All @@ -618,7 +621,8 @@ final class CodecTests: MongoSwiftTestCase {
let wrappedBool: BSONDocument = ["x": bool]
expect(try encoder.encode(AnyBSONStruct(bool))).to(equal(wrappedBool))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedBool).x).to(equal(bool))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedBool.canonicalExtendedJSON).x).to(equal(bool))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedBool.toCanonicalExtendedJSONString()).x)
.to(equal(bool))

// date
let date = BSON.datetime(Date(timeIntervalSince1970: 5000))
Expand All @@ -628,7 +632,8 @@ final class CodecTests: MongoSwiftTestCase {
let wrappedDate: BSONDocument = ["x": date]
expect(try encoder.encode(AnyBSONStruct(date))).to(equal(wrappedDate))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedDate).x).to(equal(date))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedDate.canonicalExtendedJSON).x).to(equal(date))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedDate.toCanonicalExtendedJSONString()).x)
.to(equal(date))

let dateEncoder = BSONEncoder()
dateEncoder.dateEncodingStrategy = .millisecondsSince1970
Expand All @@ -650,7 +655,8 @@ final class CodecTests: MongoSwiftTestCase {
let wrappedRegex: BSONDocument = ["x": regex]
expect(try encoder.encode(AnyBSONStruct(regex))).to(equal(wrappedRegex))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedRegex).x).to(equal(regex))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedRegex.canonicalExtendedJSON).x).to(equal(regex))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedRegex.toCanonicalExtendedJSONString()).x)
.to(equal(regex))

// codewithscope
let code = BSON.codeWithScope(BSONCodeWithScope(code: "console.log(x);", scope: ["x": 1]))
Expand All @@ -666,7 +672,8 @@ final class CodecTests: MongoSwiftTestCase {
let wrappedCode: BSONDocument = ["x": code]
expect(try encoder.encode(AnyBSONStruct(code))).to(equal(wrappedCode))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedCode).x).to(equal(code))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedCode.canonicalExtendedJSON).x).to(equal(code))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedCode.toCanonicalExtendedJSONString()).x)
.to(equal(code))

// int32
let int32 = BSON.int32(5)
Expand All @@ -676,8 +683,9 @@ final class CodecTests: MongoSwiftTestCase {
let wrappedInt32: BSONDocument = ["x": int32]
expect(try encoder.encode(AnyBSONStruct(int32))).to(equal(wrappedInt32))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt32).x).to(equal(int32))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt32.canonicalExtendedJSON).x).to(equal(int32)
)
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt32.toCanonicalExtendedJSONString()).x)
.to(equal(int32)
)

// int
let int: BSON = 5
Expand All @@ -687,7 +695,8 @@ final class CodecTests: MongoSwiftTestCase {
let wrappedInt: BSONDocument = ["x": int]
expect(try encoder.encode(AnyBSONStruct(int))).to(equal(wrappedInt))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt).x).to(equal(int))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt.canonicalExtendedJSON).x).to(equal(int))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt.toCanonicalExtendedJSONString()).x)
.to(equal(int))

// int64
let int64 = BSON.int64(5)
Expand All @@ -697,7 +706,8 @@ final class CodecTests: MongoSwiftTestCase {
let wrappedInt64: BSONDocument = ["x": int64]
expect(try encoder.encode(AnyBSONStruct(int64))).to(equal(wrappedInt64))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt64).x).to(equal(int64))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt64.canonicalExtendedJSON).x).to(equal(int64))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt64.toCanonicalExtendedJSONString()).x)
.to(equal(int64))

// decimal128
let decimal = BSON.decimal128(try BSONDecimal128("1.2E+10"))
Expand All @@ -707,7 +717,8 @@ final class CodecTests: MongoSwiftTestCase {
let wrappedDecimal: BSONDocument = ["x": decimal]
expect(try encoder.encode(AnyBSONStruct(decimal))).to(equal(wrappedDecimal))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedDecimal).x).to(equal(decimal))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedDecimal.canonicalExtendedJSON).x).to(equal(decimal))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedDecimal.toCanonicalExtendedJSONString()).x)
.to(equal(decimal))

// maxkey
let maxKey = BSON.maxKey
Expand All @@ -717,7 +728,8 @@ final class CodecTests: MongoSwiftTestCase {
let wrappedMaxKey: BSONDocument = ["x": maxKey]
expect(try encoder.encode(AnyBSONStruct(maxKey))).to(equal(wrappedMaxKey))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedMaxKey).x).to(equal(maxKey))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedMaxKey.canonicalExtendedJSON).x).to(equal(maxKey))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedMaxKey.toCanonicalExtendedJSONString()).x)
.to(equal(maxKey))

// minkey
let minKey = BSON.minKey
Expand All @@ -727,7 +739,8 @@ final class CodecTests: MongoSwiftTestCase {
let wrappedMinKey: BSONDocument = ["x": minKey]
expect(try encoder.encode(AnyBSONStruct(minKey))).to(equal(wrappedMinKey))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedMinKey).x).to(equal(minKey))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedMinKey.canonicalExtendedJSON).x).to(equal(minKey))
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedMinKey.toCanonicalExtendedJSONString()).x)
.to(equal(minKey))

// BSONNull
expect(try decoder.decode(AnyBSONStruct.self, from: ["x": .null]).x).to(equal(BSON.null))
Expand Down