Skip to content

Commit 65d576b

Browse files
authored
Merge pull request #11 from LiveUI/refinedEmptyElements
Add additional unit tests. Refined behavior around empty elements.
2 parents 8d3dd32 + b349fce commit 65d576b

File tree

6 files changed

+575
-33
lines changed

6 files changed

+575
-33
lines changed

Sources/XMLCoding/Decoder/XMLDecoder.swift

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ extension _XMLDecoder {
345345

346346
/// Returns the given value unboxed from a container.
347347
internal func unbox(_ value: Any, as type: Bool.Type) throws -> Bool? {
348-
guard !(value is NSNull) else { return nil }
348+
guard !(value is MissingValue) else { return nil }
349349

350350
guard let value = value as? String else {
351351
return nil
@@ -361,7 +361,7 @@ extension _XMLDecoder {
361361
}
362362

363363
internal func unbox(_ value: Any, as type: Int.Type) throws -> Int? {
364-
guard !(value is NSNull) else { return nil }
364+
guard !(value is MissingValue) else { return nil }
365365

366366
guard let string = value as? String else { return nil }
367367

@@ -373,7 +373,7 @@ extension _XMLDecoder {
373373
}
374374

375375
internal func unbox(_ value: Any, as type: Int8.Type) throws -> Int8? {
376-
guard !(value is NSNull) else { return nil }
376+
guard !(value is MissingValue) else { return nil }
377377

378378
guard let string = value as? String else { return nil }
379379

@@ -385,7 +385,7 @@ extension _XMLDecoder {
385385
}
386386

387387
internal func unbox(_ value: Any, as type: Int16.Type) throws -> Int16? {
388-
guard !(value is NSNull) else { return nil }
388+
guard !(value is MissingValue) else { return nil }
389389

390390
guard let string = value as? String else { return nil }
391391

@@ -397,7 +397,7 @@ extension _XMLDecoder {
397397
}
398398

399399
internal func unbox(_ value: Any, as type: Int32.Type) throws -> Int32? {
400-
guard !(value is NSNull) else { return nil }
400+
guard !(value is MissingValue) else { return nil }
401401

402402
guard let string = value as? String else { return nil }
403403

@@ -409,7 +409,7 @@ extension _XMLDecoder {
409409
}
410410

411411
internal func unbox(_ value: Any, as type: Int64.Type) throws -> Int64? {
412-
guard !(value is NSNull) else { return nil }
412+
guard !(value is MissingValue) else { return nil }
413413

414414
guard let string = value as? String else { return nil }
415415

@@ -421,7 +421,7 @@ extension _XMLDecoder {
421421
}
422422

423423
internal func unbox(_ value: Any, as type: UInt.Type) throws -> UInt? {
424-
guard !(value is NSNull) else { return nil }
424+
guard !(value is MissingValue) else { return nil }
425425

426426
guard let string = value as? String else { return nil }
427427

@@ -433,7 +433,7 @@ extension _XMLDecoder {
433433
}
434434

435435
internal func unbox(_ value: Any, as type: UInt8.Type) throws -> UInt8? {
436-
guard !(value is NSNull) else { return nil }
436+
guard !(value is MissingValue) else { return nil }
437437

438438
guard let string = value as? String else { return nil }
439439

@@ -445,7 +445,7 @@ extension _XMLDecoder {
445445
}
446446

447447
internal func unbox(_ value: Any, as type: UInt16.Type) throws -> UInt16? {
448-
guard !(value is NSNull) else { return nil }
448+
guard !(value is MissingValue) else { return nil }
449449

450450
guard let string = value as? String else { return nil }
451451

@@ -457,7 +457,7 @@ extension _XMLDecoder {
457457
}
458458

459459
internal func unbox(_ value: Any, as type: UInt32.Type) throws -> UInt32? {
460-
guard !(value is NSNull) else { return nil }
460+
guard !(value is MissingValue) else { return nil }
461461

462462
guard let string = value as? String else { return nil }
463463

@@ -469,7 +469,7 @@ extension _XMLDecoder {
469469
}
470470

471471
internal func unbox(_ value: Any, as type: UInt64.Type) throws -> UInt64? {
472-
guard !(value is NSNull) else { return nil }
472+
guard !(value is MissingValue) else { return nil }
473473

474474
guard let string = value as? String else { return nil }
475475

@@ -481,7 +481,7 @@ extension _XMLDecoder {
481481
}
482482

483483
internal func unbox(_ value: Any, as type: Float.Type) throws -> Float? {
484-
guard !(value is NSNull) else { return nil }
484+
guard !(value is MissingValue) else { return nil }
485485

486486
guard let string = value as? String else { return nil }
487487

@@ -493,7 +493,7 @@ extension _XMLDecoder {
493493
}
494494

495495
internal func unbox(_ value: Any, as type: Double.Type) throws -> Double? {
496-
guard !(value is NSNull) else { return nil }
496+
guard !(value is MissingValue) else { return nil }
497497

498498
guard let string = value as? String else { return nil }
499499

@@ -505,7 +505,7 @@ extension _XMLDecoder {
505505
}
506506

507507
internal func unbox(_ value: Any, as type: String.Type) throws -> String? {
508-
guard !(value is NSNull) else { return nil }
508+
guard !(value is MissingValue) else { return nil }
509509

510510
guard let string = value as? String else {
511511
throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value)
@@ -515,7 +515,9 @@ extension _XMLDecoder {
515515
}
516516

517517
internal func unbox(_ value: Any, as type: Date.Type) throws -> Date? {
518-
guard !(value is NSNull) else { return nil }
518+
guard !(value is MissingValue) else {
519+
return nil
520+
}
519521

520522
switch self.options.dateDecodingStrategy {
521523
case .deferredToDate:
@@ -561,7 +563,7 @@ extension _XMLDecoder {
561563
}
562564

563565
internal func unbox(_ value: Any, as type: Data.Type) throws -> Data? {
564-
guard !(value is NSNull) else { return nil }
566+
guard !(value is MissingValue) else { return nil }
565567

566568
switch self.options.dataDecodingStrategy {
567569
case .deferredToData:
@@ -590,7 +592,7 @@ extension _XMLDecoder {
590592
}
591593

592594
internal func unbox(_ value: Any, as type: Decimal.Type) throws -> Decimal? {
593-
guard !(value is NSNull) else { return nil }
595+
guard !(value is MissingValue) else { return nil }
594596

595597
// Attempt to bridge from NSDecimalNumber.
596598
let doubleValue = try self.unbox(value, as: Double.self)!
@@ -620,7 +622,12 @@ extension _XMLDecoder {
620622
guard let decimal = try self.unbox(value, as: Decimal.self) else { return nil }
621623
decoded = decimal as! T
622624
} else {
623-
self.storage.push(container: value)
625+
if value is MissingValue {
626+
self.storage.push(container: [:])
627+
} else {
628+
self.storage.push(container: value)
629+
}
630+
624631
decoded = try type.init(from: self)
625632
self.storage.popContainer()
626633
}

0 commit comments

Comments
 (0)