resx_csv v0.2.0 ResxCSV.Encoder

Encode data resources into strings of CSV.

Media Types

Only x.erlang.native types are valid. This can either be a subtype or suffix.

Valid: application/x.erlang.native, application/geo+x.erlang.native. If an error is being returned when attempting to open a data URI due to { :invalid_reference, "invalid media type: #{type}" }, the MIME type will need to be added to the config.

To add additional media types to be encoded, that can be done by configuring the :native_types option.

config :resx_csv,
    native_types: [
        { "application/x.my-type", &("application/#{&1}"), &(&1) }
    ]

The :native_types field should contain a list of 3 element tuples with the format { pattern :: String.pattern | Regex.t, (replacement_type :: String.t -> replacement :: String.t), preprocessor :: (Resx.Resource.content -> Resx.Resource.content) }.

The pattern and replacement are arguments to String.replace/3. While the preprocessor performs any operations on the content before it is encoded.

The replacement becomes the new media type of the transformed resource. Nested media types will be preserved. By default the current matches will be replaced (where the x.erlang.native type part is), with the new type (currently csv), in order to denote that the content is now a CSV type. If this behaviour is not desired simply override the match with :native_types for the media types that should not be handled like this.

Encoding

The CSV format (final encoding type) is specified when calling transform, by providing an atom or string to the :format option. This type is then used to infer how the content should be encoded, as well as what type will be used for the media type.

Resx.Resource.transform(resource, ResxCSV.Encoder, format: :csv)

The current formats are:

  • :csv - This encodes the data into CSV. This is the default encoding format.
  • :tsv - This encodes the data into TSV (tab-separated-values).

Otherwise can pass in any string to explicitly denote another format.

Resx.Resource.transform(resource, ResxCSV.Encoder, format: "dsv", separator: ?;)

Options

:format - expects an atom or String.t value, defaults to :csv. This option specifies the encoding format.

:separator - expects a char value, defaults to the separator literal that is returned for the given encoding format. This option allows for the separator to be overriden.

:delimiter - expects a String.t value, defaults to "\r\n". This option specifies the delimiter use to separate the different rows.

:headers - expects a boolean or list value, defaults to true. This option specifies whether the encoding should include headers or not. If false then no headers will be included in the final output, if true then the content will be parsed first in order to get the headers, if a list of keys then those keys will be used as the header. If a header is being used, and given a row of type map, it will use the header to retrieve the individual values, whereas if the row is a list then it will use the list as is. If the content is a stream and headers are being inferred, this may have unintended effects if any stage of the stream prior has any side-effects. To alleviate this, you should cache the previous stage of the stream.

:validate_row_length - expects a boolean value, defaults to true. This option specifies whether the row length needs to be the same or whether it can be of variable length. If true then it will enforce all rows are of equal size to the first row, so if a row is a list it should match the size as the previous rows or headers. If false then it will use any list row as is.