From b70ef0af75be53df60b4e342aee98ac08037231e Mon Sep 17 00:00:00 2001 From: Ben Wetherfield Date: Sat, 13 Jul 2019 11:16:34 -0700 Subject: [PATCH 01/10] Run swiftformat --- .../Decoder/XMLDecoderImplementation.swift | 5 +- .../EnumAssociatedValueTestComposite.swift | 2 - .../EnumAssociatedValueTestSimple.swift | 4 +- .../NestedEnumAssociatedValueTest.swift | 12 +-- Tests/XMLCoderTests/NestingTests.swift | 2 + .../xcschemes/XMLCoder-Package.xcscheme | 81 +++++++++++++++++++ 6 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 XMLCoder.xcodeproj/xcshareddata/xcschemes/XMLCoder-Package.xcscheme diff --git a/Sources/XMLCoder/Decoder/XMLDecoderImplementation.swift b/Sources/XMLCoder/Decoder/XMLDecoderImplementation.swift index 69bc4300..344e51df 100644 --- a/Sources/XMLCoder/Decoder/XMLDecoderImplementation.swift +++ b/Sources/XMLCoder/Decoder/XMLDecoderImplementation.swift @@ -146,8 +146,9 @@ class XMLDecoderImplementation: Decoder { return XMLUnkeyedDecodingContainer( referencing: self, wrapping: keyed.withShared { - SharedBox($0.elements.map { (key, element) in - KeyedBox(elements: KeyedStorage([(key, element)]), attributes: .init()) } + SharedBox($0.elements.map { key, element in + KeyedBox(elements: KeyedStorage([(key, element)]), attributes: .init()) + } ) } ) diff --git a/Tests/XMLCoderTests/EnumAssociatedValueTestComposite.swift b/Tests/XMLCoderTests/EnumAssociatedValueTestComposite.swift index 54df48db..4455281d 100644 --- a/Tests/XMLCoderTests/EnumAssociatedValueTestComposite.swift +++ b/Tests/XMLCoderTests/EnumAssociatedValueTestComposite.swift @@ -22,7 +22,6 @@ private enum IntOrStringWrapper: Equatable { } extension IntOrStringWrapper: Decodable { - enum CodingKeys: String, CodingKey { case int case string @@ -39,7 +38,6 @@ extension IntOrStringWrapper: Decodable { } class EnumAssociatedValueTestComposite: XCTestCase { - func testIntOrStringWrapper() throws { let xml = """ diff --git a/Tests/XMLCoderTests/EnumAssociatedValueTestSimple.swift b/Tests/XMLCoderTests/EnumAssociatedValueTestSimple.swift index ec5cc599..51c60635 100644 --- a/Tests/XMLCoderTests/EnumAssociatedValueTestSimple.swift +++ b/Tests/XMLCoderTests/EnumAssociatedValueTestSimple.swift @@ -18,6 +18,7 @@ extension IntOrString: Decodable { case int case string } + init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) do { @@ -28,10 +29,9 @@ extension IntOrString: Decodable { } } -extension IntOrString: Equatable { } +extension IntOrString: Equatable {} class EnumAssociatedValuesTest: XCTestCase { - func testIntOrStringIntDecoding() throws { let xml = "42" let result = try XMLDecoder().decode(IntOrString.self, from: xml.data(using: .utf8)!) diff --git a/Tests/XMLCoderTests/NestedEnumAssociatedValueTest.swift b/Tests/XMLCoderTests/NestedEnumAssociatedValueTest.swift index 506f18df..53f41e0e 100644 --- a/Tests/XMLCoderTests/NestedEnumAssociatedValueTest.swift +++ b/Tests/XMLCoderTests/NestedEnumAssociatedValueTest.swift @@ -32,7 +32,7 @@ private struct Properties: Decodable, Equatable { let title: String } -private struct Break: Decodable, Equatable { } +private struct Break: Decodable, Equatable {} extension Container: Decodable { private enum CodingKeys: String, CodingKey { @@ -43,7 +43,7 @@ extension Container: Decodable { extension Paragraph: Decodable { init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() - self.entries = try container.decode([Entry].self) + entries = try container.decode([Entry].self) } } @@ -51,6 +51,7 @@ extension Entry: Decodable { private enum CodingKeys: String, CodingKey { case run, properties, br } + public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) do { @@ -66,7 +67,6 @@ extension Entry: Decodable { } class NestedEnumAssociatedValueTest: XCTestCase { - func testBreakDecoding() throws { let xml = "

" let result = try XMLDecoder().decode(Break.self, from: xml.data(using: .utf8)!) @@ -142,7 +142,7 @@ class NestedEnumAssociatedValueTest: XCTestCase { let result = try XMLDecoder().decode([Entry].self, from: xml.data(using: .utf8)!) let expected: [Entry] = [ .run(Run(id: 1518, text: "I am answering it again.")), - .properties(Properties(id: 431, title: "A Word About Wake Times")) + .properties(Properties(id: 431, title: "A Word About Wake Times")), ] XCTAssertEqual(result, expected) } @@ -164,7 +164,7 @@ class NestedEnumAssociatedValueTest: XCTestCase { let expected = Paragraph( entries: [ .run(Run(id: 1518, text: "I am answering it again.")), - .properties(Properties(id: 431, title: "A Word About Wake Times")) + .properties(Properties(id: 431, title: "A Word About Wake Times")), ] ) XCTAssertEqual(result, expected) @@ -204,7 +204,7 @@ class NestedEnumAssociatedValueTest: XCTestCase { entries: [ .run(Run(id: 1519, text: "I am answering it again.")), ] - ) + ), ] ) XCTAssertEqual(result, expected) diff --git a/Tests/XMLCoderTests/NestingTests.swift b/Tests/XMLCoderTests/NestingTests.swift index 6aa48d3f..32100c23 100644 --- a/Tests/XMLCoderTests/NestingTests.swift +++ b/Tests/XMLCoderTests/NestingTests.swift @@ -91,6 +91,7 @@ final class NestingTests: XCTestCase { """ let encoded = xml.data(using: .utf8)! + print(try decoder.decode(type(of: unkeyedWithinKeyed), from: encoded)) XCTAssertNoThrow(try decoder.decode(type(of: unkeyedWithinKeyed), from: encoded)) } @@ -127,6 +128,7 @@ final class NestingTests: XCTestCase { """ let encoded = xml.data(using: .utf8)! + print(try decoder.decode(type(of: keyedWithinKeyed), from: encoded)) XCTAssertNoThrow(try decoder.decode(type(of: keyedWithinKeyed), from: encoded)) } diff --git a/XMLCoder.xcodeproj/xcshareddata/xcschemes/XMLCoder-Package.xcscheme b/XMLCoder.xcodeproj/xcshareddata/xcschemes/XMLCoder-Package.xcscheme new file mode 100644 index 00000000..c117e5d6 --- /dev/null +++ b/XMLCoder.xcodeproj/xcshareddata/xcschemes/XMLCoder-Package.xcscheme @@ -0,0 +1,81 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From d19fd94e80834b08238d8355439dd1a1fdb35d23 Mon Sep 17 00:00:00 2001 From: Ben Wetherfield Date: Sat, 13 Jul 2019 11:25:07 -0700 Subject: [PATCH 02/10] Remove prints --- Tests/XMLCoderTests/NestingTests.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/Tests/XMLCoderTests/NestingTests.swift b/Tests/XMLCoderTests/NestingTests.swift index 32100c23..6aa48d3f 100644 --- a/Tests/XMLCoderTests/NestingTests.swift +++ b/Tests/XMLCoderTests/NestingTests.swift @@ -91,7 +91,6 @@ final class NestingTests: XCTestCase { """ let encoded = xml.data(using: .utf8)! - print(try decoder.decode(type(of: unkeyedWithinKeyed), from: encoded)) XCTAssertNoThrow(try decoder.decode(type(of: unkeyedWithinKeyed), from: encoded)) } @@ -128,7 +127,6 @@ final class NestingTests: XCTestCase { """ let encoded = xml.data(using: .utf8)! - print(try decoder.decode(type(of: keyedWithinKeyed), from: encoded)) XCTAssertNoThrow(try decoder.decode(type(of: keyedWithinKeyed), from: encoded)) } From cc4b7ccaf539faf74db7d0c769c7643874b03023 Mon Sep 17 00:00:00 2001 From: Ben Wetherfield Date: Sat, 13 Jul 2019 11:26:29 -0700 Subject: [PATCH 03/10] Remove xcscheme --- .../xcschemes/XMLCoder-Package.xcscheme | 81 ------------------- 1 file changed, 81 deletions(-) delete mode 100644 XMLCoder.xcodeproj/xcshareddata/xcschemes/XMLCoder-Package.xcscheme diff --git a/XMLCoder.xcodeproj/xcshareddata/xcschemes/XMLCoder-Package.xcscheme b/XMLCoder.xcodeproj/xcshareddata/xcschemes/XMLCoder-Package.xcscheme deleted file mode 100644 index c117e5d6..00000000 --- a/XMLCoder.xcodeproj/xcshareddata/xcschemes/XMLCoder-Package.xcscheme +++ /dev/null @@ -1,81 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From 717f56c347bb096ac14bcc38aed2881a1478ffe5 Mon Sep 17 00:00:00 2001 From: Ben Wetherfield Date: Sat, 13 Jul 2019 11:40:55 -0700 Subject: [PATCH 04/10] Add concrete decoding tests --- Tests/XMLCoderTests/NestingTests.swift | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Tests/XMLCoderTests/NestingTests.swift b/Tests/XMLCoderTests/NestingTests.swift index 6aa48d3f..62470030 100644 --- a/Tests/XMLCoderTests/NestingTests.swift +++ b/Tests/XMLCoderTests/NestingTests.swift @@ -73,8 +73,10 @@ final class NestingTests: XCTestCase { """ let encoded = xml.data(using: .utf8)! - - XCTAssertNoThrow(try decoder.decode(type(of: unkeyedWithinUnkeyed), from: encoded)) + let expected = [[1,2,3], [1,2,3]] + let decoded = try decoder.decode(type(of: unkeyedWithinUnkeyed), from: encoded) + + XCTAssertEqual(expected, decoded) } func testDecodeUnkeyedWithinKeyed() throws { @@ -90,8 +92,10 @@ final class NestingTests: XCTestCase { """ let encoded = xml.data(using: .utf8)! - - XCTAssertNoThrow(try decoder.decode(type(of: unkeyedWithinKeyed), from: encoded)) + let expected = ["first":[1,2,3], "second":[1,2,3]] + let decoded = try decoder.decode(type(of: unkeyedWithinKeyed), from: encoded) + + XCTAssertEqual(expected, decoded) } func testDecodeKeyedWithinUnkeyed() throws { @@ -107,8 +111,10 @@ final class NestingTests: XCTestCase { """ let encoded = xml.data(using: .utf8)! + let expected = [["first": 1], ["second": 2]] + let decoded = try decoder.decode(type(of: keyedWithinUnkeyed), from: encoded) - XCTAssertNoThrow(try decoder.decode(type(of: keyedWithinUnkeyed), from: encoded)) + XCTAssertEqual(expected, decoded) } func testDecodeKeyedWithinKeyed() throws { @@ -126,8 +132,10 @@ final class NestingTests: XCTestCase { """ let encoded = xml.data(using: .utf8)! + let expected = ["first": ["a": 1, "b": 2], "second": ["c": 3, "d": 4]] + let decoded = try decoder.decode(type(of: keyedWithinKeyed), from: encoded) - XCTAssertNoThrow(try decoder.decode(type(of: keyedWithinKeyed), from: encoded)) + XCTAssertEqual(expected, decoded) } static var allTests = [ From 315583b35177ba8842b4731bfad02fb8b89d689e Mon Sep 17 00:00:00 2001 From: Ben Wetherfield Date: Sat, 13 Jul 2019 11:41:35 -0700 Subject: [PATCH 05/10] Fix formatting --- Tests/XMLCoderTests/NestingTests.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/XMLCoderTests/NestingTests.swift b/Tests/XMLCoderTests/NestingTests.swift index 62470030..98c164f8 100644 --- a/Tests/XMLCoderTests/NestingTests.swift +++ b/Tests/XMLCoderTests/NestingTests.swift @@ -73,9 +73,9 @@ final class NestingTests: XCTestCase { """ let encoded = xml.data(using: .utf8)! - let expected = [[1,2,3], [1,2,3]] + let expected = [[1, 2, 3], [1, 2, 3]] let decoded = try decoder.decode(type(of: unkeyedWithinUnkeyed), from: encoded) - + XCTAssertEqual(expected, decoded) } @@ -92,9 +92,9 @@ final class NestingTests: XCTestCase { """ let encoded = xml.data(using: .utf8)! - let expected = ["first":[1,2,3], "second":[1,2,3]] + let expected = ["first": [1, 2, 3], "second": [1, 2, 3]] let decoded = try decoder.decode(type(of: unkeyedWithinKeyed), from: encoded) - + XCTAssertEqual(expected, decoded) } From 0e459b1d6b2dc9e11f4f7248062e50aeb6ca0b8b Mon Sep 17 00:00:00 2001 From: Ben Wetherfield Date: Sat, 13 Jul 2019 12:04:10 -0700 Subject: [PATCH 06/10] Remove type(of:) definitions --- Tests/XMLCoderTests/NestingTests.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/XMLCoderTests/NestingTests.swift b/Tests/XMLCoderTests/NestingTests.swift index 98c164f8..b4a550c0 100644 --- a/Tests/XMLCoderTests/NestingTests.swift +++ b/Tests/XMLCoderTests/NestingTests.swift @@ -74,7 +74,7 @@ final class NestingTests: XCTestCase { """ let encoded = xml.data(using: .utf8)! let expected = [[1, 2, 3], [1, 2, 3]] - let decoded = try decoder.decode(type(of: unkeyedWithinUnkeyed), from: encoded) + let decoded = try decoder.decode([[Int]].self, from: encoded) XCTAssertEqual(expected, decoded) } @@ -93,7 +93,7 @@ final class NestingTests: XCTestCase { """ let encoded = xml.data(using: .utf8)! let expected = ["first": [1, 2, 3], "second": [1, 2, 3]] - let decoded = try decoder.decode(type(of: unkeyedWithinKeyed), from: encoded) + let decoded = try decoder.decode([String: [Int]].self, from: encoded) XCTAssertEqual(expected, decoded) } @@ -112,7 +112,7 @@ final class NestingTests: XCTestCase { """ let encoded = xml.data(using: .utf8)! let expected = [["first": 1], ["second": 2]] - let decoded = try decoder.decode(type(of: keyedWithinUnkeyed), from: encoded) + let decoded = try decoder.decode([[String: Int]].self, from: encoded) XCTAssertEqual(expected, decoded) } @@ -133,7 +133,7 @@ final class NestingTests: XCTestCase { """ let encoded = xml.data(using: .utf8)! let expected = ["first": ["a": 1, "b": 2], "second": ["c": 3, "d": 4]] - let decoded = try decoder.decode(type(of: keyedWithinKeyed), from: encoded) + let decoded = try decoder.decode([String: [String: Int]].self, from: encoded) XCTAssertEqual(expected, decoded) } From f0e1d0433678d7b824d033e7ef61acb5cd75e2d7 Mon Sep 17 00:00:00 2001 From: Ben Wetherfield Date: Sat, 13 Jul 2019 17:18:26 -0700 Subject: [PATCH 07/10] Fix roundtrip test --- Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift index 65584337..aa478611 100644 --- a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift +++ b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift @@ -230,7 +230,9 @@ struct XMLCoderElement: Equatable { string += "" } } else if !elements.isEmpty { - string += prettyPrinted ? ">\n" : ">" + if !key.isEmpty { + string += prettyPrinted ? ">\n" : ">" + } formatXMLElements(formatting, &string, level, cdata, prettyPrinted) string += indentation @@ -250,7 +252,7 @@ struct XMLCoderElement: Equatable { extension XMLCoderElement { init(key: String, box: UnkeyedBox) { self.init(key: key, elements: box.map { - XMLCoderElement(key: key, box: $0) + XMLCoderElement(key: "", box: $0) }) } From e57b271b122ce8a9dc521d310677ad704c5075ff Mon Sep 17 00:00:00 2001 From: Ben Wetherfield Date: Sat, 13 Jul 2019 21:22:45 -0700 Subject: [PATCH 08/10] Add pattern matching to encoding step --- Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift index aa478611..03ddc6eb 100644 --- a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift +++ b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift @@ -252,7 +252,12 @@ struct XMLCoderElement: Equatable { extension XMLCoderElement { init(key: String, box: UnkeyedBox) { self.init(key: key, elements: box.map { - XMLCoderElement(key: "", box: $0) + switch $0 { + case _ as KeyedBox: + return XMLCoderElement(key: "", box: $0) + default: + return XMLCoderElement(key: key, box: $0) + } }) } From 85dcf9742e1bfd8a0dd3e825a0ed37c2affefdc7 Mon Sep 17 00:00:00 2001 From: Ben Wetherfield Date: Sun, 14 Jul 2019 18:40:56 +0100 Subject: [PATCH 09/10] Improve patternmatching syntax --- Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift index 03ddc6eb..d170707d 100644 --- a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift +++ b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift @@ -252,10 +252,9 @@ struct XMLCoderElement: Equatable { extension XMLCoderElement { init(key: String, box: UnkeyedBox) { self.init(key: key, elements: box.map { - switch $0 { - case _ as KeyedBox: + if $0 is KeyedBox { return XMLCoderElement(key: "", box: $0) - default: + } else { return XMLCoderElement(key: key, box: $0) } }) From a9e4a27564d23ec1ff01dc3c91bde4690426b3de Mon Sep 17 00:00:00 2001 From: Ben Wetherfield Date: Sun, 14 Jul 2019 18:41:18 +0100 Subject: [PATCH 10/10] Fix formatting --- Tests/XMLCoderTests/EnumAssociatedValueTestSimple.swift | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Tests/XMLCoderTests/EnumAssociatedValueTestSimple.swift b/Tests/XMLCoderTests/EnumAssociatedValueTestSimple.swift index 05c4e356..f5a0265d 100644 --- a/Tests/XMLCoderTests/EnumAssociatedValueTestSimple.swift +++ b/Tests/XMLCoderTests/EnumAssociatedValueTestSimple.swift @@ -14,7 +14,6 @@ private enum IntOrString { } extension IntOrString: Codable { - enum CodingKeys: String, CodingKey { case int case string @@ -23,9 +22,9 @@ extension IntOrString: Codable { func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) switch self { - case .int(let value): + case let .int(value): try container.encode(value, forKey: .int) - case .string(let value): + case let .string(value): try container.encode(value, forKey: .string) } }