Apero.Conf (Apero v1.0.0)

Copy Markdown View Source

Configuration file management — unified interface for JSON, YAML and TOML.

Provides operations for loading, validating, writing and merging config files regardless of their format. The format is auto-detected from the file extension and can be explicitly specified.

For .env files and environment variables, use Apero.Env.

Summary

Functions

Detects the config format from a file extension.

Serializes a map to a config string.

Gets a nested value from a config map using dot-separated key path.

Loads a config file. Format is auto-detected from extension.

Merges a list of config maps. Later entries override earlier ones.

Parses a config string in the given format.

Prints a formatted summary of a config map to the terminal.

Sets a nested value in a config map using dot-separated key path.

Validates a config map against a schema map (shallow key-type check).

Writes a map to a config file.

Types

format()

@type format() :: :json | :yaml | :toml

Functions

detect_format(path)

@spec detect_format(Path.t()) :: format()

Detects the config format from a file extension.

encode(data, atom)

@spec encode(map(), format()) :: {:ok, String.t()} | {:error, term()}

Serializes a map to a config string.

get(config, key_path)

@spec get(map(), String.t()) :: term()

Gets a nested value from a config map using dot-separated key path.

Examples

iex> Conf.get(%{retrieval: %{vector_weight: 0.5}}, "retrieval.vector_weight")
0.5

iex> Conf.get(%{foo: 1}, "bar")
nil

load(path, opts \\ [])

@spec load(
  Path.t(),
  keyword()
) :: {:ok, map()} | {:error, term()}

Loads a config file. Format is auto-detected from extension.

merge(configs)

@spec merge([map()]) :: map()

Merges a list of config maps. Later entries override earlier ones.

parse(content, atom)

@spec parse(String.t(), format()) :: {:ok, map()} | {:error, term()}

Parses a config string in the given format.

set(config, key_path, value)

@spec set(map(), String.t(), term()) :: map()

Sets a nested value in a config map using dot-separated key path.

Returns an updated config map (immutable — original is not modified).

Examples

iex> Conf.set(%{retrieval: %{vector_weight: 0.5}}, "retrieval.vector_weight", 0.8)
%{retrieval: %{vector_weight: 0.8}}

validate(config, schema)

@spec validate(map(), map()) :: :ok | {:error, [String.t()]}

Validates a config map against a schema map (shallow key-type check).

write(path, data, opts \\ [])

@spec write(Path.t(), map(), keyword()) :: :ok | {:error, term()}

Writes a map to a config file.