csv_schema v0.2.1 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

Link to this function

atom!(value)
atom!(String.t() | atom()) :: atom() | nil | no_return()

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
Link to this function

boolean!(value)
boolean!(String.t()) :: boolean() | nil | no_return()

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
Link to this function

csv!(path, headers, separator)
csv!(String.t(), boolean(), pos_integer()) :: map() | no_return()

Given a csv file path try to parse as csv with headers. Use list of maps as data representation

Link to this function

date!(value, format)
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'
Link to this function

float!(value)
float!(String.t()) :: number() | nil | no_return()

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
Link to this function

integer!(value)
integer!(String.t()) :: number() | nil | no_return()

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
Link to this function

string!(value)
string!(String.t() | atom()) :: String.t() | nil | no_return()

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