@@ -34,6 +34,11 @@ internal class ConnectionString {
3434 }
3535 }
3636
37+ /// Initializes a new connection string that wraps a copy of the provided URI. Does not destroy the input URI.
38+ internal init ( copying uri: OpaquePointer ) {
39+ self . _uri = mongoc_uri_copy ( uri)
40+ }
41+
3742 /// Cleans up the underlying `mongoc_uri_t`.
3843 deinit {
3944 mongoc_uri_destroy ( self . _uri)
@@ -72,4 +77,116 @@ internal class ConnectionString {
7277 mongoc_uri_set_read_prefs_t ( self . _uri, rp. _readPreference)
7378 }
7479 }
80+
81+ /// Returns the username if one was provided, otherwise nil.
82+ internal var username : String ? {
83+ guard let username = mongoc_uri_get_username ( self . _uri) else {
84+ return nil
85+ }
86+ return String ( cString: username)
87+ }
88+
89+ /// Returns the password if one was provided, otherwise nil.
90+ internal var password : String ? {
91+ guard let pw = mongoc_uri_get_password ( self . _uri) else {
92+ return nil
93+ }
94+ return String ( cString: pw)
95+ }
96+
97+ /// Returns the auth database if one was provided, otherwise nil.
98+ internal var authSource : String ? {
99+ guard let source = mongoc_uri_get_auth_source ( self . _uri) else {
100+ return nil
101+ }
102+ return String ( cString: source)
103+ }
104+
105+ /// Returns the auth mechanism if one was provided, otherwise nil.
106+ internal var authMechanism : AuthMechanism ? {
107+ guard let mechanism = mongoc_uri_get_auth_mechanism ( self . _uri) else {
108+ return nil
109+ }
110+ let str = String ( cString: mechanism)
111+ return AuthMechanism ( rawValue: str)
112+ }
113+
114+ /// Returns a document containing the auth mechanism properties if any were provided, otherwise nil.
115+ internal var authMechanismProperties : Document ? {
116+ var props = bson_t ( )
117+ return withUnsafeMutablePointer ( to: & props) { propsPtr in
118+ let opaquePtr = OpaquePointer ( propsPtr)
119+ guard mongoc_uri_get_mechanism_properties ( self . _uri, opaquePtr) else {
120+ return nil
121+ }
122+ /// This copy should not be returned directly as its only guaranteed valid for as long as the
123+ /// `mongoc_uri_t`, as `props` was statically initialized from data stored in the URI and may contain
124+ /// pointers that will be invalidated once the URI is.
125+ let copy = Document ( copying: opaquePtr)
126+
127+ return copy. mapValues { value in
128+ // mongoc returns boolean options e.g. CANONICALIZE_HOSTNAME as strings, but they are boolean values.
129+ switch value {
130+ case " true " :
131+ return true
132+ case " false " :
133+ return false
134+ default :
135+ return value
136+ }
137+ }
138+ }
139+ }
140+
141+ /// Returns the credential configured on this URI. Will be empty if no options are set.
142+ internal var credential : Credential {
143+ return Credential (
144+ username: self . username,
145+ password: self . password,
146+ source: self . authSource,
147+ mechanism: self . authMechanism,
148+ mechanismProperties: self . authMechanismProperties
149+ )
150+ }
151+
152+ internal var db : String ? {
153+ guard let db = mongoc_uri_get_database ( self . _uri) else {
154+ return nil
155+ }
156+ return String ( cString: db)
157+ }
158+
159+ /// Returns a document containing all of the options provided after the ? of the URI.
160+ internal var options : Document ? {
161+ guard let optsDoc = mongoc_uri_get_options ( self . _uri) else {
162+ return nil
163+ }
164+ return Document ( copying: optsDoc)
165+ }
166+
167+ /// Returns the host/port pairs specified in the connection string, or nil if this connection string's scheme is
168+ /// “mongodb+srv://”.
169+ internal var hosts : [ String ] ? {
170+ guard let hostList = mongoc_uri_get_hosts ( self . _uri) else {
171+ return nil
172+ }
173+
174+ var hosts = [ String] ( )
175+ var next = hostList. pointee
176+ while true {
177+ hosts. append ( withUnsafeBytes ( of: next. host_and_port) { rawPtr in
178+ guard let baseAddress = rawPtr. baseAddress else {
179+ return " "
180+ }
181+ return String ( cString: baseAddress. assumingMemoryBound ( to: CChar . self) )
182+ } )
183+
184+ if next. next == nil {
185+ break
186+ }
187+ next = next. next. pointee
188+ }
189+
190+ return hosts
191+ }
75192}
0 commit comments