Skip to content

Easily perform a deep in-place key remapping on lists and maps in Elixir.

License

Notifications You must be signed in to change notification settings

dhaspden/transmog

Repository files navigation

transmog

Easily perform deep in-place data mappings for keys on lists and maps.

Table Of Contents

About

transmog is a simple module which allows for in-place data mapping for keys on deeply nested lists and maps. One use case for this library is to convert external data into a shape that is compatible with an internal data structure.

Installation

To install transmog add the following to your mix.exs file and run mix deps.get in your project root directory.

defp deps do
  [{:transmog, "~> 0.1"}]
end

Getting Started

The simplest way to use transmog is to first create a key mapping for your data using the following.

defmodule TransmogExample do
  @key_mapping [
    {"account", ":user"},
    {"account.identity", ":user.:details"},
    {"account.identity.first_name", ":user.:details.:first_name"}
  ]
end

This key mapping says that the account key in a map or list of maps will be converted to :user, identity to :details, and first_name to :first_name. The entire chain does not have to be present if you only want to update the lowest level.

Once you have a key mapping defined you can then perform the translation using Transmog.format/2.

iex> fields = %{"account" => %{"identity" => %{"first_name" => "Billy"}}}
iex> Transmog.format(fields, @key_mapping)
{:ok, %{user: %{details: %{first_name: "Bobby"}}}}

If you are confident that your key mapping is valid then you can unwrap the results by using Transmog.format!/2.

iex> fields = %{"account" => %{"identity" => %{"first_name" => "Sally"}}}
iex> Transmog.format!(fields, @key_mapping)
%{user: %{details: %{first_name: "Bobby"}}}

Contributing

I would greatly appreciate any contributions to make this project better. Please make sure to follow the below guidelines before getting your hands dirty.

  1. Fork the repository
  2. Create your branch (git checkout -b my-branch)
  3. Commit any changes to your branch
  4. Push your changes to your remote branch
  5. Open a pull request

License

Distributed under the MIT License. See LICENSE for more information.

Acknowledgements

Copyright © 2019 Dylan Aspden

About

Easily perform a deep in-place key remapping on lists and maps in Elixir.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages