Common usage

Common usage of Pathex boils down to getters/setters for structures with defined set of fields

GenServer state access

For example, you have a wallet of user and you want to have a quick access to amount of currencies user has,

State looks like:

state = %{
  user_data: %{
    name: "...",
    private: %{
      wallet: %{
        "USD" => 123,
        "EUR" => 456

And getter for currency amount will look like:

def currency(cur) do
  path :user_data / :private / :wallet / cur, :map

With this you can set, get and update value in state and it's better than put_in, update_in because it's

  • Faster

  • You don't have to ensure that given field exsists when putting value


    case state do
      %{user_data: %{private: %{wallet: %{^cur => _}}}} ->
        put_in ..., amount
      %{user_data: %{private: %{wallet: %{}}}} ->
        put_in ..., %{cur => amount}


    Pathex.force_set(state, currency(cur), amount)

Polymorphic input

Sometimes you don't know if incoming structure will be Map or Keyword Pathex solves this problem by providing polymorphic input matching

# For Keyword
{:ok, 1} =
  [x: 1]
  |> Pathex.view(path :x)

# For Map
{:ok, 1} =
  %{x: 1}
  |> Pathex.view(path :x)