dashbitco / nimble_csv

A simple and fast CSV parsing and dumping library for Elixir
https://hexdocs.pm/nimble_csv
767 stars 51 forks source link

Allow overriding separator in parse functions #68

Closed Dvogiatz closed 2 years ago

Dvogiatz commented 2 years ago

Hello, and apologies if this already discussed.

We could update the init_parser/1 like this:

   defp init_parser(opts) do
        state = if Keyword.get(opts, :skip_headers, true), do: :header, else: :line
        separator = :binary.compile_pattern(Keyword.get(opts, :separator, @separator))
        {state, separator, :binary.compile_pattern(@escape)}
      end

That way the user is allowed to override the parser separator (and maybe also escape).

I understand that this contradicts with the idea that each Parser has a specified functionality, but when you have to deal with different kind of separated files (commas, semicolons, tabs, spaces, pipes, etc), it would really help to avoid creating multiple modules for just in case or having to rewrite-recompile-redeploy if/when a new format arrives.

josevalim commented 2 years ago

It unfortunately won’t work. The whole deal about NimbleCSV is to generate parsers at compile time for performance, so we can’t guarantee you can override options at runtime.

Dvogiatz commented 2 years ago

@josevalim Alright, thank you Jose