1
+ import ExtrasBase64
1
2
import Foundation
2
3
import NIO
3
4
@@ -108,13 +109,14 @@ public struct BSONBinary: Equatable, Hashable {
108
109
/// - `BSONError.InvalidArgumentError` if the base64 `String` is invalid or if the provided data is
109
110
/// incompatible with the specified subtype.
110
111
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 {
112
116
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) "
115
118
)
116
119
}
117
- try self . init ( data: dataObj, subtype: subtype)
118
120
}
119
121
120
122
/// Converts this `BSONBinary` instance to a `UUID`.
@@ -143,6 +145,8 @@ public struct BSONBinary: Equatable, Hashable {
143
145
}
144
146
145
147
extension BSONBinary : BSONValue {
148
+ internal static let extJSONTypeWrapperKeys : [ String ] = [ " $binary " , " $uuid " ]
149
+
146
150
/*
147
151
* Initializes a `Binary` from ExtendedJSON.
148
152
*
@@ -158,16 +162,16 @@ extension BSONBinary: BSONValue {
158
162
* - `DecodingError` if `json` is a partial match or is malformed.
159
163
*/
160
164
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) {
162
166
guard let uuidString = uuidJSON. stringValue else {
163
- throw DecodingError . _extendedJSONError (
167
+ throw Swift . DecodingError. _extendedJSONError (
164
168
keyPath: keyPath,
165
169
debugDescription: " Expected value for key $uuid \" \( uuidJSON) \" to be a string "
166
170
+ " but got some other value "
167
171
)
168
172
}
169
173
guard let uuid = UUID ( uuidString: uuidString) else {
170
- throw DecodingError . _extendedJSONError (
174
+ throw Swift . DecodingError. _extendedJSONError (
171
175
keyPath: keyPath,
172
176
debugDescription: " Invalid UUID string: \( uuidString) "
173
177
)
@@ -177,27 +181,27 @@ extension BSONBinary: BSONValue {
177
181
self = try BSONBinary ( from: uuid)
178
182
return
179
183
} catch {
180
- throw DecodingError . _extendedJSONError (
184
+ throw Swift . DecodingError. _extendedJSONError (
181
185
keyPath: keyPath,
182
186
debugDescription: error. localizedDescription
183
187
)
184
188
}
185
189
}
186
190
187
191
// 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 {
189
193
return nil
190
194
}
191
195
guard
192
196
let ( base64, subTypeInput) = try binary. unwrapObject ( withKeys: " base64 " , " subType " , keyPath: keyPath)
193
197
else {
194
- throw DecodingError . _extendedJSONError (
198
+ throw Swift . DecodingError. _extendedJSONError (
195
199
keyPath: keyPath,
196
200
debugDescription: " Missing \" base64 \" or \" subType \" in \( binary) "
197
201
)
198
202
}
199
203
guard let base64Str = base64. stringValue else {
200
- throw DecodingError . _extendedJSONError (
204
+ throw Swift . DecodingError. _extendedJSONError (
201
205
keyPath: keyPath,
202
206
debugDescription: " Could not parse `base64` from \" \( base64) \" , " +
203
207
" input must be a base64-encoded (with padding as =) payload as a string "
@@ -208,7 +212,7 @@ extension BSONBinary: BSONValue {
208
212
let subTypeInt = UInt8 ( subTypeStr, radix: 16 ) ,
209
213
let subType = Subtype ( rawValue: subTypeInt)
210
214
else {
211
- throw DecodingError . _extendedJSONError (
215
+ throw Swift . DecodingError. _extendedJSONError (
212
216
keyPath: keyPath,
213
217
debugDescription: " Could not parse `SubType` from \" \( subTypeInput) \" , " +
214
218
" input must be a BSON binary type as a one- or two-character hex string "
@@ -217,7 +221,7 @@ extension BSONBinary: BSONValue {
217
221
do {
218
222
self = try BSONBinary ( base64: base64Str, subtype: subType)
219
223
} catch {
220
- throw DecodingError . _extendedJSONError (
224
+ throw Swift . DecodingError. _extendedJSONError (
221
225
keyPath: keyPath,
222
226
debugDescription: error. localizedDescription
223
227
)
@@ -233,8 +237,8 @@ extension BSONBinary: BSONValue {
233
237
internal func toCanonicalExtendedJSON( ) -> JSON {
234
238
[
235
239
" $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) ) )
238
242
]
239
243
]
240
244
}
0 commit comments