From 83b79225ce4ed4b5f13487a4eb7b3eb58c6295dd Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 24 May 2019 10:31:59 +0100 Subject: [PATCH 1/3] Fix decoding of Borders model type --- CoreXLSX.xcodeproj/project.pbxproj | 4 ++++ Sources/CoreXLSX/Styles.swift | 2 +- Tests/CoreXLSXTests/Border.swift | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 Tests/CoreXLSXTests/Border.swift diff --git a/CoreXLSX.xcodeproj/project.pbxproj b/CoreXLSX.xcodeproj/project.pbxproj index d9b517ef..7f15b92b 100644 --- a/CoreXLSX.xcodeproj/project.pbxproj +++ b/CoreXLSX.xcodeproj/project.pbxproj @@ -49,6 +49,7 @@ D1B4F8E1221C10AC00C53C42 /* Path.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1B4F8DF221C10AC00C53C42 /* Path.swift */; }; D1B4F8E2221C10AC00C53C42 /* Path.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1B4F8DF221C10AC00C53C42 /* Path.swift */; }; D1B4F8E3221C10AC00C53C42 /* Path.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1B4F8DF221C10AC00C53C42 /* Path.swift */; }; + D1B6A2C42297F0F3005B8A6E /* Border.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1B6A2C32297F0F3005B8A6E /* Border.swift */; }; D1BBD9C6223D0E2F00B28C7B /* Styles.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1BBD9C5223D0E2F00B28C7B /* Styles.swift */; }; D1BBD9C7223D0E2F00B28C7B /* Styles.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1BBD9C5223D0E2F00B28C7B /* Styles.swift */; }; D1BBD9C8223D0E2F00B28C7B /* Styles.swift in Sources */ = {isa = PBXBuildFile; fileRef = D1BBD9C5223D0E2F00B28C7B /* Styles.swift */; }; @@ -125,6 +126,7 @@ D1A8190A21A9D0EF004FCA33 /* Cell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Cell.swift; sourceTree = ""; }; D1A8191021A9D4ED004FCA33 /* CellQueries.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CellQueries.swift; sourceTree = ""; }; D1B4F8DF221C10AC00C53C42 /* Path.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Path.swift; sourceTree = ""; }; + D1B6A2C32297F0F3005B8A6E /* Border.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Border.swift; sourceTree = ""; }; D1BBD9C5223D0E2F00B28C7B /* Styles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Styles.swift; sourceTree = ""; }; D1BBD9D3223D3D5C00B28C7B /* Styles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Styles.swift; sourceTree = ""; }; D1C96B7F21A806A500303975 /* Workbook.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Workbook.swift; sourceTree = ""; }; @@ -309,6 +311,7 @@ D1EB1B3321B151440043CD1E /* SharedStrings.swift */, D1BBD9D3223D3D5C00B28C7B /* Styles.swift */, D12211DB227B555600888BCB /* Formula.swift */, + D1B6A2C32297F0F3005B8A6E /* Border.swift */, ); name = CoreXLSXTests; path = Tests/CoreXLSXTests; @@ -720,6 +723,7 @@ buildActionMask = 0; files = ( D1C96B8321A80EC000303975 /* Workbook.swift in Sources */, + D1B6A2C42297F0F3005B8A6E /* Border.swift in Sources */, D1769CCC225F456A00521A47 /* Namespaces.swift in Sources */, OBJ_90 /* CellReference.swift in Sources */, D1EB1B3421B151440043CD1E /* SharedStrings.swift in Sources */, diff --git a/Sources/CoreXLSX/Styles.swift b/Sources/CoreXLSX/Styles.swift index 2bcc5a47..91f4c9b8 100644 --- a/Sources/CoreXLSX/Styles.swift +++ b/Sources/CoreXLSX/Styles.swift @@ -152,7 +152,7 @@ public struct PatternFill: Codable, Equatable { } public struct Borders: Codable, Equatable { - public let items: [Border] + public let items: [Border?] public let count: Int enum CodingKeys: String, CodingKey { diff --git a/Tests/CoreXLSXTests/Border.swift b/Tests/CoreXLSXTests/Border.swift new file mode 100644 index 00000000..f4a04473 --- /dev/null +++ b/Tests/CoreXLSXTests/Border.swift @@ -0,0 +1,24 @@ +// +// Border.swift +// CoreXLSXTests +// +// Created by Max Desiatov on 24/05/2019. +// + +import CoreXLSX +import XCTest +@testable import XMLCoder + +private let xml = """ + + + +""".data(using: .utf8)! + +final class BorderTest: XCTestCase { + func testSingleEmpty() throws { + let result = try XMLDecoder().decode(Borders.self, from: xml) + XCTAssertEqual(result.count, 1) + XCTAssertEqual(result.items[0], nil) + } +} From 4bf747c831fa4afa04749d853635a26d4f32db26 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 24 May 2019 12:36:18 +0100 Subject: [PATCH 2/3] Restore compatibility with previous model types --- CoreXLSX.xcodeproj/project.pbxproj | 12 ++++++------ Sources/CoreXLSX/Styles.swift | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/CoreXLSX.xcodeproj/project.pbxproj b/CoreXLSX.xcodeproj/project.pbxproj index 7f15b92b..50049de9 100644 --- a/CoreXLSX.xcodeproj/project.pbxproj +++ b/CoreXLSX.xcodeproj/project.pbxproj @@ -301,17 +301,17 @@ OBJ_17 /* CoreXLSXTests */ = { isa = PBXGroup; children = ( - D1769CCA225F456300521A47 /* Namespaces.swift */, + D1B6A2C32297F0F3005B8A6E /* Border.swift */, OBJ_18 /* CellReference.swift */, OBJ_19 /* CoreXLSX.swift */, + D12211DB227B555600888BCB /* Formula.swift */, + D1769CCA225F456300521A47 /* Namespaces.swift */, OBJ_20 /* Relationships.swift */, - OBJ_21 /* XCTestManifests.swift */, - D1C96B8121A80E5900303975 /* Workbook.swift */, - D1A8190821A9CB89004FCA33 /* Worksheet.swift */, D1EB1B3321B151440043CD1E /* SharedStrings.swift */, D1BBD9D3223D3D5C00B28C7B /* Styles.swift */, - D12211DB227B555600888BCB /* Formula.swift */, - D1B6A2C32297F0F3005B8A6E /* Border.swift */, + D1C96B8121A80E5900303975 /* Workbook.swift */, + D1A8190821A9CB89004FCA33 /* Worksheet.swift */, + OBJ_21 /* XCTestManifests.swift */, ); name = CoreXLSXTests; path = Tests/CoreXLSXTests; diff --git a/Sources/CoreXLSX/Styles.swift b/Sources/CoreXLSX/Styles.swift index 91f4c9b8..f64398ef 100644 --- a/Sources/CoreXLSX/Styles.swift +++ b/Sources/CoreXLSX/Styles.swift @@ -152,13 +152,20 @@ public struct PatternFill: Codable, Equatable { } public struct Borders: Codable, Equatable { - public let items: [Border?] + public let items: [Border] public let count: Int enum CodingKeys: String, CodingKey { case items = "border" case count } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + count = try container.decode(Int.self, forKey: .count) + items = try container.decode([Border?].self, forKey: .items) + .map { $0 ?? Border() } + } } public struct Border: Codable, Equatable { @@ -174,6 +181,16 @@ public struct Border: Codable, Equatable { public let diagonal: Value? public let horizontal: Value? public let vertical: Value? + + init() { + left = nil + right = nil + top = nil + bottom = nil + diagonal = nil + horizontal = nil + vertical = nil + } } public struct CellStyleFormats: Codable, Equatable { From b47b4517ee32327c5759e28c3cfce2c0ae1c4d01 Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Fri, 24 May 2019 12:36:54 +0100 Subject: [PATCH 3/3] Fix test after Border model type changes --- Tests/CoreXLSXTests/Border.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/CoreXLSXTests/Border.swift b/Tests/CoreXLSXTests/Border.swift index f4a04473..b0b0dfc9 100644 --- a/Tests/CoreXLSXTests/Border.swift +++ b/Tests/CoreXLSXTests/Border.swift @@ -5,7 +5,7 @@ // Created by Max Desiatov on 24/05/2019. // -import CoreXLSX +@testable import CoreXLSX import XCTest @testable import XMLCoder @@ -19,6 +19,6 @@ final class BorderTest: XCTestCase { func testSingleEmpty() throws { let result = try XMLDecoder().decode(Borders.self, from: xml) XCTAssertEqual(result.count, 1) - XCTAssertEqual(result.items[0], nil) + XCTAssertEqual(result.items[0], Border()) } }