PathMap

Hex.pmHexdocs

Deterministic helpers for traversing and mutating nested maps using explicit paths (lists of keys). Every call validates inputs, refuses to guess, and returns tagged results instead of raising.

Purpose

PathMap focuses on predictability and safety for nested map operations without introducing a DSL or macros. If you want small, composable functions that tell you exactly why traversal failed, this library is for you.

Quick example

map = %{"config" => %{"port" => 4000}}

# Strict read
{:ok, 4000} = PathMap.fetch(map, ["config", "port"])

# Strict write (fails because the path is missing)
{:error, {:missing, ["config", "db"]}} = PathMap.put(map, ["config", "db", "port"], 5432)

# Auto-vivifying write
{:ok, map} = PathMap.put_auto(map, ["config", "db", "port"], 5432)
5432 = map["config"]["db"]["port"]

# Update with default and auto-vivify
{:ok, map} = PathMap.update_auto(%{}, [:a, :b], 0, &(&1 + 1))
1 = get_in(map, [:a, :b])

Core behaviors

Error shapes

When to use PathMap

When not to use PathMap

Comparison

vs Pathex

vs lens

API highlights

See doctests in lib/path_map.ex and test/path_map_test.exs for detailed examples and edge cases.

Documentation

Full API docs live at hexdocs.pm/path_map.

Requirements

Changelog

See CHANGELOG.md.

License

Released under the MIT License. See LICENSE for details.

Installation

Add to your mix.exs dependencies:

def deps do
  [
    {:path_map, "~> 0.1.0"}
  ]
end