File tree Expand file tree Collapse file tree 3 files changed +43
-1
lines changed Expand file tree Collapse file tree 3 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -158,6 +158,32 @@ extension BSONBinary: BSONValue {
158
158
* - `DecodingError` if `json` is a partial match or is malformed.
159
159
*/
160
160
internal init ? ( fromExtJSON json: JSON , keyPath: [ String ] ) throws {
161
+ if let uuidJSON = try json. unwrapObject ( withKey: " $uuid " , keyPath: keyPath) {
162
+ guard let uuidString = uuidJSON. stringValue else {
163
+ throw DecodingError . _extendedJSONError (
164
+ keyPath: keyPath,
165
+ debugDescription: " Expected value for key $uuid \" \( uuidJSON) \" to be a string "
166
+ + " but got some other value "
167
+ )
168
+ }
169
+ guard let uuid = UUID ( uuidString: uuidString) else {
170
+ throw DecodingError . _extendedJSONError (
171
+ keyPath: keyPath,
172
+ debugDescription: " Invalid UUID string: \( uuidString) "
173
+ )
174
+ }
175
+
176
+ do {
177
+ self = try BSONBinary ( from: uuid)
178
+ return
179
+ } catch {
180
+ throw DecodingError . _extendedJSONError (
181
+ keyPath: keyPath,
182
+ debugDescription: error. localizedDescription
183
+ )
184
+ }
185
+ }
186
+
161
187
// canonical and relaxed extended JSON
162
188
guard let binary = try json. unwrapObject ( withKey: " $binary " , keyPath: keyPath) else {
163
189
return nil
Original file line number Diff line number Diff line change 39
39
"canonical_bson" : " 1D000000057800100000000473FFD26444B34C6990E8E7D1DFC035D400" ,
40
40
"canonical_extjson" : " {\" x\" : { \" $binary\" : {\" base64\" : \" c//SZESzTGmQ6OfR38A11A==\" , \" subType\" : \" 04\" }}}"
41
41
},
42
+ {
43
+ "description" : " subtype 0x04 UUID" ,
44
+ "canonical_bson" : " 1D000000057800100000000473FFD26444B34C6990E8E7D1DFC035D400" ,
45
+ "canonical_extjson" : " {\" x\" : { \" $binary\" : {\" base64\" : \" c//SZESzTGmQ6OfR38A11A==\" , \" subType\" : \" 04\" }}}" ,
46
+ "degenerate_extjson" : " {\" x\" : { \" $uuid\" : \" 73ffd264-44b3-4c69-90e8-e7d1dfc035d4\" }}"
47
+ },
42
48
{
43
49
"description" : " subtype 0x05" ,
44
50
"canonical_bson" : " 1D000000057800100000000573FFD26444B34C6990E8E7D1DFC035D400" ,
81
87
"description" : " subtype 0x02 length negative one" ,
82
88
"bson" : " 130000000578000600000002FFFFFFFFFFFF00"
83
89
}
90
+ ],
91
+ "parseErrors" : [
92
+ {
93
+ "description" : " $uuid wrong type" ,
94
+ "string" : " {\" x\" : { \" $uuid\" : { \" data\" : \" 73ffd264-44b3-4c69-90e8-e7d1dfc035d4\" }}}"
95
+ },
96
+ {
97
+ "description" : " $uuid invalid value" ,
98
+ "string" : " {\" x\" : { \" $uuid\" : \" 73ffd264-44b3-90e8-e7d1dfc035d4\" }}"
99
+ }
84
100
]
85
101
}
Original file line number Diff line number Diff line change @@ -243,7 +243,7 @@ final class BSONCorpusTests: BSONTestCase {
243
243
}
244
244
let description = " \( testFile. description) - \( test. description) "
245
245
switch BSONType ( rawValue: UInt8 ( testFile. bsonType. dropFirst ( 2 ) , radix: 16 ) !) ! {
246
- case . invalid: // "top level document" uses 0x00 for the bson type
246
+ case . binary , . invalid: // "top level document" uses 0x00 for the bson type
247
247
guard let testData = test. string. data ( using: . utf8) else {
248
248
XCTFail ( " Unable to interpret canonical_bson as Data " )
249
249
return
You can’t perform that action at this time.
0 commit comments