From 58152c6ee764e2ea66b5ae9a4ed53ba3bae711c3 Mon Sep 17 00:00:00 2001 From: Ben Wetherfield Date: Tue, 30 Jul 2019 23:50:28 -0700 Subject: [PATCH 1/3] Add test for empty struct choice elements --- Tests/XMLCoderTests/NestedChoiceTests.swift | 44 +++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Tests/XMLCoderTests/NestedChoiceTests.swift b/Tests/XMLCoderTests/NestedChoiceTests.swift index 4810c454..44344c49 100644 --- a/Tests/XMLCoderTests/NestedChoiceTests.swift +++ b/Tests/XMLCoderTests/NestedChoiceTests.swift @@ -227,6 +227,50 @@ class NestedChoiceTests: XCTestCase { XCTAssertEqual(result, expected) } + func testNestedEnumsWithEmptyStruct() throws { + let xml = """ + +

+

+ + 1518 + I am answering it again. + + + 431 + A Word About Wake Times + +

+

+ + 1519 + I am answering it again. + +
+

+
+ """ + let result = try XMLDecoder().decode(Container.self, from: xml.data(using: .utf8)!) + let expected = Container( + paragraphs: [ + Paragraph( + entries: [ + .br(Break()), + .run(Run(id: 1518, text: "I am answering it again.")), + .properties(Properties(id: 431, title: "A Word About Wake Times")), + ] + ), + Paragraph( + entries: [ + .run(Run(id: 1519, text: "I am answering it again.")), + .br(Break()), + ] + ), + ] + ) + XCTAssertEqual(result, expected) + } + func testNestedEnumsRoundTrip() throws { let original = Container( paragraphs: [ From c87b068f9fe873b60aef93777b87e53669372778 Mon Sep 17 00:00:00 2001 From: Ben Wetherfield Date: Sat, 20 Jul 2019 23:19:37 -0700 Subject: [PATCH 2/3] Wrap NullBox for decoding --- .../XMLCoder/Auxiliaries/KeyedStorage.swift | 2 +- .../Decoder/XMLKeyedDecodingContainer.swift | 26 ++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift b/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift index 49a82d0d..740455fa 100644 --- a/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift +++ b/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift @@ -83,7 +83,7 @@ extension KeyedStorage where Key == String, Value == Box { } else if let value = element.value { result.append(StringBox(value), at: element.key) } else { - result.append(NullBox(), at: element.key) + result.append(SingleKeyedBox(key: element.key, element: NullBox()), at: element.key) } return result diff --git a/Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift b/Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift index cbf42eb2..4380315a 100644 --- a/Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift +++ b/Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift @@ -69,8 +69,14 @@ struct XMLKeyedDecodingContainer: KeyedDecodingContainerProtocol { } public func decodeNil(forKey key: Key) throws -> Bool { - let elements = container.withShared { keyedBox in - keyedBox.elements[key.stringValue] + let elements = container.withShared { keyedBox -> [Box] in + keyedBox.elements[key.stringValue].map { + if let singleKeyed = $0 as? SingleKeyedBox { + return singleKeyed.element + } else { + return $0 + } + } } let attributes = container.withShared { keyedBox in @@ -232,14 +238,26 @@ extension XMLKeyedDecodingContainer { let keyString = key.stringValue.isEmpty ? "value" : key.stringValue let value = keyedBox.elements[keyString] if !value.isEmpty { - return value + return value.map { + if let singleKeyed = $0 as? SingleKeyedBox { + return singleKeyed.element + } else { + return $0 + } + } } else if let value = keyedBox.value { return [value] } else { return [] } } else { - return keyedBox.elements[key.stringValue] + return keyedBox.elements[key.stringValue].map { + if let singleKeyed = $0 as? SingleKeyedBox { + return singleKeyed.element + } else { + return $0 + } + } } } From fd0a3a88510d099582b3b0296f0baac186c2ada9 Mon Sep 17 00:00:00 2001 From: Ben Wetherfield Date: Wed, 31 Jul 2019 00:30:05 -0700 Subject: [PATCH 3/3] Refactor needless map --- .../Decoder/XMLKeyedDecodingContainer.swift | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift b/Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift index 4380315a..e236db91 100644 --- a/Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift +++ b/Sources/XMLCoder/Decoder/XMLKeyedDecodingContainer.swift @@ -69,14 +69,8 @@ struct XMLKeyedDecodingContainer: KeyedDecodingContainerProtocol { } public func decodeNil(forKey key: Key) throws -> Bool { - let elements = container.withShared { keyedBox -> [Box] in - keyedBox.elements[key.stringValue].map { - if let singleKeyed = $0 as? SingleKeyedBox { - return singleKeyed.element - } else { - return $0 - } - } + let elements = container.withShared { keyedBox in + keyedBox.elements[key.stringValue] } let attributes = container.withShared { keyedBox in @@ -85,6 +79,10 @@ struct XMLKeyedDecodingContainer: KeyedDecodingContainerProtocol { let box = elements.first ?? attributes.first + if let singleKeyed = box as? SingleKeyedBox { + return singleKeyed.element.isNull + } + return box?.isNull ?? true }