View Source ExAequoKwds (ExAequoKwds v0.1.0)

Tools to handle access and constraints to Keyword Lists

check_kwds

All required keys are present

iex(1)> check_kwds([a: 1, b: 2], [:b, :a])
{:ok, %{a: 1, b: 2}}

Defaults can be used

iex(2)> check_kwds([a: 1], [:a, b: 2])
{:ok, %{a: 1, b: 2}}

But might not

iex(3)> check_kwds([a: 1, b: 1], [:a, b: 2])
{:ok, %{a: 1, b: 1}}

We must not have spurious keys

iex(4)> check_kwds([a: 1, b: 1], [:a])
{:error, "spurious [b: 1]"}

Nor missing ones

iex(5)> check_kwds([b: 1], [:a, :b])
{:error, "missing key a"}

But we can ignore_errors

iex(6)> check_kwds([a: 1, b: 1], [:a], ignore_errors: true)
{:ok, %{a: 1}}

iex(7)> check_kwds([b: 1], [:a, :b], ignore_errors: true)
{:ok, %{a: nil, b: 1}}

check_kwds!

All fine or ignoring errors

iex(8)> check_kwds!([a: 1, b: 2], [:b, :a])
%{a: 1, b: 2}

iex(9)> check_kwds!([a: 1], [:a, b: 2])
%{a: 1, b: 2}

iex(10)> check_kwds!([a: 1, b: 1], [:a], ignore_errors: true)
%{a: 1}

iex(11)> check_kwds!([b: 1], [:a, :b], ignore_errors: true)
%{a: nil, b: 1}

Otherwise ArgumentError will be raised

iex(12)> assert_raise(ArgumentError, fn -> check_kwds!([a: 1, b: 1], [:a]) end)

Nor missing ones

iex(13)> assert_raise(ArgumentError, fn -> check_kwds!([b: 1], [:a, :b]) end)

Summary

Types

@type result_t() :: {:ok, map()} | {:error, String.t()}
@type single_spec_t() :: atom() | {atom(), any()}
@type spec_t() :: [single_spec_t()]

Functions

Link to this function

check_kwds(kwds, keys, options \\ [])

View Source
@spec check_kwds(Keyword.t(), spec_t(), Keyword.t()) :: result_t()
Link to this function

check_kwds!(kwds, keys, options \\ [])

View Source
@spec check_kwds!(Keyword.t(), spec_t(), Keyword.t()) :: map()