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

Copy Markdown View Source

Utility functions for working with 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.

These functions are intended to stay equivalent to the config interfaces provided by other language APIs such as zenoh-python.

If you want an Elixir-like map-centric API, use Zenohex.ConfigMap.

Summary

Functions

Returns the default Zenoh configuration as a JSON binary.

Loads configuration from the file path specified by the ZENOH_CONFIG environment variable.

Loads configuration from the file at the given path and returns it as a JSON string.

Parses a JSON5 configuration string and returns it as a JSON string.

Returns the JSON string of the configuration value at key.

Inserts or updates a JSON5 configuration value at key, returning the updated config.

Types

t()

@type t() :: String.t()

Functions

default()

@spec default() :: t()

Returns the default Zenoh configuration as a JSON binary.

The returned configuration is valid input for Zenohex.Session.open/1.

Examples

Print the config in a readable form to check its contents.

iex> config = Zenohex.Config.default()
iex> config |> JSON.decode!() |> IO.inspect(pretty: true)

from_env()

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

Loads configuration from the file path specified by the ZENOH_CONFIG environment variable.

Examples

### Set the environment variable and load the config
$ ZENOH_CONFIG=path/to/zenoh_config.json5 iex -S mix
iex> {:ok, config} = Zenohex.Config.from_env()
iex> is_binary(config)
true

### Set the environment variable in IEx
$ unset ZENOH_CONFIG && iex -S mix
iex> System.put_env("ZENOH_CONFIG", "path/to/zenoh_config.json5")
iex> {:ok, config} = Zenohex.Config.from_env()
iex> is_binary(config)
true

from_file(path)

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

Loads configuration from the file at the given path and returns it as a JSON string.

Examples

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

from_json5(binary)

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

Parses a JSON5 configuration string and returns it as a JSON string.

This is useful when you already have configuration content in memory, such as text loaded from a file, template output, or environment-driven string construction.

Examples

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

get_json(config, key)

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

Returns the JSON string of the configuration value at key.

Examples

iex> config = Zenohex.Config.default()
iex> {:ok, value} = Zenohex.Config.get_json(config, "scouting/delay")
{:ok, "null"}

insert_json5(config, key, value)

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

Inserts or updates a JSON5 configuration value at key, returning the updated config.

value should be a valid JSON5 string (e.g., "500", "true", or ""peer""). If value is not valid JSON5 format (for example, a plain string like "peer" missing its quotes), this function automatically quotes it (encodes as a JSON string) and retries the insertion.

A list value (e.g., ["tcp/localhost:7447"]) is also accepted and encoded as a JSON array before insertion. Printable ASCII charlist values (e.g., single-quoted text like 'peer') are rejected to avoid accidentally inserting a list of integer codepoints. Other non-ASCII charlists are treated as lists and may be encoded as JSON arrays of integers.

Examples

### Case 1: Insert numeric value
iex> config = Zenohex.Config.default()
iex> {:ok, updated1} = Zenohex.Config.insert_json5(config, "scouting/delay", "100")
iex> Zenohex.Config.get_json(updated1, "scouting/delay")
{:ok, "100"}

### Case 2: Insert valid JSON5 string (manually quoted)
iex> {:ok, updated2} = Zenohex.Config.insert_json5(updated1, "mode", ""peer"")
iex> Zenohex.Config.get_json(updated2, "mode")
{:ok, ""peer""}

### Case 3: Insert plain string (automatically quoted by this function)
iex> {:ok, updated3} = Zenohex.Config.insert_json5(updated2, "mode", "client")
iex> Zenohex.Config.get_json(updated3, "mode")
{:ok, ""client""}

### Case 4: Insert list as JSON array
iex> {:ok, updated4} = Zenohex.Config.insert_json5(updated3, "connect/endpoints", ["tcp/localhost:7447"])
iex> Zenohex.Config.get_json(updated4, "connect/endpoints")
{:ok, "["tcp/localhost:7447"]"}

Migration from update_in/3

The function update_in/3 has been removed in v0.9.0. For updating Zenoh configurations, please use insert_json5/3 instead.

### Past usage with `update_in/3`:
Zenohex.Config.update_in(config, ["scouting", "delay"], fn _ -> 100 end)
### Use `insert_json5/3` with the key path joined by `/`:
Zenohex.Config.insert_json5(config, "scouting/delay", "100")