csv_schema v0.2.4 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
field :first_name, "first_name", filter_by: true
field :last_name, "last_name", sort: :asc
field :identifier, ["first_name", "last_name"], key: true, join: " "
field :email, "email", unique: true
field :gender, "gender", filter_by: true, sort: :desc
field :ip_address, "ip_address"
field :date_of_birth, "date_of_birth", parser: &Parser.date!(&1, "{0M}/{0D}/{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/1expecting integer as arg__MODULE__.filter_by_name/1expecting string as arg__MODULE__.by_fiscal_code/1expecting string as arg__MODULE__.get_all/0Some 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 Types
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 values:key- boolean; at most one key must be set. It is something similar to a primary key:unique- boolean; creates a functionby_{name}for you:filter_by- boolean; do i create afilter_by_{name}function for this field for you?:parser- function; parser function used to get_changeset data from string to custom type:sort-:ascor:desc; It sorts according to Erlang's term ordering withnilexception:join- string; if present it joins the given fields into a binary using the separator
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