Csv.Schema.Parser (csv_schema v1.1.0) View Source

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.

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.

Link to this section Functions

Specs

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

Specs

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!(stream, headers, separator)

View Source

Specs

csv!(
  %Stream{accs: term(), done: term(), enum: term(), funs: term()},
  boolean(),
  pos_integer()
) ::
  %Stream{accs: term(), done: term(), enum: term(), funs: term()} | no_return()

Given a CSV file path try to parse as CSV with headers.

Use list of maps as data representation.

Specs

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'

Specs

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

Specs

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

Specs

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