Skip to content

mathieuprog/attrs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Attrs

Unifying atom and string key handling for user data (attrs maps) given to Ecto's cast function.

Problem

When working with Elixir's Phoenix Controllers or Absinthe, external data maps passed to cast/4 must consist entirely of either atom keys or string keys, but cannot contain a mix of both types. This leads to tedious checks and potentially error-prone adjustments for data retrieval and insertion, since the key type must be accounted for each time:

user_id = attrs[:user_id] || attrs["user_id"]

Solution

user_id = Attrs.get(attrs, :user_id)

Attrs enables handling of maps irrespective of whether they contain atom or string keys. It eliminates the distinction between the two key types, simplifying code when working with external data maps passed to cast/4.

Why "Attrs"? In Phoenix Contexts, user parameters and external data are often labeled as attrs, hence the name of this package.

Attrs module

Attrs.put(attrs, key, value)

# If the map contains string keys, ensure the new entry is added with a string key
Attrs.put(%{"foo" => 1}, :bar, 2) == %{"foo" => 1, "bar" => 2}

# If the map contains atom keys, ensure the new entry is added with an atom key
Attrs.put(%{foo: 1}, :bar, 2) == %{foo: 1, bar: 2}

Attrs.get(attrs, key, default)

# Regardless of whether the key is a string or an atom, the value can be retrieved
Attrs.get(%{foo: 1}, :foo) == 1
Attrs.get(%{"foo" => 1}, :foo) == 1

# A default value may be provided in case the key is not found in the map
Attrs.get(%{foo: 1}, :bar) == nil
Attrs.get(%{foo: 1}, :bar, 2) == 2

Attrs.has?(attrs, key)

Attrs.has?(%{foo: 1}, :foo) == true
Attrs.has?(%{"foo" => 1}, :foo) == true

Attrs.merge(attrs1, attrs2)

# If either of the input maps has string keys, the merged map will also use string keys
Attrs.merge(%{"foo" => 1}, %{bar: 2}) == %{"foo" => 1, "bar" => 2}
Attrs.merge(%{"foo" => 1}, %{"bar" => 2}) == %{"foo" => 1, "bar" => 2}

# If both input maps use atom keys, the merged map will maintain the use of atom keys
Attrs.merge(%{foo: 1}, %{bar: 2}) == %{foo: 1, bar: 2}

Attrs.normalize(attrs)

# When normalizing key types, if the input map contains a mix of atom and string keys, all keys are converted to strings
Attrs.normalize(%{"foo" => 1, bar: 2}) == %{"foo" => 1, "bar" => 2}

# If all keys are already atoms or strings, they are left unchanged
Attrs.normalize(%{foo: 1, bar: 2}) == %{foo: 1, bar: 2}
Attrs.normalize(%{"foo" => 1, "bar" => 2}) == %{"foo" => 1, "bar" => 2}

Installation

Add attrs for Elixir as a dependency in your mix.exs file:

def deps do
  [
    {:attrs, "~> 0.4.0"}
  ]
end

HexDocs

HexDocs documentation can be found at https://hexdocs.pm/attrs.

About

Unifying atom and string key handling for user data (attrs maps) given to Ecto's cast function

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages