Git Product home page Git Product logo

csv's People

Contributors

calebkleveter avatar daskier avatar jonny7 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

csv's Issues

Row handling in Parser?

@calebkleveter I'm using CSV to load data into a database and I'm combining onHeader and onCell to effectively build a row dictionary. Do you think that is a common enough use case to merit including something like an onRow: handler in Parser.swift?

Escaped Quotes Dropping Characters

Characters are lost when parsing a slightly modified version of your test data:

let data = """ first name,last_name,age,gender,tagLine TinTin,,16,M,"=HYPERLINK(""https://en.wikipedia.org/wiki/The_Great_Snake"",""Great snakes!"")" """

CSV produces the following when processing the last field:
Source:
=HYPERLINK(""https://en.wikipedia.org/wiki/The_Great_Snake"",""Great snakes!"")
Parsed Result:
=HYPERLINK("https://en.wikipedia.org/wiki/The_Great_Snak""Great snakes"

its missing the 'e' in Snake, the ',' after Snake", the '!' and the '")" at the end:

I updated the cellDelimiter processing line 128 in Parser.swift with the following:

case true: index += 1; slice.end += 1

The test that failed with the above data now passes and with an update of the test data all your other tests pass as well.

Decoder expects different type than encoder

When encoding, we have to pass an array to the encoder:

try encoder.encode([obj1, obj2])`

but when decoding we have to pass the type of the object in the array and not the type of the array itself:

try decoder.decode(MyObject.self, from: data)

Even if this is briefly mentioned in the readme, it's not intuitive and unexpected, since when dealing with the familiar coders (JSONDecoder, PropertyListDecoder) we have to pass the same type on encoding and decoding.

Escape Quotes in Cells

When encoding or serializing data to CSV format, we don't escape double-quote characters ("), which will causes the data to be parsed incorrectly. We need to escape the quote characters in the cell contents by adding another double-quote before it:

"Exactly!" He replied

Becomes this:

""Exactly!"" He replied

KeyDecodingStrategy

A main use case I have for this package is ingesting CSV files in Swift from exported existing database tables. Many database tables have snake_cased column names or don't exactly match the property in Swift . It would be convenient to add a keyDecodingStrategy option similar to that of Swifts JSONDecoder so that the values can be mapped without having to rename the csv column headers.

Encoder doesn't escape cell separator

Take this code:

struct Asd: Codable {
    var asd: String
    var zuio = 0
}

let config = Config(cellSeparator: UInt8(ascii: ","), cellDelimiter: nil)
let encoder = CSVEncoder(configuration: config).sync
let data = try! encoder.encode([Asd(asd: "asd,fgh"), Asd(asd: "bla")])
let string = String(decoding: data, as: UTF8.self)
print(string)
let decoder = CSVDecoder(configuration: config).sync
do {
    let object = try decoder.decode(Asd.self, from: data)
    print(object)
} catch {
    print(error)
}

It fails with error:

"Cannot convert bytes `[102, 103, 104]` to type `Int`

When removing var zuio = 0 from the Asd struct, the encoding doesn't throw any error, but when decoded again, it results in 3 rows intead of 2:

[Asd(asd: "asd"), Asd(asd: "fgh"), Asd(asd: "bla")]

Only when removing the comma in the string passed to the Asd constructor everything goes well.

Support for delimiters other than ","?

I know this package is appropriately titled CSV, but has any thought been given to supporting delimiters other than ","?

I have a data set that I'm parsing right now that uses "\t" and I've modified the case statement in Parser.swift to be case 44,9: rather than case 44:. This isn't a great solution, but for the time being it works.

If you were interested in supporting this concept I would think abstracting the ascii code away would be a good idea. This could be done by either creating some sort of Delimiter enum or adding a Character parameter to the parse function.

Version 0.3.1 requires Swift 4.2

Between versions 0.3.0 and 0.3.1, a few changes in the code were introduced that require Swift 4.2 (which was not released yet), such as Bool.toggle().

I think changes that require different Swift version should be done in major versions or at least minor versions, not patch versions, as swift update happily fetched the version 0.3.1 as part of the regular update process, which then made the code uncompilable in the current stable version of Xcode/Swift.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.