@@ -137,7 +137,7 @@ extension Array: BSONValue where Element == BSON {
137137 }
138138
139139 /// Attempts to map this `[BSON]` to a `[T]`, where `T` is a `BSONValue`.
140- internal func asArrayOf < T: BSONValue > ( _: T . Type ) -> [ T ] ? {
140+ internal func toArrayOf < T: BSONValue > ( _: T . Type ) -> [ T ] ? {
141141 var result : [ T ] = [ ]
142142 for element in self {
143143 guard let bsonValue = element. bsonValue as? T else {
@@ -321,6 +321,27 @@ public struct BSONBinary: BSONValue, Equatable, Codable, Hashable {
321321 return try self . init ( data: dataObj, subtype: UInt8 ( subtype. rawValue) )
322322 } )
323323 }
324+
325+ /// Converts this `BSONBinary` instance to a `UUID`.
326+ /// - Throws:
327+ /// - `InvalidArgumentError` if a non-UUID subtype is set on this `BSONBinary`.
328+ public func toUUID( ) throws -> UUID {
329+ guard [ Subtype . uuid. rawValue, Subtype . uuidDeprecated. rawValue] . contains ( self . subtype) else {
330+ throw InvalidArgumentError (
331+ message: " Expected a UUID binary subtype, got subtype \( self . subtype) instead. "
332+ )
333+ }
334+
335+ let data = self . data
336+ let uuid : uuid_t = (
337+ data [ 0 ] , data [ 1 ] , data [ 2 ] , data [ 3 ] ,
338+ data [ 4 ] , data [ 5 ] , data [ 6 ] , data [ 7 ] ,
339+ data [ 8 ] , data [ 9 ] , data [ 10 ] , data [ 11 ] ,
340+ data [ 12 ] , data [ 13 ] , data [ 14 ] , data [ 15 ]
341+ )
342+
343+ return UUID ( uuid: uuid)
344+ }
324345}
325346
326347/// An extension of `Bool` to represent the BSON Boolean type.
@@ -886,34 +907,6 @@ extension BSONObjectID: Hashable {
886907 }
887908}
888909
889- /// Extension to allow a `UUID` to be initialized from a `BSONBinary`.
890- extension UUID {
891- /// Initializes a `UUID` instance from a `BSONBinary`.
892- /// - Throws:
893- /// - `InvalidArgumentError` if a non-UUID subtype is set on the `BSONBinary`.
894- public init ( from binary: BSONBinary ) throws {
895- guard [
896- BSONBinary . Subtype. uuid. rawValue,
897- BSONBinary . Subtype. uuidDeprecated. rawValue
898- ] . contains ( binary. subtype) else {
899- throw InvalidArgumentError (
900- message: " Expected a UUID binary type " +
901- " ( \( BSONBinary . Subtype. uuid) ), got \( binary. subtype) instead. "
902- )
903- }
904-
905- let data = binary. data
906- let uuid : uuid_t = (
907- data [ 0 ] , data [ 1 ] , data [ 2 ] , data [ 3 ] ,
908- data [ 4 ] , data [ 5 ] , data [ 6 ] , data [ 7 ] ,
909- data [ 8 ] , data [ 9 ] , data [ 10 ] , data [ 11 ] ,
910- data [ 12 ] , data [ 13 ] , data [ 14 ] , data [ 15 ]
911- )
912-
913- self . init ( uuid: uuid)
914- }
915- }
916-
917910// A mapping of regex option characters to their equivalent `NSRegularExpression` option.
918911// note that there is a BSON regexp option 'l' that `NSRegularExpression`
919912// doesn't support. The flag will be dropped if BSON containing it is parsed,
@@ -926,7 +919,7 @@ private let regexOptsMap: [Character: NSRegularExpression.Options] = [
926919 " x " : . allowCommentsAndWhitespace
927920]
928921
929- /// An extension of `NSRegularExpression` to allow it to be initialized from a `BSONRegularExpression`.
922+ /// An extension of `NSRegularExpression` to support conversion to and from `BSONRegularExpression`.
930923extension NSRegularExpression {
931924 /// Convert a string of options flags into an equivalent `NSRegularExpression.Options`
932925 internal static func optionsFromString( _ stringOptions: String ) -> NSRegularExpression . Options {
@@ -945,14 +938,6 @@ extension NSRegularExpression {
945938 for (char, o) in regexOptsMap { if options. contains ( o) { optsString += String ( char) } }
946939 return String ( optsString. sorted ( ) )
947940 }
948-
949- /// Initializes a new `NSRegularExpression` with the pattern and options of the provided `BSONRegularExpression`.
950- /// Note: `NSRegularExpression` does not support the `l` locale dependence option, so it will
951- /// be omitted if set on the provided `BSONRegularExpression`.
952- public convenience init ( from regex: BSONRegularExpression ) throws {
953- let opts = NSRegularExpression . optionsFromString ( regex. options)
954- try self . init ( pattern: regex. pattern, options: opts)
955- }
956941}
957942
958943/// A struct to represent a BSON regular expression.
@@ -1016,6 +1001,14 @@ public struct BSONRegularExpression: BSONValue, Equatable, Codable, Hashable {
10161001 return self . init ( pattern: patternString, options: optionsString)
10171002 } )
10181003 }
1004+
1005+ /// Converts this `BSONRegularExpression` to an `NSRegularExpression`.
1006+ /// Note: `NSRegularExpression` does not support the `l` locale dependence option, so it will be omitted if it was
1007+ /// set on this instance.
1008+ public func toNSRegularExpression( ) throws -> NSRegularExpression {
1009+ let opts = NSRegularExpression . optionsFromString ( self . options)
1010+ return try NSRegularExpression ( pattern: self . pattern, options: opts)
1011+ }
10191012}
10201013
10211014/// An extension of String to represent the BSON string type.
0 commit comments