Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Defmap is a utility which allows you to embed a map into a module for faster/easier lookups.

Notifications You must be signed in to change notification settings

minhajuddin/defmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

10 Commits

Repository files navigation

defmap

Travis Hex.pm Hex.pm

Defmap is a utility which allows you to embed a map into a module for faster/easier lookups. A lot of times you may want to have lookup tables in your app. e.g. when you need to map error codes to messages. Using Defmap, you directly embed the lookup map/table into your module, this improves the performance and decreases the memory used.

Example Usage

Simple lookup which adds a HttpStatusMessages.get method

defmodule HttpStatusMessages do
 use Defmap, map: %{
 400 => "Bad Request",
 401 => "Unauthorized",
 403 => "Forbidden",
 }
end
IO.inspect HttpStatusMessages.get(401) # => {:ok, "Unauthorized"}
IO.inspect HttpStatusMessages.get(3) # => :error

Lookup with custom function name

defmodule HttpStatusMessages do
 use Defmap, func_name: :get_message, map: %{
 400 => "Bad Request",
 401 => "Unauthorized",
 403 => "Forbidden",
 }
end
IO.inspect HttpStatusMessages.get_message(401) # => {:ok, "Unauthorized"}
IO.inspect HttpStatusMessages.get_message(3) # => :error

Multiple lookups

defmodule HttpStatusMessages do
 use Defmap, func_name: :get_message, map: %{
 400 => "Bad Request",
 401 => "Unauthorized",
 403 => "Forbidden",
 }
 use Defmap, func_name: :get_detailed_info, map: %{
 400 => "Bad Request, happens when your input is invalid",
 401 => "Unauthorized, happens when you don't have enough privileges",
 }
end
IO.inspect HttpStatusMessages.get_message(401) # => {:ok, "Unauthorized"}
IO.inspect HttpStatusMessages.get_message(3) # => :error
IO.inspect HttpStatusMessages.get_detailed_info(401) # => {:ok, "Bad Request, happens when your input is invalid"}
IO.inspect HttpStatusMessages.get_detailed_info(3) # => :error

Use a CSV file to store the map and then embed it at compile time

Contents of ./http_statuses.csv

417, Expectation Failed
500, Internal Server Error
205, Reset Content
...
defmodule HttpStatuses do
 use Defmap, map: (File.stream!(Path.expand(Path.join(__DIR__, "./http_statuses.csv")))
 |> CSV.decode
 |> Enum.reject(& length(&1) != 2)
 |> Enum.map(fn [k, v] -> {String.to_integer(k), String.strip(v)} end)
 |> Enum.into(%{})),
 func_name: :get_status
end
IO.inspect HttpStatusMessages.get_status(401) # => {:ok, "Unauthorized"}
IO.inspect HttpStatusMessages.get_status(3) # => :error

Installation

The package can be installed as:

  1. Add defmap to your list of dependencies in mix.exs:
def deps do
 [{:defmap, "~> 0.1.0"}]
end
  1. Ensure defmap is started before your application:
def application do
 [applications: [:defmap]]
end

About

Defmap is a utility which allows you to embed a map into a module for faster/easier lookups.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

AltStyle によって変換されたページ (->オリジナル) /