Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/CoreXLSX/Workbook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ public struct Workbook: Codable, Equatable {
public let name: String?
public let id: String
public let relationship: String
public let state: String?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In contrast to name, id and relationship - state seems to me more fit to be an enum.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please (re-)mark me for review so that I see it in my list of open PRs to review when you're ready

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I didn't use an enum because I've no idea what are the possible values. I've seen "visible", "hidden", and "veryHidden" XD
I guess people can add if they know about other states.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't know the exhaustive list of states, let's do a struct similar to this instead:

public struct State: RawRepresentable, Codable {
  public var rawValue: String
  
  public func encode(to encoder: Encoder) throws {
    var container = encoder.singleValueContainer()
    try container.encode(rawValue)
  }
  
  public init(from decoder: Decoder) throws {
    let container = try decoder.singleValueContainer()
    self.rawValue = try container.decode(String.self)
  }
  
  public static let visible = State(rawValue: "visible")
  public static let hidden = State(rawValue: "hidden")
  public static let veryHidden = State(rawValue: "veryHidden")
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please tag me again when you have a reply/updated the code. Sorry about the ping-ponging, but if we don't know the exhaustive list of cases, we cannot use an enum at this stage. Swift doesn't allow libraries to have unknown future cases using @unknown default:


enum CodingKeys: String, CodingKey {
case name
case id = "sheetId"
case relationship = "r:id"
case state
}
}

Expand Down
11 changes: 7 additions & 4 deletions Tests/CoreXLSXTests/Workbook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import XMLCoder
private let parsedSheet = [
Workbook.Sheet(name: "Sheet 1",
id: "1",
relationship: "rId4"),
relationship: "rId4",
state: nil),
]

// swiftlint:disable line_length
private let workbookNoViews =
"""
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main"><workbookPr/><sheets><sheet state="visible" name="Summary" sheetId="1" r:id="rId4"/><sheet state="visible" name="General" sheetId="2" r:id="rId5"/></sheets><definedNames/><calcPr/></workbook>
<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main"><workbookPr/><sheets><sheet state="visible" name="Summary" sheetId="1" r:id="rId4"/><sheet state="hidden" name="General" sheetId="2" r:id="rId5"/></sheets><definedNames/><calcPr/></workbook>
""".data(using: .utf8)!
// swiftlint:enable line_length

Expand All @@ -38,12 +39,14 @@ private let expectedWorkbook =
.init(
name: "Summary",
id: "1",
relationship: "rId4"
relationship: "rId4",
state: "visible"
),
.init(
name: "General",
id: "2",
relationship: "rId5"
relationship: "rId5",
state: "hidden"
),
]))

Expand Down