Delimit.Field (delimit v0.4.1)
View SourceDefines field types, options, and conversion functions for Delimit schemas.
This module handles field definitions, data type conversions, and validation for delimited data format parsing and generation.
Summary
Types
Boolean field configuration options.
Date field configuration options.
General field configuration options.
Supported field types.
Field definition structure.
Functions
Returns true if the field is a derived/computed type whose value comes from the parsing pipeline rather than from a column in the input file.
Creates a new field definition.
Parses a raw string value into the specified type.
Converts a value to a string representation for writing to a delimited file.
Types
Boolean field configuration options.
Date field configuration options.
:format- A single Timex/ISO format string for parsing and writing:formats- A list of format strings to try in order on read (mutually exclusive with:format). The first format that successfully parses wins. Useful for files that contain mixed date formats (e.g. mostlyM/D/YYYYwith occasionalYYYY-MM-DD). Writing always uses the first format in the list.
@type field_opts() :: [ optional: boolean(), default: any(), read_fn: (String.t() -> any()), write_fn: (any() -> String.t()), nil_on_empty: boolean(), label: String.t(), struct_type: field_type() | boolean_opts() | date_opts() ]
General field configuration options.
:optional- Whether the field is optional (default: false):default- Default value if the field is missing:read_fn- Custom function to parse the raw field value:write_fn- Custom function to convert the field value to string:nil_on_empty- If true, empty strings become nil (default: true):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)
@type field_type() :: :string | :integer | :float | :boolean | :date | :datetime | :embed | :row_hash | :raw_row | {:list, field_type()} | {:map, field_type()} | {:map, field_type(), field_type()}
Supported field types.
Basic field types:
:string- String values:integer- Integer values:float- Floating point values:boolean- Boolean values:date- Date values:datetime- DateTime values:embed- Embedded struct
Complex type annotations (for struct_type option):
{:list, inner_type}- A list where each element is of typeinner_type{:map, key_type, value_type}- A map with keys ofkey_typeand values ofvalue_type{:map, value_type}- A map with atom keys and values ofvalue_type
Field definition structure.
:name- The name of the field:type- The data type of the field:opts- Additional options for the field
Functions
Returns true if the field is a derived/computed type whose value comes from the parsing pipeline rather than from a column in the input file.
Derived fields are skipped during write, do not consume input columns on read, and do not contribute to canonical encoding.
@spec new(atom(), field_type(), field_opts()) :: t()
Creates a new field definition.
Parameters
name- The name of the field as an atomtype- The type of the field (:string,:integer, etc.)opts- A keyword list of options for the field
Example
iex> Delimit.Field.new(:first_name, :string, [])
%Delimit.Field{name: :first_name, type: :string, opts: []}
iex> Delimit.Field.new(:age, :integer, [default: 0])
%Delimit.Field{name: :age, type: :integer, opts: [default: 0]}
Parses a raw string value into the specified type.
Parameters
value- The raw string value from the delimited filefield- The field definition
Returns
- The parsed value or nil if the value is empty and nil_on_empty is true
Example
iex> field = Delimit.Field.new(:age, :integer)
iex> Delimit.Field.parse_value("42", field)
42
iex> field = Delimit.Field.new(:active, :boolean)
iex> Delimit.Field.parse_value("Yes", field)
true
Converts a value to a string representation for writing to a delimited file.
Parameters
value- The value to convertfield- The field definition
Returns
- The string representation of the value
Example
iex> field = Delimit.Field.new(:age, :integer)
iex> Delimit.Field.to_string(42, field)
"42"
iex> field = Delimit.Field.new(:active, :boolean)
iex> Delimit.Field.to_string(true, field)
"true"