Csv.Schema.Parser (csv_schema v2.0.2)
View SourceUtility module containing some functions for parsing and to read CSV file.
Summary
Functions
Having a string or an atom as input cast value to atom. If something else is given an exception is raised.
Having a string as input cast value to boolean.
Given a CSV file path try to parse as CSV with headers.
Having a string as input representing string date to parse and a string representing the date format try to cast value to date.
Having a string as input cast value to float.
Having a string as input cast value to integer.
Having a string or an atom as input cast value to string.
Functions
Having a string or an atom as input cast value to atom. If something else is given an exception is raised.
If given argument is empty string or nil return value will be nil.
Examples
iex> Csv.Schema.Parser.atom!("")
nil
iex> Csv.Schema.Parser.atom!(nil)
nil
iex> Csv.Schema.Parser.atom!("id")
:id
iex> Csv.Schema.Parser.atom!(:id)
:id
iex> Csv.Schema.Parser.atom!(1)
** (RuntimeError) Cannot cast '1' to atom
Having a string as input cast value to boolean.
If something else is given an exception is raised.
If given argument is empty string or nil return value will be nil.
Examples
iex> Csv.Schema.Parser.boolean!("")
nil
iex> Csv.Schema.Parser.boolean!(nil)
nil
iex> Csv.Schema.Parser.boolean!("true")
true
iex> Csv.Schema.Parser.boolean!("false")
false
iex> Csv.Schema.Parser.boolean!("1a")
** (RuntimeError) Cannot cast '1a' to boolean
@spec csv!(Enumerable.t(), boolean(), pos_integer()) :: Enumerable.t() | no_return()
Given a CSV file path try to parse as CSV with headers.
Use list of maps as data representation.
@spec date!(String.t(), String.t()) :: DateTime.t() | nil | no_return()
Having a string as input representing string date to parse and a string representing the date format try to cast value to date.
If something else is given or format is invalid or date is not parsable with given format an exception is raised. If given argument is empty string or nil return value will be nil.
Examples
iex> Csv.Schema.Parser.date!("", "whatever")
nil
iex> Csv.Schema.Parser.date!(nil, "whatever")
nil
iex> Csv.Schema.Parser.date!("18/01/2019", "{0D}/{0M}/{0YYYY}")
~N[2019-01-18 00:00:00]
iex> Csv.Schema.Parser.date!("18/01/2019", "{0M}/{0D}/{0YYYY}")
** (RuntimeError) Cannot cast '18/01/2019' to date with format '{0M}/{0D}/{0YYYY}'
iex> Csv.Schema.Parser.date!("18/01/2019", "MDY")
** (RuntimeError) Invalid date format 'MDY'
Having a string as input cast value to float.
If something else is given an exception is raised. If given argument is empty string or nil return value will be nil.
Examples
iex> Csv.Schema.Parser.float!("")
nil
iex> Csv.Schema.Parser.float!(nil)
nil
iex> Csv.Schema.Parser.float!("1.2")
1.2
iex> Csv.Schema.Parser.float!("1a")
** (RuntimeError) Cannot cast '1a' to float
Having a string as input cast value to integer.
If something else is given an exception is raised. If given argument is empty string or nil return value will be nil.
Examples
iex> Csv.Schema.Parser.integer!("")
nil
iex> Csv.Schema.Parser.integer!(nil)
nil
iex> Csv.Schema.Parser.integer!("1")
1
iex> Csv.Schema.Parser.integer!("1a")
** (RuntimeError) Cannot cast '1a' to integer
Having a string or an atom as input cast value to string.
If something else is given an exception is raised. If given argument is empty string or nil return value will be nil.
Examples
iex> Csv.Schema.Parser.string!("")
nil
iex> Csv.Schema.Parser.string!(nil)
nil
iex> Csv.Schema.Parser.string!("id")
"id"
iex> Csv.Schema.Parser.string!(:id)
"id"
iex> Csv.Schema.Parser.string!(1)
** (RuntimeError) Cannot cast '1' to string