CIHex.pmHex.pmHex.pm

ExAequoKwds

Tools to handle access and constraints to Keyword Lists

Documentation

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)

Author

Copyright © 2024 Robert Dober robert.dober@gmail.com

LICENSE

GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007 or later. Please refer to LICENSE for details.