csv_schema v0.1.0 Csv.Schema.Parser
Utility module containing some functions for parsing and to read csv file
Link to this section Summary
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
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
Given a csv file path try to parse as csv with headers. Use list of maps as data representation
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
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
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
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
Link to this section Functions
atom!(value)
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
boolean!(value)
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
csv!(path)
Given a csv file path try to parse as csv with headers. Use list of maps as data representation
date!(value, format)
date!(String.t(), String.t()) :: DateTime.t() | nil | no_return()
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'
float!(value)
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
integer!(value)
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
string!(value)
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