Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new writer configuration option 'quoteAll' #47

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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: 1 addition & 1 deletion sources/imperative/writer/Writer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ extension CSVWriter {

// 3.A. If escaping is allowed.
if let escapingScalar = self.settings.escapingScalar {
var (index, needsEscaping) = (0, false)
var (index, needsEscaping) = (0, self.settings.quoteAll)
// 4. Iterate through all the input's Unicode scalars.
while index < input.endIndex {
let scalar = input[index]
Expand Down
3 changes: 3 additions & 0 deletions sources/imperative/writer/WriterConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ extension CSVWriter {
public var delimiters: Delimiter.Pair
/// The strategy to allow/disable escaped fields and how.
public var escapingStrategy: Strategy.Escaping
/// Whether to quote all fields (as opposed to quoting only fields that need to be escaped)
public var quoteAll: Bool
/// The row of headers to write at the beginning of the CSV data.
///
/// If empty, no row will be written.
Expand All @@ -25,6 +27,7 @@ extension CSVWriter {
self.delimiters = (field: ",", row: "\n")
self.escapingStrategy = .doubleQuote
self.headers = Array()
self.quoteAll = false
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions sources/imperative/writer/internal/WriterInternals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ extension CSVWriter {
let delimiters: (field: [Unicode.Scalar], row: [Unicode.Scalar])
/// The unicode scalar used as encapsulator and escaping character (when printed two times).
let escapingScalar: Unicode.Scalar?
/// Whether to quote all fields (as opposed to quoting only fields that need to be escaped)
let quoteAll: Bool
/// Boolean indicating whether the received CSV contains a header row or not.
let headers: [String]
/// The encoding used to identify the underlying data.
Expand All @@ -52,6 +54,7 @@ extension CSVWriter {
}!)
// 2. Copy all other values.
self.escapingScalar = configuration.escapingStrategy.scalar
self.quoteAll = configuration.quoteAll
self.headers = configuration.headers
self.encoding = encoding
}
Expand Down
20 changes: 20 additions & 0 deletions tests/declarative/CodableNumericBoolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ extension CodableNumericBoolTests {
XCTAssertEqual(rows, try decoder.decode([_Row].self, from: shuffledString.data(using: .utf8)!))
}

/// Tests the quoteAll configuration option
func testQuoteAll() throws {
let rows = [_Row(a: true, b: false, c: nil),
_Row(a: true, b: true, c: false),
_Row(a: false, b: false, c: true)]
let string = """
"a","b","c"
"1","0",
"1","1","0"
"0","0","1"
""".appending("\n")

let encoder = CSVEncoder {
$0.headers = ["a", "b", "c"]
$0.boolStrategy = .numeric
$0.quoteAll = true
}
XCTAssertEqual(string, try encoder.encode(rows, into: String.self))
}

/// Tests the error throwing/handling.
func testThrows() throws {
// b = nil on 2nd row, must throw an exception.
Expand Down