Skip to content

Commit 790bfa8

Browse files
SWIFT-1616 Support creating text indexes with default names (#770)
1 parent 6c26d5e commit 790bfa8

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Sources/MongoSwift/MongoCollection+Indexes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public struct IndexModel: Codable {
1818
/// Gets the default name for this index.
1919
internal func getDefaultName() throws -> String {
2020
try self.keys.map { k, v in
21-
guard let vInt = v.toInt() else {
21+
guard let vString = v.toInt().map({ String($0) }) ?? v.stringValue else {
2222
throw MongoError.InvalidArgumentError(message: "Invalid index value for key: \"\(k)\"=\(v)")
2323
}
24-
return "\(k)_\(vInt)"
24+
return "\(k)_\(vString)"
2525
}.joined(separator: "_")
2626
}
2727

Tests/MongoSwiftSyncTests/MongoCollection+IndexTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ final class MongoCollection_IndexTests: MongoSwiftTestCase {
7272
expect(try indexes.next()?.get()).to(beNil())
7373
}
7474

75+
func testCreateTextIndexFromModel() throws {
76+
let model = IndexModel(keys: ["cat": "text"])
77+
expect(try self.coll.createIndex(model)).to(equal("cat_text"))
78+
let indexes = try coll.listIndexes()
79+
expect(try indexes.next()?.get().options?.name).to(equal("_id_"))
80+
expect(try indexes.next()?.get().options?.name).to(equal("cat_text"))
81+
expect(try indexes.next()?.get()).to(beNil())
82+
}
83+
7584
func testIndexOptions() throws {
7685
var options = IndexOptions(
7786
background: true,

0 commit comments

Comments
 (0)