From a6df277ba8c7a38b09b3ab91ee94c76424ecf49e Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 2 May 2019 10:37:11 +0100 Subject: [PATCH 1/3] Rename flatten to transformToBoxTree, rename tests --- Sources/XMLCoder/Auxiliaries/KeyedStorage.swift | 10 +++++++--- Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift | 2 +- Sources/XMLCoder/Auxiliaries/XMLStackParser.swift | 2 +- Tests/XMLCoderTests/Minimal/FlattenTests.swift | 6 +++--- XMLCoder.xcodeproj/project.pbxproj | 8 ++++---- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift b/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift index 12b48873..7c743ae4 100644 --- a/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift +++ b/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift @@ -33,8 +33,12 @@ struct KeyedStorage { return buffer.count } - var keys: Buffer.Keys { - return buffer.keys + var keys: [Key] { + return order + } + + var values: [Value] { + return order.compactMap { buffer[$0] } } init(_ sequence: S) where S: Sequence, S.Element == (Key, Value) { @@ -102,7 +106,7 @@ private extension KeyedStorage where Key == String, Value == Box { let hasValue = element.value != nil let key = element.key - let content = element.flatten() + let content = element.transformToBoxTree() switch self[key] { case var unkeyedBox as UnkeyedBox: unkeyedBox.append(content) diff --git a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift index 206ddcb8..766adb3a 100644 --- a/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift +++ b/Sources/XMLCoder/Auxiliaries/XMLCoderElement.swift @@ -46,7 +46,7 @@ struct XMLCoderElement: Equatable { elements.append(element) } - func flatten() -> KeyedBox { + func transformToBoxTree() -> KeyedBox { let attributes = KeyedStorage(self.attributes.map { key, value in (key: key, value: StringBox(value) as SimpleBox) }) diff --git a/Sources/XMLCoder/Auxiliaries/XMLStackParser.swift b/Sources/XMLCoder/Auxiliaries/XMLStackParser.swift index 8bc40552..6aada60d 100644 --- a/Sources/XMLCoder/Auxiliaries/XMLStackParser.swift +++ b/Sources/XMLCoder/Auxiliaries/XMLStackParser.swift @@ -32,7 +32,7 @@ class XMLStackParser: NSObject { shouldProcessNamespaces: shouldProcessNamespaces ) - return node.flatten() + return node.transformToBoxTree() } func parse( diff --git a/Tests/XMLCoderTests/Minimal/FlattenTests.swift b/Tests/XMLCoderTests/Minimal/FlattenTests.swift index bd1cde92..feb224ff 100644 --- a/Tests/XMLCoderTests/Minimal/FlattenTests.swift +++ b/Tests/XMLCoderTests/Minimal/FlattenTests.swift @@ -8,7 +8,7 @@ import XCTest @testable import XMLCoder -class FlattenTests: XCTestCase { +class BoxTreeTests: XCTestCase { func testNestedValues() { let e1 = XMLCoderElement( key: "foo", @@ -29,9 +29,9 @@ class FlattenTests: XCTestCase { attributes: [:] ) - let flattened = root.flatten() + let boxTree = root.transformToBoxTree() - guard let foo = flattened.elements["foo"] as? UnkeyedBox else { + guard let foo = boxTree.elements["foo"] as? UnkeyedBox else { XCTAssert( false, """ diff --git a/XMLCoder.xcodeproj/project.pbxproj b/XMLCoder.xcodeproj/project.pbxproj index e5425f99..e20cbb1f 100644 --- a/XMLCoder.xcodeproj/project.pbxproj +++ b/XMLCoder.xcodeproj/project.pbxproj @@ -95,7 +95,7 @@ D1CFC8242226B13F00B03222 /* NamespaceTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1CFC8222226AFB400B03222 /* NamespaceTest.swift */; }; D1E0C85321D8E65E0042A261 /* ErrorContextTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1E0C85121D8E6540042A261 /* ErrorContextTest.swift */; }; D1E0C85521D91EBF0042A261 /* Metatypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1E0C85421D91EBF0042A261 /* Metatypes.swift */; }; - D1EC3E62225A32F500C610E3 /* FlattenTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1EC3E61225A32F500C610E3 /* FlattenTests.swift */; }; + D1EC3E62225A32F500C610E3 /* BoxTreeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1EC3E61225A32F500C610E3 /* BoxTreeTests.swift */; }; D1EC3E65225A38EC00C610E3 /* KeyedStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1EC3E64225A38EC00C610E3 /* KeyedStorage.swift */; }; D1FC040521C7EF8200065B43 /* RJISample.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1FC040421C7EF8200065B43 /* RJISample.swift */; }; OBJ_48 /* DecodingErrorExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_10 /* DecodingErrorExtension.swift */; }; @@ -216,7 +216,7 @@ D1CFC8222226AFB400B03222 /* NamespaceTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NamespaceTest.swift; sourceTree = ""; }; D1E0C85121D8E6540042A261 /* ErrorContextTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ErrorContextTest.swift; sourceTree = ""; }; D1E0C85421D91EBF0042A261 /* Metatypes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Metatypes.swift; sourceTree = ""; }; - D1EC3E61225A32F500C610E3 /* FlattenTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlattenTests.swift; sourceTree = ""; }; + D1EC3E61225A32F500C610E3 /* BoxTreeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoxTreeTests.swift; sourceTree = ""; }; D1EC3E63225A334C00C610E3 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = ""; }; D1EC3E64225A38EC00C610E3 /* KeyedStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeyedStorage.swift; sourceTree = ""; }; D1FC040421C7EF8200065B43 /* RJISample.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RJISample.swift; sourceTree = ""; }; @@ -353,7 +353,7 @@ A61FE03721E4D4F10015D993 /* UnkeyedIntTests.swift */, BF9457E721CBB6BC005ACFDE /* UnkeyedTests.swift */, BF63EF0921CD7C1A001D38C5 /* URLTests.swift */, - D1EC3E61225A32F500C610E3 /* FlattenTests.swift */, + D1EC3E61225A32F500C610E3 /* BoxTreeTests.swift */, ); path = Minimal; sourceTree = ""; @@ -656,7 +656,7 @@ B3B6902E220A71DF0084D407 /* AttributedIntrinsicTest.swift in Sources */, D11815C1227788C8008836E4 /* SpacePreserveTest.swift in Sources */, D1FC040521C7EF8200065B43 /* RJISample.swift in Sources */, - D1EC3E62225A32F500C610E3 /* FlattenTests.swift in Sources */, + D1EC3E62225A32F500C610E3 /* BoxTreeTests.swift in Sources */, BF63EF0A21CD7C1A001D38C5 /* URLTests.swift in Sources */, BF9457CE21CBB516005ACFDE /* StringBoxTests.swift in Sources */, D1CFC8242226B13F00B03222 /* NamespaceTest.swift in Sources */, From 55eaaed2cee913e66598862fd48c0f27f9a6d741 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 2 May 2019 11:11:46 +0100 Subject: [PATCH 2/3] Rename FlattenTests file and header --- .../Minimal/{FlattenTests.swift => BoxTreeTests.swift} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Tests/XMLCoderTests/Minimal/{FlattenTests.swift => BoxTreeTests.swift} (97%) diff --git a/Tests/XMLCoderTests/Minimal/FlattenTests.swift b/Tests/XMLCoderTests/Minimal/BoxTreeTests.swift similarity index 97% rename from Tests/XMLCoderTests/Minimal/FlattenTests.swift rename to Tests/XMLCoderTests/Minimal/BoxTreeTests.swift index feb224ff..7daa6e21 100644 --- a/Tests/XMLCoderTests/Minimal/FlattenTests.swift +++ b/Tests/XMLCoderTests/Minimal/BoxTreeTests.swift @@ -1,5 +1,5 @@ // -// FlattenTests.swift +// BoxTreeTests.swift // XMLCoderTests // // Created by Max Desiatov on 07/04/2019. From 328cb56eee2c2c90055474b2c8d96562f5f6781b Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Thu, 2 May 2019 11:39:47 +0100 Subject: [PATCH 3/3] Update KeyedStorage.swift --- Sources/XMLCoder/Auxiliaries/KeyedStorage.swift | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift b/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift index 7c743ae4..9ae780eb 100644 --- a/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift +++ b/Sources/XMLCoder/Auxiliaries/KeyedStorage.swift @@ -37,10 +37,6 @@ struct KeyedStorage { return order } - var values: [Value] { - return order.compactMap { buffer[$0] } - } - init(_ sequence: S) where S: Sequence, S.Element == (Key, Value) { order = sequence.map { $0.0 } buffer = Dictionary(uniqueKeysWithValues: sequence)