-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathHint.swift
40 lines (35 loc) · 1.27 KB
/
Hint.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import Foundation
/// An index to "hint" or force MongoDB to use when performing a query.
public enum IndexHint: Codable, ExpressibleByStringLiteral, ExpressibleByDictionaryLiteral {
/// Specifies an index to use by its name.
case indexName(String)
/// Specifies an index to use by a specification `BSONDocument` containing the index key(s).
case indexSpec(BSONDocument)
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case let .indexName(name):
try container.encode(name)
case let .indexSpec(doc):
try container.encode(doc)
}
}
public init(stringLiteral value: StringLiteralType) {
self = .indexName(value)
}
public init(dictionaryLiteral keyValuePairs: (String, BSON)...) {
var docs = BSONDocument()
for (k, v) in keyValuePairs {
docs[k] = v
}
self = .indexSpec(docs)
}
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let str = try? container.decode(String.self) {
self = .indexName(str)
} else {
self = .indexSpec(try container.decode(BSONDocument.self))
}
}
}