DataMorph

Create Elixir structs from data.

Build StatusInline docsHex.pm

Documentation

You can view full DataMorph API documentation on hexdocs.

Installation

Add

{:data_morph, "~> 0.0.4"}

to your deps in mix.exs like so:

defp deps do
[
{:data_morph, "~> 0.0.4"}
]
end

Usage examples

Define a struct and return stream of structs created from a tsv string, a namespace atom and name string.

iex> "name\tiso\n" <>
...> "New Zealand\tnz\n" <>
...> "United Kingdom\tgb" \
...> |> DataMorph.structs_from_tsv(OpenRegister, "country") \
...> |> Enum.to_list
[
%OpenRegister.Country{iso: "nz", name: "New Zealand"},
%OpenRegister.Country{iso: "gb", name: "United Kingdom"}
]

Define a struct and return stream of structs created from a csv file stream, and a namespace string and name atom.

(echo name,iso && echo New Zealand,nz && echo United Kingdom,gb) > tmp.csv
iex> File.stream!('./tmp.csv') \
...> |> DataMorph.structs_from_csv("open-register", :iso_country) \
...> |> Enum.to_list
[
%OpenRegister.IsoCountry{iso: "nz", name: "New Zealand"},
%OpenRegister.IsoCountry{iso: "gb", name: "United Kingdom"}
]

Define a struct and puts stream of structs created from a stream of csv on standard input, and a namespace atom, and name string.

(echo name,iso && echo New Zealand,NZ) | \
mix run -e 'IO.puts inspect \
IO.stream(:stdio, :line) \
|> DataMorph.structs_from_csv(:ex, "ample") \
|> Enum.at(0)'
# %Ex.Ample{iso: "NZ", name: "New Zealand"}

Add additional new fields to struct when called again with different tsv.

iex> "name\tiso\n" <>
...> "New Zealand\tnz\n" <>
...> "United Kingdom\tgb" \
...> |> DataMorph.structs_from_tsv(OpenRegister, "country") \
...> |> Enum.to_list
...>
...> "name\tacronym\n" <>
...> "New Zealand\tNZ\n" <>
...> "United Kingdom\tUK" \
...> |> DataMorph.structs_from_tsv(OpenRegister, "country") \
...> |> Enum.to_list
[
%OpenRegister.Country{acronym: "NZ", iso: nil, name: "New Zealand"},
%OpenRegister.Country{acronym: "UK", iso: nil, name: "United Kingdom"}
]