Skip to content

Commit b6401f2

Browse files
authored
SWIFT-882 Convert extJSON properties to methods (#496)
1 parent facf2ee commit b6401f2

File tree

4 files changed

+41
-28
lines changed

4 files changed

+41
-28
lines changed

Sources/MongoSwift/BSON/BSONDocument.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ extension BSONDocument {
300300

301301
/// Returns the relaxed extended JSON representation of this `BSONDocument`.
302302
/// On error, an empty string will be returned.
303-
public var extendedJSON: String {
303+
public func toExtendedJSONString() -> String {
304304
let result = self.withBSONPointer { ptr in
305305
bson_as_relaxed_extended_json(ptr, nil)
306306
}
@@ -314,7 +314,7 @@ extension BSONDocument {
314314

315315
/// Returns the canonical extended JSON representation of this `BSONDocument`.
316316
/// On error, an empty string will be returned.
317-
public var canonicalExtendedJSON: String {
317+
public func toCanonicalExtendedJSONString() -> String {
318318
let result = self.withBSONPointer { ptr in
319319
bson_as_canonical_extended_json(ptr, nil)
320320
}
@@ -541,7 +541,7 @@ extension BSONDocument: CustomStringConvertible {
541541
/// Returns the relaxed extended JSON representation of this `BSONDocument`.
542542
/// On error, an empty string will be returned.
543543
public var description: String {
544-
self.extendedJSON
544+
self.toExtendedJSONString()
545545
}
546546
}
547547

Sources/TestsCommon/CommonTestUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ private func clean(json: String?) -> String {
167167
}
168168
do {
169169
let doc = try BSONDocument(fromJSON: str.data(using: .utf8)!)
170-
return doc.extendedJSON
170+
return doc.toExtendedJSONString()
171171
} catch {
172172
print("Failed to clean string: \(str)")
173173
return String()

Tests/BSONTests/BSONCorpusTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,16 @@ final class BSONCorpusTests: MongoSwiftTestCase {
151151
expect(docFromNative.toData()).to(equal(cBData))
152152

153153
// native_to_canonical_extended_json( bson_to_native(cB) ) = cEJ
154-
expect(docFromCB.canonicalExtendedJSON).to(cleanEqual(test.canonicalExtJSON))
154+
expect(docFromCB.toCanonicalExtendedJSONString()).to(cleanEqual(test.canonicalExtJSON))
155155

156156
// native_to_relaxed_extended_json( bson_to_native(cB) ) = rEJ (if rEJ exists)
157157
if let rEJ = test.relaxedExtJSON {
158-
expect(try BSONDocument(fromBSON: cBData).extendedJSON).to(cleanEqual(rEJ))
158+
expect(try BSONDocument(fromBSON: cBData).toExtendedJSONString()).to(cleanEqual(rEJ))
159159
}
160160

161161
// for cEJ input:
162162
// native_to_canonical_extended_json( json_to_native(cEJ) ) = cEJ
163-
expect(try BSONDocument(fromJSON: cEJData).canonicalExtendedJSON)
163+
expect(try BSONDocument(fromJSON: cEJData).toCanonicalExtendedJSONString())
164164
.to(cleanEqual(test.canonicalExtJSON))
165165

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

178178
// bson_to_canonical_extended_json(dB) = cEJ
179-
expect(try BSONDocument(fromBSON: dBData).canonicalExtendedJSON)
179+
expect(try BSONDocument(fromBSON: dBData).toCanonicalExtendedJSONString())
180180
.to(cleanEqual(test.canonicalExtJSON))
181181

182182
// bson_to_relaxed_extended_json(dB) = rEJ (if rEJ exists)
183183
if let rEJ = test.relaxedExtJSON {
184-
expect(try BSONDocument(fromBSON: dBData).extendedJSON).to(cleanEqual(rEJ))
184+
expect(try BSONDocument(fromBSON: dBData).toExtendedJSONString()).to(cleanEqual(rEJ))
185185
}
186186
}
187187

188188
// for dEJ input (if it exists):
189189
if let dEJ = test.degenerateExtJSON {
190190
// native_to_canonical_extended_json( json_to_native(dEJ) ) = cEJ
191-
expect(try BSONDocument(fromJSON: dEJ).canonicalExtendedJSON)
191+
expect(try BSONDocument(fromJSON: dEJ).toCanonicalExtendedJSONString())
192192
.to(cleanEqual(test.canonicalExtJSON))
193193

194194
// native_to_bson( json_to_native(dEJ) ) = cB (unless lossy)
@@ -200,7 +200,7 @@ final class BSONCorpusTests: MongoSwiftTestCase {
200200
// for rEJ input (if it exists):
201201
if let rEJ = test.relaxedExtJSON {
202202
// native_to_relaxed_extended_json( json_to_native(rEJ) ) = rEJ
203-
expect(try BSONDocument(fromJSON: rEJ).extendedJSON).to(cleanEqual(rEJ))
203+
expect(try BSONDocument(fromJSON: rEJ).toExtendedJSONString()).to(cleanEqual(rEJ))
204204
}
205205
}
206206
}

Tests/BSONTests/CodecTests.swift

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -533,15 +533,15 @@ final class CodecTests: MongoSwiftTestCase {
533533
let bsonDoc = BSON.document(doc)
534534
expect(try encoder.encode(bsonDoc)).to(equal(doc))
535535
expect(try decoder.decode(BSON.self, from: doc)).to(equal(bsonDoc))
536-
expect(try decoder.decode(BSON.self, from: doc.canonicalExtendedJSON)).to(equal(bsonDoc))
536+
expect(try decoder.decode(BSON.self, from: doc.toCanonicalExtendedJSONString())).to(equal(bsonDoc))
537537
// doc wrapped in a struct
538538

539539
let wrappedDoc: BSONDocument = ["x": bsonDoc]
540540
expect(try encoder.encode(AnyBSONStruct(bsonDoc))).to(equal(wrappedDoc))
541541
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedDoc).x).to(equal(bsonDoc))
542542
expect(try decoder.decode(
543543
AnyBSONStruct.self,
544-
from: wrappedDoc.canonicalExtendedJSON
544+
from: wrappedDoc.toCanonicalExtendedJSONString()
545545
).x).to(equal(bsonDoc))
546546

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

556557
// string
557558
let string: BSON = "hi"
@@ -560,7 +561,8 @@ final class CodecTests: MongoSwiftTestCase {
560561
let wrappedString: BSONDocument = ["x": string]
561562
expect(try encoder.encode(AnyBSONStruct(string))).to(equal(wrappedString))
562563
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedString).x).to(equal(string))
563-
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedString.canonicalExtendedJSON).x).to(equal(string))
564+
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedString.toCanonicalExtendedJSONString()).x)
565+
.to(equal(string))
564566

565567
// array
566568
let array: BSON = [1, 2, "hello"]
@@ -596,7 +598,7 @@ final class CodecTests: MongoSwiftTestCase {
596598
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedBinary).x).to(equal(binary))
597599
expect(try decoder.decode(
598600
AnyBSONStruct.self,
599-
from: wrappedBinary.canonicalExtendedJSON
601+
from: wrappedBinary.toCanonicalExtendedJSONString()
600602
).x).to(equal(binary))
601603

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

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

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

633638
let dateEncoder = BSONEncoder()
634639
dateEncoder.dateEncodingStrategy = .millisecondsSince1970
@@ -650,7 +655,8 @@ final class CodecTests: MongoSwiftTestCase {
650655
let wrappedRegex: BSONDocument = ["x": regex]
651656
expect(try encoder.encode(AnyBSONStruct(regex))).to(equal(wrappedRegex))
652657
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedRegex).x).to(equal(regex))
653-
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedRegex.canonicalExtendedJSON).x).to(equal(regex))
658+
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedRegex.toCanonicalExtendedJSONString()).x)
659+
.to(equal(regex))
654660

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

671678
// int32
672679
let int32 = BSON.int32(5)
@@ -676,8 +683,9 @@ final class CodecTests: MongoSwiftTestCase {
676683
let wrappedInt32: BSONDocument = ["x": int32]
677684
expect(try encoder.encode(AnyBSONStruct(int32))).to(equal(wrappedInt32))
678685
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt32).x).to(equal(int32))
679-
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt32.canonicalExtendedJSON).x).to(equal(int32)
680-
)
686+
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt32.toCanonicalExtendedJSONString()).x)
687+
.to(equal(int32)
688+
)
681689

682690
// int
683691
let int: BSON = 5
@@ -687,7 +695,8 @@ final class CodecTests: MongoSwiftTestCase {
687695
let wrappedInt: BSONDocument = ["x": int]
688696
expect(try encoder.encode(AnyBSONStruct(int))).to(equal(wrappedInt))
689697
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt).x).to(equal(int))
690-
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt.canonicalExtendedJSON).x).to(equal(int))
698+
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt.toCanonicalExtendedJSONString()).x)
699+
.to(equal(int))
691700

692701
// int64
693702
let int64 = BSON.int64(5)
@@ -697,7 +706,8 @@ final class CodecTests: MongoSwiftTestCase {
697706
let wrappedInt64: BSONDocument = ["x": int64]
698707
expect(try encoder.encode(AnyBSONStruct(int64))).to(equal(wrappedInt64))
699708
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt64).x).to(equal(int64))
700-
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt64.canonicalExtendedJSON).x).to(equal(int64))
709+
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedInt64.toCanonicalExtendedJSONString()).x)
710+
.to(equal(int64))
701711

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

712723
// maxkey
713724
let maxKey = BSON.maxKey
@@ -717,7 +728,8 @@ final class CodecTests: MongoSwiftTestCase {
717728
let wrappedMaxKey: BSONDocument = ["x": maxKey]
718729
expect(try encoder.encode(AnyBSONStruct(maxKey))).to(equal(wrappedMaxKey))
719730
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedMaxKey).x).to(equal(maxKey))
720-
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedMaxKey.canonicalExtendedJSON).x).to(equal(maxKey))
731+
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedMaxKey.toCanonicalExtendedJSONString()).x)
732+
.to(equal(maxKey))
721733

722734
// minkey
723735
let minKey = BSON.minKey
@@ -727,7 +739,8 @@ final class CodecTests: MongoSwiftTestCase {
727739
let wrappedMinKey: BSONDocument = ["x": minKey]
728740
expect(try encoder.encode(AnyBSONStruct(minKey))).to(equal(wrappedMinKey))
729741
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedMinKey).x).to(equal(minKey))
730-
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedMinKey.canonicalExtendedJSON).x).to(equal(minKey))
742+
expect(try decoder.decode(AnyBSONStruct.self, from: wrappedMinKey.toCanonicalExtendedJSONString()).x)
743+
.to(equal(minKey))
731744

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

0 commit comments

Comments
 (0)