Encode CSV cells in quotes and separate cells with commas

This commit is contained in:
Caleb Kleveter 2019-04-18 11:45:27 -05:00
parent 7a6fdd39db
commit c94dc26b1d
No known key found for this signature in database
GPG Key ID: B38DBD5CF2C98D69
3 changed files with 14 additions and 10 deletions

View File

@ -39,14 +39,14 @@ final class AsyncEncoder: Encoder {
switch self.container.section {
case .header:
try object.encode(to: self)
self.onRow(Array(self.container.cells.joined()))
self.onRow(Array(self.container.cells.joined(separator: [44])))
self.container.section = .row
self.container.rowCount += 1
self.container.cells = []
fallthrough
case .row:
try object.encode(to: self)
self.onRow(Array(self.container.cells.joined()))
self.onRow(Array(self.container.cells.joined(separator: [44])))
self.container.rowCount += 1
self.container.cells = []
}

View File

@ -11,8 +11,12 @@ final class AsyncKeyedEncoder<K>: KeyedEncodingContainerProtocol where K: Coding
func _encode(_ value: [UInt8], for key: K) {
switch self.encoder.container.section {
case .header: self.encoder.container.cells.append(key.stringValue.bytes)
case .row: self.encoder.container.cells.append(value)
case .header:
let bytes = Array([[34], key.stringValue.bytes, [34]].joined())
self.encoder.container.cells.append(bytes)
case .row:
let bytes = Array([[34], value, [34]].joined())
self.encoder.container.cells.append(bytes)
}
}
func _encode(_ value: [UInt8]?, for key: K)throws {
@ -32,8 +36,8 @@ final class AsyncKeyedEncoder<K>: KeyedEncodingContainerProtocol where K: Coding
self._encode(value, for: key)
}
func encode(_ value: Double, forKey key: K) throws { self._encode(value.bytes, for: key) }
func encode(_ value: Float, forKey key: K) throws { self._encode(value.bytes, for: key) }
func encode(_ value: Int, forKey key: K) throws { self._encode(value.bytes, for: key) }
func encode(_ value: Float, forKey key: K) throws { self._encode(value.bytes, for: key) }
func encode(_ value: Int, forKey key: K) throws { self._encode(value.bytes, for: key) }
func encode(_ value: String, forKey key: K) throws { self._encode(value.bytes, for: key) }
func encode<T>(_ value: T, forKey key: K) throws where T : Encodable {

View File

@ -8,7 +8,7 @@ final class AsyncSingleValueEncoder: SingleValueEncodingContainer {
self.codingPath = path
self.encoder = encoder
}
func encodeNil() throws {
let value = self.encoder.encodingOptions.nilCodingStrategy.bytes()
self.encoder.container.cells.append(value)
@ -19,9 +19,9 @@ final class AsyncSingleValueEncoder: SingleValueEncodingContainer {
}
func encode(_ value: String) throws { self.encoder.container.cells.append(value.bytes) }
func encode(_ value: Double) throws { self.encoder.container.cells.append(value.bytes) }
func encode(_ value: Float) throws { self.encoder.container.cells.append(value.bytes) }
func encode(_ value: Int) throws { self.encoder.container.cells.append(value.bytes) }
func encode(_ value: Float) throws { self.encoder.container.cells.append(value.bytes) }
func encode(_ value: Int) throws { self.encoder.container.cells.append(value.bytes) }
func encode<T>(_ value: T) throws where T : Encodable {
let column = self.codingPath.map { $0.stringValue }.joined(separator: ".")
throw EncodingError.invalidValue(value, EncodingError.Context(