Zenohex.ConfigMap (Zenohex v0.9.1-beta.1)

Copy Markdown View Source

Elixir-like map-centric utility functions for Zenoh session configurations.

This module provides helpers to obtain default configuration, load from files or environment variables, parse JSON5 strings, and retrieve or update individual config keys as Elixir map values.

This module automatically normalizes maps internally: atom keys become strings, charlist values are rejected, and only JSON-compatible types are accepted.

This module is intended for configuring Zenoh naturally with Elixir maps. For the raw JSON / JSON5 API that tracks other language APIs (for example, zenoh-python), use Zenohex.Config.

Summary

Functions

Returns the default Zenoh configuration as an Elixir map.

Loads configuration from ZENOH_CONFIG and returns it as an Elixir map.

Loads configuration from a file and returns it as an Elixir map.

Parses a JSON5 configuration string and returns it as an Elixir map.

Returns the configuration value at key from an Elixir map config.

Inserts or updates a value at key using Elixir data types.

Merges Elixir map data into the configuration.

Types

json_scalar()

@type json_scalar() :: nil | boolean() | number() | String.t()

json_value()

@type json_value() ::
  json_scalar() | [json_value()] | %{optional(String.t()) => json_value()}

t()

@type t() :: %{optional(String.t()) => json_value()}

Functions

default()

@spec default() :: t()

Returns the default Zenoh configuration as an Elixir map.

Examples

iex> config = Zenohex.ConfigMap.default()
iex> is_map(config)
true

from_env()

@spec from_env() :: {:ok, t()} | {:error, reason :: term()}

Loads configuration from ZENOH_CONFIG and returns it as an Elixir map.

Examples

iex> System.put_env("ZENOH_CONFIG", "path/to/zenoh_config.json5")
iex> {:ok, config} = Zenohex.ConfigMap.from_env()
iex> is_map(config)
true

from_file(path)

@spec from_file(String.t()) :: {:ok, t()} | {:error, reason :: term()}

Loads configuration from a file and returns it as an Elixir map.

Examples

iex> {:ok, config} = Zenohex.ConfigMap.from_file("path/to/zenoh_config.json5")
iex> is_map(config)
true

from_json5(binary)

@spec from_json5(Zenohex.Config.t()) :: {:ok, t()} | {:error, reason :: term()}

Parses a JSON5 configuration string and returns it as an Elixir map.

Examples

iex> json5 = File.read!("path/to/zenoh_config.json5")
iex> {:ok, config} = Zenohex.ConfigMap.from_json5(json5)
iex> is_map(config)
true

get(config, key)

@spec get(map(), String.t()) :: {:ok, json_value()} | {:error, reason :: term()}

Returns the configuration value at key from an Elixir map config.

Examples

iex> {:ok, config} = Zenohex.ConfigMap.merge(%{}, %{mode: "peer", scouting: %{delay: 100}, connect: %{endpoints: ["tcp/localhost:7447"]}})
iex> {:ok, "peer"} = Zenohex.ConfigMap.get(config, "mode")
iex> {:ok, delay} = Zenohex.ConfigMap.get(config, "scouting/delay")
iex> delay
100
iex> {:ok, endpoints} = Zenohex.ConfigMap.get(config, "connect/endpoints")
iex> endpoints
["tcp/localhost:7447"]

insert(config, key, value)

@spec insert(map(), String.t(), json_value() | map()) ::
  {:ok, t()} | {:error, reason :: term()}

Inserts or updates a value at key using Elixir data types.

The first argument must be an Elixir map config.

Examples

iex> {:ok, config} = Zenohex.ConfigMap.merge(%{}, %{mode: "client", scouting: %{delay: 500}, connect: %{endpoints: []}})
iex> {:ok, updated1} = Zenohex.ConfigMap.insert(config, "mode", "peer")
iex> {:ok, updated2} = Zenohex.ConfigMap.insert(updated1, "scouting/delay", 100)
iex> {:ok, updated3} = Zenohex.ConfigMap.insert(updated2, "connect/endpoints", ["tcp/localhost:7447"])
iex> {:ok, "peer"} = Zenohex.ConfigMap.get(updated3, "mode")
iex> {:ok, 100} = Zenohex.ConfigMap.get(updated3, "scouting/delay")
iex> {:ok, ["tcp/localhost:7447"]} = Zenohex.ConfigMap.get(updated3, "connect/endpoints")

merge(config, map)

@spec merge(map(), map()) :: {:ok, t()} | {:error, reason :: term()}

Merges Elixir map data into the configuration.

Keys in map overwrite corresponding keys in config. Nested maps are merged recursively, so unrelated sub-keys in config are preserved.

The map argument is normalized before merging.

Examples

iex> base = %{"mode" => "client", "scouting" => %{"delay" => 500, "other" => "value"}}
iex> {:ok, updated} = Zenohex.ConfigMap.merge(base, %{mode: "peer", scouting: %{delay: 100}})
iex> {:ok, "peer"} = Zenohex.ConfigMap.get(updated, "mode")
iex> {:ok, 100} = Zenohex.ConfigMap.get(updated, "scouting/delay")
iex> {:ok, "value"} = Zenohex.ConfigMap.get(updated, "scouting/other")