DataMorph

Create Elixir structs, maps with atom keys, and keyword lists from CSV/TSV data.

Build StatusInline docsHex.pm

Documentation

You can view full DataMorph API documentation on hexdocs.

Installation

Add

{:data_morph, "~> 0.0.8"}

to your deps in mix.exs like so:

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

Usage examples

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

"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
File.stream!(&#39;./tmp.csv&#39;) \
|> 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.

"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"}
# ]

You can view full DataMorph API documentation on hexdocs.