Csv.Schema (csv_schema v1.1.0) View Source

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: "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

It is possible to define the schema with data: param in order to directly use a string to geerate content

...
@content """
id,first_name,last_name,email,gender,ip_address,date_of_birth
1,Ivory,Overstreet,ioverstreet0@businessweek.com,Female,30.138.91.62,10/22/2018
2,Ulick,Vasnev,uvasnev1@vkontakte.ru,Male,35.15.164.70,01/19/2018
3,Chloe,Freemantle,cfreemantle2@parallels.com,Female,133.133.113.255,08/13/2018
"""

schema data: @content do
...
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 nil.
  • filter_by_{field_name} returns list of records matching provided property.
  • get_all returns all records

Back 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

  • separator it's possible to set a separator argument to macro to let the macro split csv for you using provided separator.
  • header 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

Specs

name() :: String.t() | atom()

Specs

order() :: :asc | :desc

Specs

row() :: map() | list()

Link to this section Functions

Link to this macro

__using__(opts)

View Source (macro)
  • separator it's possible to set a separator argument to macro to let the macro split csv for you using provided separator.
  • header if your csv file does not have headers, it's possible to set headers to false and configure fields by index (1..N)
Link to this macro

field(name, col, opts \\ [])

View Source (macro)

Configure a new field (csv column). Parameters are

  • name - new struct field name
  • column - header name or column index (if headers: false) in csv file
  • opts - 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 function by_{name} for you
    • :filter_by - boolean; do i create a filter_by_{name} function for this field for you?
    • :parser - function; parser function used to get_changeset data from string to custom type
    • :sort - :asc or :desc; It sorts according to Erlang's term ordering with nil exception
    • :join - string; if present it joins the given fields into a binary using the separator
Link to this macro

schema(list1, list2)

View Source (macro)

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