1+ import ExtrasBase64
12import Foundation
23import NIO
34
@@ -108,13 +109,14 @@ public struct BSONBinary: Equatable, Hashable {
108109 /// - `BSONError.InvalidArgumentError` if the base64 `String` is invalid or if the provided data is
109110 /// incompatible with the specified subtype.
110111 public init ( base64: String , subtype: Subtype ) throws {
111- guard let dataObj = Data ( base64Encoded: base64) else {
112+ do {
113+ let bytes = try base64. base64decoded ( )
114+ try self . init ( bytes: bytes, subtype: subtype)
115+ } catch let error as ExtrasBase64 . DecodingError {
112116 throw BSONError . InvalidArgumentError (
113- message:
114- " failed to create Data object from invalid base64 string \( base64) "
117+ message: " failed to create Data object from invalid base64 string \( base64) : \( error) "
115118 )
116119 }
117- try self . init ( data: dataObj, subtype: subtype)
118120 }
119121
120122 /// Converts this `BSONBinary` instance to a `UUID`.
@@ -143,6 +145,8 @@ public struct BSONBinary: Equatable, Hashable {
143145}
144146
145147extension BSONBinary : BSONValue {
148+ internal static let extJSONTypeWrapperKeys : [ String ] = [ " $binary " , " $uuid " ]
149+
146150 /*
147151 * Initializes a `Binary` from ExtendedJSON.
148152 *
@@ -158,16 +162,16 @@ extension BSONBinary: BSONValue {
158162 * - `DecodingError` if `json` is a partial match or is malformed.
159163 */
160164 internal init ? ( fromExtJSON json: JSON , keyPath: [ String ] ) throws {
161- if let uuidJSON = try json. unwrapObject ( withKey: " $uuid " , keyPath: keyPath) {
165+ if let uuidJSON = try json. value . unwrapObject ( withKey: " $uuid " , keyPath: keyPath) {
162166 guard let uuidString = uuidJSON. stringValue else {
163- throw DecodingError . _extendedJSONError (
167+ throw Swift . DecodingError. _extendedJSONError (
164168 keyPath: keyPath,
165169 debugDescription: " Expected value for key $uuid \" \( uuidJSON) \" to be a string "
166170 + " but got some other value "
167171 )
168172 }
169173 guard let uuid = UUID ( uuidString: uuidString) else {
170- throw DecodingError . _extendedJSONError (
174+ throw Swift . DecodingError. _extendedJSONError (
171175 keyPath: keyPath,
172176 debugDescription: " Invalid UUID string: \( uuidString) "
173177 )
@@ -177,27 +181,27 @@ extension BSONBinary: BSONValue {
177181 self = try BSONBinary ( from: uuid)
178182 return
179183 } catch {
180- throw DecodingError . _extendedJSONError (
184+ throw Swift . DecodingError. _extendedJSONError (
181185 keyPath: keyPath,
182186 debugDescription: error. localizedDescription
183187 )
184188 }
185189 }
186190
187191 // canonical and relaxed extended JSON
188- guard let binary = try json. unwrapObject ( withKey: " $binary " , keyPath: keyPath) else {
192+ guard let binary = try json. value . unwrapObject ( withKey: " $binary " , keyPath: keyPath) else {
189193 return nil
190194 }
191195 guard
192196 let ( base64, subTypeInput) = try binary. unwrapObject ( withKeys: " base64 " , " subType " , keyPath: keyPath)
193197 else {
194- throw DecodingError . _extendedJSONError (
198+ throw Swift . DecodingError. _extendedJSONError (
195199 keyPath: keyPath,
196200 debugDescription: " Missing \" base64 \" or \" subType \" in \( binary) "
197201 )
198202 }
199203 guard let base64Str = base64. stringValue else {
200- throw DecodingError . _extendedJSONError (
204+ throw Swift . DecodingError. _extendedJSONError (
201205 keyPath: keyPath,
202206 debugDescription: " Could not parse `base64` from \" \( base64) \" , " +
203207 " input must be a base64-encoded (with padding as =) payload as a string "
@@ -208,7 +212,7 @@ extension BSONBinary: BSONValue {
208212 let subTypeInt = UInt8 ( subTypeStr, radix: 16 ) ,
209213 let subType = Subtype ( rawValue: subTypeInt)
210214 else {
211- throw DecodingError . _extendedJSONError (
215+ throw Swift . DecodingError. _extendedJSONError (
212216 keyPath: keyPath,
213217 debugDescription: " Could not parse `SubType` from \" \( subTypeInput) \" , " +
214218 " input must be a BSON binary type as a one- or two-character hex string "
@@ -217,7 +221,7 @@ extension BSONBinary: BSONValue {
217221 do {
218222 self = try BSONBinary ( base64: base64Str, subtype: subType)
219223 } catch {
220- throw DecodingError . _extendedJSONError (
224+ throw Swift . DecodingError. _extendedJSONError (
221225 keyPath: keyPath,
222226 debugDescription: error. localizedDescription
223227 )
@@ -233,8 +237,8 @@ extension BSONBinary: BSONValue {
233237 internal func toCanonicalExtendedJSON( ) -> JSON {
234238 [
235239 " $binary " : [
236- " base64 " : . string( Data ( self . data. readableBytesView) . base64EncodedString ( ) ) ,
237- " subType " : . string( String ( format: " %02x " , self . subtype. rawValue) )
240+ " base64 " : JSON ( . string( Data ( self . data. readableBytesView) . base64EncodedString ( ) ) ) ,
241+ " subType " : JSON ( . string( String ( format: " %02x " , self . subtype. rawValue) ) )
238242 ]
239243 ]
240244 }
0 commit comments