csv_schema v0.2.3 Csv.Schema
Csv schema is a library helping you to build Ecto schema-like files having as source a csv file.
The idea behind this library is give the possibility to create, at compile-time, getters function for a CSV inside codebase.
APIs related to this macro are similar to Ecto.Schema; Eg.
defmodule Person do
use Csv.Schema, headers: true, separator: ?,
alias Csv.Schema.Parser
schema "path/to/person.csv" do
field :id, "ID", key: true, parser: &Parser.integer!/1
field :name, "Name", filter_by: true
field :fiscal_code, "Fiscal Code", unique: true
field :birth, "Date of birth", parser: &Parser.date!(&1, "{0D}/{0M}/{0YYYY}")
end
end
At the end of compilation now your module is a Struct and has 3 kind of getters:
by_{key_field_name}
-> returns single records object or nilfilter_by_{field_name}
-> returns list of records matching provided propertyget_all
-> returns all recordsBack to the example in the module will be created:
__MODULE__.by_id/1
expecting integer as arg__MODULE__.filter_by_name/1
expecting string as arg__MODULE__.by_fiscal_code/1
expecting string as arg__MODULE__.get_all/0
Some example definitions could be found here
Link to this section Summary
Functions
It's possible to set a :separator argument to macro to let the macro split csv for you using provided separator. Moreover, if your csv file does not have headers, it's possible to set headers to false and configure fields by index (1..N)
Configure a new field (csv column). Parameters are
schema macro helps you to build a block of fields. First parameter should be
the relative path to csv file in your project. Second parameter should be a field
list
included in do
-end
block
Link to this section Functions
It's possible to set a :separator argument to macro to let the macro split csv for you using provided separator. Moreover, if your csv file does not have headers, it's possible to set headers to false and configure fields by index (1..N)
Configure a new field (csv column). Parameters are
name
- new struct field namecolumn
- header name or column index (if headers: false) in csv fileopts
- list of configuration valueskey
- boolean; at most one key must be set. It is something similar to a primary keyfilter_by
- boolean; do i create afilter_by_{name}
function for this field for you?unique
- boolean; creates a functionby_{name}
for youparser
- function; parser function used to get_changeset data from string to custom type
schema macro helps you to build a block of fields. First parameter should be
the relative path to csv file in your project. Second parameter should be a field
list
included in do
-end
block