resx_csv v0.1.0 ResxCSV.Decoder
Decode CSV string resources into erlang terms.
Media Types
Only CSV/TSV types are valid. This can either be a CSV/TSV subtype or suffix.
Valid: text/csv
, application/geo+csv
, text/tab-separated-values
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 decoded, that can be done by configuring the :csv_types
option.
config :resx_csv,
csv_types: [
{ "application/x.my-type", "application/x.erlang.native", ?; }
]
The :csv_types
field should contain a list of 3 element tuples with the format { pattern :: String.pattern | Regex.t, replacement :: String.t, separator :: char }
.
The pattern
and replacement
are arguments to String.replace/3
. While the separator specifies the character literal used to separate columns in the document.
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 csv
type part is), with x.erlang.native
, in order to denote that the content is now a native erlang type. If this behaviour is not desired simply override the match with :csv_types
for the media types that should not be handled like this.
Options
:skip_errors
- expects a boolean
value, defaults to false
. This option specifies whether any decoding/formatting errors in the CSV should be skipped. If they are skipped then those rows will not appear in the decoded result, if should not be skipped then the decoding fails if there are any errors.
:separator
- expects a char
value, defaults to the separator literal that is returned for the given MIME/csv_type. This option allows for the separator to be overriden.
:headers
- expects a boolean
value, defaults to true
. This option specifies whether the first row will be used as a header.
:strip_fields
- expects a boolean
value, defaults to false
. This option specifies whether any surrounding whitespace will be trimmed.
: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.
Resx.Resource.transform(resource, ResxCSV.Decoder, skip_errors: true, headers: false)