-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathMongoServerAPI.swift
101 lines (83 loc) · 3.62 KB
/
MongoServerAPI.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import CLibMongoC
/// A type containing options for specifying a MongoDB server API version and related behavior.
/// - SeeAlso: https://docs.mongodb.com/manual/reference/stable-api/
public struct MongoServerAPI: Codable {
/// Represents a server API version.
public struct Version: Codable, Equatable, LosslessStringConvertible {
/// MongoDB API version 1.
public static let v1 = Version(.v1)
private enum _Version: String {
case v1 = "1"
}
private let _version: _Version
private init(_ version: _Version) {
self._version = version
}
/// `LosslessStringConvertible` conformance
public init?(_ description: String) {
guard let _version = _Version(rawValue: description) else {
return nil
}
self.init(_version)
}
public var description: String {
self._version.rawValue
}
/// `Codable` conformance
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self._version.rawValue)
}
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let stringValue = try container.decode(String.self)
guard let version = _Version(rawValue: stringValue) else {
throw DecodingError.dataCorruptedError(
in: container,
debugDescription: "Invalid API version string \(stringValue)"
)
}
self = Version(version)
}
fileprivate var mongocAPIVersion: mongoc_server_api_version_t {
switch self {
case .v1:
return MONGOC_SERVER_API_V1
default:
// we would only ever get a value besides v1 if we upgraded to a
// version of libmongoc that supported a new API version.
fatalError("Encountered unexpected API version \(self)")
}
}
}
/// Declares an API version to use.
public var version: Version
/// Specifies whether the server should reject all commands that are not part of the declare API version. This
/// includes command options and aggregation pipeline stages.
public var strict: Bool?
/// Specifies whether the server should return command failures when functionality that is deprecated in the
/// specified API version is used.
public var deprecationErrors: Bool?
/// Convenience initializer allowing optional parameters to be optional or omitted.
public init(version: Version, strict: Bool? = nil, deprecationErrors: Bool? = nil) {
self.version = version
self.strict = strict
self.deprecationErrors = deprecationErrors
}
/// Creates an equivalent `mongoc_server_api_t` and uses a pointer to it to execute the provided closure, cleaning
/// it up afterward. The pointer must not escape the closure.
internal func withMongocServerAPI<T>(_ body: (OpaquePointer) throws -> T) rethrows -> T {
// this never returns nil
guard let api = mongoc_server_api_new(self.version.mongocAPIVersion) else {
fatalError("Failed to initialize mongoc_server_api_t")
}
defer { mongoc_server_api_destroy(api) }
if let strict = self.strict {
mongoc_server_api_strict(api, strict)
}
if let deprecationErrors = self.deprecationErrors {
mongoc_server_api_deprecation_errors(api, deprecationErrors)
}
return try body(api)
}
}