-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This updates the `XMLStackParser` to accept a parameter called `removeWhitespaceElements`. The purpose of the `XMLStackParser` is to call the XML parser and create a tree of `XMLCoderElement` representing the structure of the parsed XML. Assuming that XMLStackParser has `trimValueWhitespaces` set to `false`, when attempting to parse a nested data structure like the following: ```xml <SomeType> <nestedStringList> <member> <member>foo</member> <member>bar</member> </member> <member> <member>baz</member> <member>qux</member> </member> </nestedStringList> </SomeType> ``` ... then there will multiple `XMLCoderElement`s in the tree which will have `elements` set to elements that are either: a. Purely whitespaced elements or b. The child elements These purely whitespaced elements are problematic for users who are implementing custom `Decoder` logic, as they are interpreted as regular child elements. Therefore, setting `removeWhitespaceElements` to `true` while `trimValueWhitespaces` is set to `false`, will remove these whitespace elements during the construction of the `XMLCoderElement` tree. An in-depth analysis of the original problem can be found [here](#219). For historical purposes, a separate approach was implemented. It uses a similar algorithm in a different part of the code. #221
- Loading branch information
Showing
10 changed files
with
303 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) 2018-2020 XMLCoder contributors | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
// | ||
// Created by John Woo on 7/29/21. | ||
// | ||
|
||
import Foundation | ||
|
||
import XCTest | ||
@testable import XMLCoder | ||
|
||
class NestedStringList: XCTestCase { | ||
|
||
struct TypeWithNestedStringList: Decodable { | ||
let nestedStringList: [[String]] | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case nestedStringList | ||
} | ||
|
||
enum NestedMemberKeys: String, CodingKey { | ||
case member | ||
} | ||
|
||
public init (from decoder: Decoder) throws { | ||
let containerValues = try decoder.container(keyedBy: CodingKeys.self) | ||
let nestedStringListWrappedContainer = try containerValues.nestedContainer(keyedBy: NestedMemberKeys.self, forKey: .nestedStringList) | ||
let nestedStringListContainer = try nestedStringListWrappedContainer.decodeIfPresent([[String]].self, forKey: .member) | ||
var nestedStringListBuffer:[[String]] = [] | ||
if let nestedStringListContainer = nestedStringListContainer { | ||
nestedStringListBuffer = [[String]]() | ||
var listBuffer0: [String]? = nil | ||
for listContainer0 in nestedStringListContainer { | ||
listBuffer0 = [String]() | ||
for stringContainer1 in listContainer0 { | ||
listBuffer0?.append(stringContainer1) | ||
} | ||
if let listBuffer0 = listBuffer0 { | ||
nestedStringListBuffer.append(listBuffer0) | ||
} | ||
} | ||
} | ||
nestedStringList = nestedStringListBuffer | ||
} | ||
} | ||
|
||
func testRemoveWhitespaceElements() throws { | ||
let decoder = XMLDecoder(trimValueWhitespaces: false, removeWhitespaceElements: true) | ||
let xmlString = | ||
""" | ||
<TypeWithNestedStringList> | ||
<nestedStringList> | ||
<member> | ||
<member>foo: &lt;
 </member> | ||
<member>bar: &lt;
 </member> | ||
</member> | ||
<member> | ||
<member>baz: &lt;
 </member> | ||
<member>qux: &lt;
 </member> | ||
</member> | ||
</nestedStringList> | ||
</TypeWithNestedStringList> | ||
""" | ||
let xmlData = xmlString.data(using: .utf8)! | ||
|
||
let decoded = try decoder.decode(TypeWithNestedStringList.self, from: xmlData) | ||
XCTAssertEqual(decoded.nestedStringList[0][0], "foo: <\r\n") | ||
XCTAssertEqual(decoded.nestedStringList[0][1], "bar: <\r\n") | ||
XCTAssertEqual(decoded.nestedStringList[1][0], "baz: <\r\n") | ||
XCTAssertEqual(decoded.nestedStringList[1][1], "qux: <\r\n") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters