Skip to content

Commit 5a2fe11

Browse files
authored
Swift-1384 Support 'let' option for multiple CRUD commands (#715)
1 parent 79c069f commit 5a2fe11

17 files changed

+2185
-214
lines changed

.swift-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5.5.1
1+
5.5.2

.swiftformat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@
4343

4444
# creates non-compiling code on Swift 5.1
4545
--disable preferKeyPath
46+
47+
# swift version 5.1.5 requires backticks in argument lists.
48+
--disable redundantBackticks

Sources/MongoSwift/MongoCollection+FindAndModify.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ public struct FindOneAndDeleteOptions: FindAndModifyOptionsConvertible, Decodabl
190190
/// A document or string that specifies the index to use to support the query. Only supported in server 4.4+.
191191
public var hint: IndexHint?
192192

193+
/// Variables that can be accessed within the operation using the double
194+
/// dollar sign prefix in the form `$$<variable_name>`. This option is only available on MongoDB 5.0+.
195+
public var `let`: BSONDocument?
196+
193197
/// The maximum amount of time to allow the query to run.
194198
public var maxTimeMS: Int?
195199

@@ -206,6 +210,8 @@ public struct FindOneAndDeleteOptions: FindAndModifyOptionsConvertible, Decodabl
206210
try FindAndModifyOptions(
207211
collation: self.collation,
208212
hint: self.hint,
213+
// swiftlint:disable:next colon
214+
`let`: self.let,
209215
maxTimeMS: self.maxTimeMS,
210216
projection: self.projection,
211217
remove: true,
@@ -218,13 +224,15 @@ public struct FindOneAndDeleteOptions: FindAndModifyOptionsConvertible, Decodabl
218224
public init(
219225
collation: BSONDocument? = nil,
220226
hint: IndexHint? = nil,
227+
`let`: BSONDocument? = nil,
221228
maxTimeMS: Int? = nil,
222229
projection: BSONDocument? = nil,
223230
sort: BSONDocument? = nil,
224231
writeConcern: WriteConcern? = nil
225232
) {
226233
self.collation = collation
227234
self.hint = hint
235+
self.let = `let`
228236
self.maxTimeMS = maxTimeMS
229237
self.projection = projection
230238
self.sort = sort
@@ -243,6 +251,10 @@ public struct FindOneAndReplaceOptions: FindAndModifyOptionsConvertible, Decodab
243251
/// A document or string that specifies the index to use to support the query. Only supported in server 4.4+.
244252
public var hint: IndexHint?
245253

254+
/// Variables that can be accessed within the operation using the double
255+
/// dollar sign prefix in the form `$$<variable_name>`. This option is only available on MongoDB 5.0+.
256+
public var `let`: BSONDocument?
257+
246258
/// The maximum amount of time to allow the query to run.
247259
public var maxTimeMS: Int?
248260

@@ -266,6 +278,8 @@ public struct FindOneAndReplaceOptions: FindAndModifyOptionsConvertible, Decodab
266278
bypassDocumentValidation: self.bypassDocumentValidation,
267279
collation: self.collation,
268280
hint: self.hint,
281+
// swiftlint:disable:next colon
282+
`let`: self.let,
269283
maxTimeMS: self.maxTimeMS,
270284
projection: self.projection,
271285
returnDocument: self.returnDocument,
@@ -280,6 +294,7 @@ public struct FindOneAndReplaceOptions: FindAndModifyOptionsConvertible, Decodab
280294
bypassDocumentValidation: Bool? = nil,
281295
collation: BSONDocument? = nil,
282296
hint: IndexHint? = nil,
297+
`let`: BSONDocument? = nil,
283298
maxTimeMS: Int? = nil,
284299
projection: BSONDocument? = nil,
285300
returnDocument: ReturnDocument? = nil,
@@ -290,6 +305,7 @@ public struct FindOneAndReplaceOptions: FindAndModifyOptionsConvertible, Decodab
290305
self.bypassDocumentValidation = bypassDocumentValidation
291306
self.collation = collation
292307
self.hint = hint
308+
self.let = `let`
293309
self.maxTimeMS = maxTimeMS
294310
self.projection = projection
295311
self.returnDocument = returnDocument
@@ -313,6 +329,10 @@ public struct FindOneAndUpdateOptions: FindAndModifyOptionsConvertible, Decodabl
313329
/// A document or string that specifies the index to use to support the query. Only supported in server 4.4+.
314330
public var hint: IndexHint?
315331

332+
/// Variables that can be accessed within the operation using the double
333+
/// dollar sign prefix in the form `$$<variable_name>`. This option is only available on MongoDB 5.0+.
334+
public var `let`: BSONDocument?
335+
316336
/// The maximum amount of time to allow the query to run.
317337
public var maxTimeMS: Int?
318338

@@ -337,6 +357,8 @@ public struct FindOneAndUpdateOptions: FindAndModifyOptionsConvertible, Decodabl
337357
bypassDocumentValidation: self.bypassDocumentValidation,
338358
collation: self.collation,
339359
hint: self.hint,
360+
// swiftlint:disable:next colon
361+
`let`: self.let,
340362
maxTimeMS: self.maxTimeMS,
341363
projection: self.projection,
342364
returnDocument: self.returnDocument,
@@ -352,6 +374,7 @@ public struct FindOneAndUpdateOptions: FindAndModifyOptionsConvertible, Decodabl
352374
bypassDocumentValidation: Bool? = nil,
353375
collation: BSONDocument? = nil,
354376
hint: IndexHint? = nil,
377+
`let`: BSONDocument? = nil,
355378
maxTimeMS: Int? = nil,
356379
projection: BSONDocument? = nil,
357380
returnDocument: ReturnDocument? = nil,
@@ -363,6 +386,7 @@ public struct FindOneAndUpdateOptions: FindAndModifyOptionsConvertible, Decodabl
363386
self.bypassDocumentValidation = bypassDocumentValidation
364387
self.collation = collation
365388
self.hint = hint
389+
self.let = `let`
366390
self.maxTimeMS = maxTimeMS
367391
self.projection = projection
368392
self.returnDocument = returnDocument

Sources/MongoSwift/Operations/FindAndModifyOperation.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal class FindAndModifyOptions {
2323
bypassDocumentValidation: Bool? = nil,
2424
collation: BSONDocument?,
2525
hint: IndexHint? = nil,
26+
`let`: BSONDocument? = nil,
2627
maxTimeMS: Int?,
2728
projection: BSONDocument?,
2829
remove: Bool? = nil,
@@ -88,6 +89,9 @@ internal class FindAndModifyOptions {
8889
case let .indexSpec(doc): extra["hint"] = .document(doc)
8990
}
9091
}
92+
if let lt = `let` {
93+
extra["let"] = .document(lt)
94+
}
9195

9296
// note: mongoc_find_and_modify_opts_set_max_time_ms() takes in a
9397
// uint32_t, but it should be a positive 64-bit integer, so we

Sources/MongoSwift/Operations/FindOperation.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ public struct FindOptions: Codable {
8888
/// A hint for the index to use.
8989
public var hint: IndexHint?
9090

91+
/// Variables that can be accessed within the operation using the double
92+
/// dollar sign prefix in the form `$$<variable_name>`. This option is only available on MongoDB 5.0+.
93+
public var `let`: BSONDocument?
94+
9195
/// The maximum number of documents to return.
9296
public var limit: Int?
9397

@@ -147,6 +151,7 @@ public struct FindOptions: Codable {
147151
comment: String? = nil,
148152
cursorType: MongoCursorType? = nil,
149153
hint: IndexHint? = nil,
154+
`let`: BSONDocument? = nil,
150155
limit: Int? = nil,
151156
max: BSONDocument? = nil,
152157
maxAwaitTimeMS: Int? = nil,
@@ -168,6 +173,7 @@ public struct FindOptions: Codable {
168173
self.comment = comment
169174
self.cursorType = cursorType
170175
self.hint = hint
176+
self.let = `let`
171177
self.limit = limit
172178
self.max = max
173179
self.maxAwaitTimeMS = maxAwaitTimeMS
@@ -188,6 +194,7 @@ public struct FindOptions: Codable {
188194
self.collation = findOneOptions.collation
189195
self.comment = findOneOptions.comment
190196
self.hint = findOneOptions.hint
197+
self.let = findOneOptions.let
191198
self.max = findOneOptions.max
192199
self.maxTimeMS = findOneOptions.maxTimeMS
193200
self.min = findOneOptions.min
@@ -203,7 +210,7 @@ public struct FindOptions: Codable {
203210

204211
// Encode everything except `self.readPreference`, because this is sent to libmongoc separately
205212
internal enum CodingKeys: String, CodingKey, CaseIterable {
206-
case allowDiskUse, allowPartialResults, awaitData, batchSize, collation, comment, hint, limit, max,
213+
case allowDiskUse, allowPartialResults, awaitData, batchSize, collation, comment, hint, `let`, limit, max,
207214
maxAwaitTimeMS, maxTimeMS, min, noCursorTimeout, projection, readConcern, returnKey,
208215
showRecordID = "showRecordId", tailable, skip, sort
209216
}
@@ -223,6 +230,10 @@ public struct FindOneOptions: Codable {
223230
/// A hint for the index to use.
224231
public var hint: IndexHint?
225232

233+
/// Variables that can be accessed within the operation using the double
234+
/// dollar sign prefix in the form `$$<variable_name>`. This option is only available on MongoDB 5.0+.
235+
public var `let`: BSONDocument?
236+
226237
/// The exclusive upper bound for a specific index.
227238
public var max: BSONDocument?
228239

@@ -260,6 +271,7 @@ public struct FindOneOptions: Codable {
260271
collation: BSONDocument? = nil,
261272
comment: String? = nil,
262273
hint: IndexHint? = nil,
274+
`let`: BSONDocument? = nil,
263275
max: BSONDocument? = nil,
264276
maxTimeMS: Int? = nil,
265277
min: BSONDocument? = nil,
@@ -275,6 +287,7 @@ public struct FindOneOptions: Codable {
275287
self.collation = collation
276288
self.comment = comment
277289
self.hint = hint
290+
self.let = `let`
278291
self.max = max
279292
self.maxTimeMS = maxTimeMS
280293
self.min = min

Tests/MongoSwiftSyncTests/CrudTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,18 @@ final class CrudTests: MongoSwiftTestCase {
7575
}
7676

7777
func testCrudUnified() throws {
78+
let skipFiles: [String] = [
79+
// Skipping because we use bulk-write for these commands and can't pass extra options
80+
// TODO: SWIFT-1429 unskip
81+
"deleteOne-let.json",
82+
"deleteMany-let.json",
83+
"updateOne-let.json",
84+
"updateMany-let.json"
85+
]
7886
let files = try retrieveSpecTestFiles(
7987
specName: "crud",
8088
subdirectory: "unified",
89+
excludeFiles: skipFiles,
8190
asType: UnifiedTestFile.self
8291
)
8392
let runner = try UnifiedTestRunner()

0 commit comments

Comments
 (0)