-
Notifications
You must be signed in to change notification settings - Fork 104
/
NestedStringList.swift
74 lines (65 loc) · 2.8 KB
/
NestedStringList.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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")
}
}