Delimit (delimit v0.1.0)

View Source

Delimit: A library for defining and working with delimited data files.

Delimit allows you to define a schema for delimited data files (CSV, TSV, etc.) and provides functions for reading, writing, and manipulating that data. The library automatically generates structs based on your schema definition, complete with proper typespecs.

Example

defmodule MyApp.Person do
  use Delimit

  layout do
    field :first_name, :string
    field :last_name, :string
    field :age, :integer
    field :birthday, :date, format: "YYYY-MM-DD"
    field :active, :boolean
  end
end

# Read data from a file (auto-detects CSV format)
people = MyApp.Person.read("people.csv")

# Read data with explicit format (tab-separated values)
people = MyApp.Person.read("people.tsv", format: :tsv)

# Write data to a file
MyApp.Person.write("new_people.csv", people)

# Write data with explicit format (pipe-separated values)
MyApp.Person.write("new_people.psv", people, format: :psv)

# Work with a specific record
first_person = Enum.at(people, 0)
IO.puts("Name: #{first_person.first_name} #{first_person.last_name}")

Summary

Functions

Defines an embedded schema.

Defines an embedded schema with options.

Defines a field in the schema.

Defines a field in the schema with options.

Defines the layout of a delimited file.

Functions

embeds_one(name, module)

(macro)

Defines an embedded schema.

Parameters

  • name - The name for the embedded schema
  • module - The module defining the embedded schema

Example

embeds_one :address, MyApp.Address
embeds_one :billing_address, MyApp.Address, prefix: "billing"

embeds_one(name, module, opts)

(macro)

Defines an embedded schema with options.

Parameters

  • name - The name for the embedded schema
  • module - The module defining the embedded schema
  • opts - Options for the embedded schema

Options

  • :prefix - Prefix to add to field headers (default: field name + "_")

Example

embeds_one :address, MyApp.Address, prefix: "addr_"

field(name, type)

(macro)

Defines a field in the schema.

Parameters

  • name - The name of the field as atom
  • type - The data type of the field (:string, :integer, :float, etc.)

Example

field :first_name, :string

field(name, type, opts)

(macro)

Defines a field in the schema with options.

Parameters

  • name - The name of the field as atom
  • type - The data type of the field (:string, :integer, :float, etc.)
  • opts - Options for the field

Options

  • :format - Format string for date/time fields
  • :default - Default value if the field is missing
  • :nil_on_empty - If true, empty strings become nil (default: true)
  • :true_values - List of values to interpret as true for boolean fields
  • :false_values - List of values to interpret as false for boolean fields
  • :read_fn - Custom function to parse the raw field value
  • :write_fn - Custom function to convert the field value to string
  • :label - Custom header label for this field (instead of the field name)
  • :struct_type - The type to use in the struct (different from file type)

Example

field :birthday, :date, format: "YYYY-MM-DD"
field :active, :boolean, true_values: ["Y", "YES"], false_values: ["N", "NO"]
field :email, :string, label: "contact_email"
field :tags, :string, read_fn: &split_tags/1, write_fn: &join_tags/1, struct_type: {:list, :string}

layout(list)

(macro)

Defines the layout of a delimited file.

Example

layout do
  field :first_name, :string
  field :last_name, :string
  field :age, :integer
end