pkg

ctxcode/valk-config

Dotenv parser and layered typed configuration loader (JSON, TOML, YAML, env) for Valk

github.com/ctxcode/valk-config ↗
0.2.0 latest Valk ≥ 0.9.1

Install

vman install github.com/ctxcode/valk-config

Adds the package to your valk.json. To allow only some versions, add them: vman install github.com/ctxcode/valk-config 0.2.x

Documentation

Namespaces: main


main

Functions for 'main'

// Reads a `.env` file, parses it, and sets variables in the process environment.
+ fn load_dotenv(path: String (".env"), override_existing: bool (false)) Map[String] !Error
// Loads a `.env` file into the process environment if it exists.
+ fn load_dotenv_if_exists(path: String (".env"), override_existing: bool (false)) Map[String] !Error
// Loads configuration directly from process environment variables into `T`.
+ fn load_env[T](prefix: String (""), separator: String ("__")) T !Error
// Loads a single configuration file (`.json`, `.toml`, `.yaml`, `.yml`, or `.env`) into `T`.
+ fn load_file[T](path: String) T !Error
// Parses `.env` formatted text into a map of key-value strings.
+ fn parse_dotenv(text: String) Map[String] !Error
// Formats `data` as JSON with all `$tag(secret)` fields replaced by `"[REDACTED]"`.
+ fn redact_debug(data: $T, pretty: bool (false)) String
// Converts `data` into a `json.Value` with all `$tag(secret)` fields replaced by `"[REDACTED]"`.
+ fn redact_value(data: $T) Value

load_dotenv

Reads a .env file, parses it, and sets variables in the process environment.

Existing process environment variables are preserved unless override_existing is true.
Returns the map of all key-value pairs parsed from the file.

load_dotenv_if_exists

Loads a .env file into the process environment if it exists.

Returns an empty map instead of throwing when path does not exist.

load_env

Loads configuration directly from process environment variables into T.

load_file

Loads a single configuration file (.json, .toml, .yaml, .yml, or .env) into T.

parse_dotenv

Parses .env formatted text into a map of key-value strings.

Supports # comments, blank lines, optional export KEY=VALUE prefixes, unquoted values
(with trailing whitespace and inline # comments after whitespace stripped), single-quoted
literal strings ('...'), double-quoted strings ("...") with escape sequences
(\n, \r, \t, \", \\, \$), multiline quoted strings, and ${VAR} /
${VAR:-default} / ${VAR-default} variable interpolation against earlier entries in the
same input and the current process environment.

redact_debug

Formats data as JSON with all $tag(secret) fields replaced by "[REDACTED]".

Safe to log or print when inspecting loaded configuration.

redact_value

Converts data into a json.Value with all $tag(secret) fields replaced by "[REDACTED]".

Classes for 'main'

// Thrown when loading, parsing, or decoding configuration fails.
+ union Error {
    io(String, String, String)
    syntax(String, String, uint)
    format(String, String)
    missing(String, String, String)
    type(String, String, String)

    + get line: uint
    + get message: String
    + get path: String
    + get source: String
    + fn to_string() String
}

Error

Thrown when loading, parsing, or decoding configuration fails.

  • io(message, path, source): a required file could not be read, or updating an environment
    variable failed.
  • syntax(message, source, line): a configuration file or .env input could not be parsed.
  • format(message, source): a file extension is not recognized by Loader.add_file.
  • missing(message, path, source): a required configuration field is absent or empty.
  • type(message, path, source): a configuration value cannot be converted to the target
    field's type.

path, source, and line default to "" / 0 on variants where they do not apply.

// Layered configuration builder that merges JSON, TOML, YAML, `.env`, environment variables, and explicit overrides in priority order (later sources override earlier ones).
+ class Loader {
    // Parses `.env` text and merges its entries as the next configuration layer.
    + fn add_dotenv(text: String, prefix: String (""), separator: String ("__"), source: String (".env")) Loader !Error
    // Reads and merges a `.env` file as the next configuration layer.
    + fn add_dotenv_file(path: String (".env"), required: bool (false), prefix: String (""), separator: String ("__")) Loader !Error
    // Reads process environment variables as the next configuration layer.
    + fn add_env(prefix: String (""), separator: String ("__")) Loader
    // Adds a map of environment-style variables as the next configuration layer.
    + fn add_env_map(values: Map[String], prefix: String (""), separator: String ("__"), source: String ("env")) Loader
    // Reads and merges a configuration file, dispatching by extension.
    + fn add_file(path: String, required: bool (true)) Loader !Error
    // Parses JSON text and deep-merges it as the next configuration layer.
    + fn add_json(text: String, source: String ("json")) Loader !Error
    // Reads and merges a JSON configuration file.
    + fn add_json_file(path: String, required: bool (true)) Loader !Error
    // Merges a map of string overrides (for example from CLI flags) as the next layer.
    + fn add_map(values: Map[String], separator: String (".")) Loader
    // Parses TOML text and deep-merges it as the next configuration layer.
    + fn add_toml(text: String, source: String ("toml")) Loader !Error
    // Reads and merges a TOML configuration file.
    + fn add_toml_file(path: String, required: bool (true)) Loader !Error
    // Parses YAML text and deep-merges it as the next configuration layer.
    + fn add_yaml(text: String, source: String ("yaml")) Loader !Error
    // Reads and merges a YAML configuration file.
    + fn add_yaml_file(path: String, required: bool (true)) Loader !Error
    // Decodes the merged configuration into a class or struct `T`.
    + fn load[T]() T !Error
    // Creates an empty configuration loader.
    + static fn new() Loader
    // Sets a single string override at dotted path `key` (such as `"db.port"`).
    + fn set(key: String, value: String) Loader
    // Sets a typed `json.Value` override at dotted path `key`.
    + fn set_value(key: String, value: Value) Loader
    // Returns the source label that last wrote `path`, or `null` when absent.
    + fn source_of(path: String) ?String
    // Returns the merged configuration tree as a `json.Value`.
    + fn value() Value
}

Loader

Layered configuration builder that merges JSON, TOML, YAML, .env, environment variables,
and explicit overrides in priority order (later sources override earlier ones).

Nested objects/tables are deep-merged across layers so overriding a single nested key
(such as db.port via an environment variable) preserves sibling keys from earlier files.

let loader = config.Loader.new()
loader.add_file("config.toml") !>
loader.add_dotenv_file(".env") !>
loader.add_env("APP_")
let cfg = loader.load[AppConfig]() !>

add_dotenv

Parses .env text and merges its entries as the next configuration layer.

Every parsed key is also stored for $tag(env: "KEY") lookups. Keys matching prefix
are split on separator (default "__") and lowercased into nested config paths.

add_dotenv_file

Reads and merges a .env file as the next configuration layer.

Defaults to optional (required: false), so a missing .env file is silently skipped.

add_env

Reads process environment variables as the next configuration layer.

Variables matching prefix (for example "APP_") have the prefix stripped, are split by
separator (default "__") into nested paths, and lowercased — so APP_DB__PORT=5432
sets db.port. All environment variables are also captured for $tag(env: "VAR") fields.

add_env_map

Adds a map of environment-style variables as the next configuration layer.

Behaves like add_env without reading the global process environment.

add_file

Reads and merges a configuration file, dispatching by extension.

Supports .json, .toml, .yaml, .yml, .env, and .env.* files. Throws .format
for unrecognized extensions.

add_json

Parses JSON text and deep-merges it as the next configuration layer.

add_json_file

Reads and merges a JSON configuration file.

When required is false and the file does not exist, this is a no-op.

add_map

Merges a map of string overrides (for example from CLI flags) as the next layer.

Keys are split by separator (default ".") into nested paths unless separator is empty.

add_toml

Parses TOML text and deep-merges it as the next configuration layer.

add_toml_file

Reads and merges a TOML configuration file.

When required is false and the file does not exist, this is a no-op.

add_yaml

Parses YAML text and deep-merges it as the next configuration layer.

An empty or null YAML document is treated as an empty layer.

add_yaml_file

Reads and merges a YAML configuration file.

When required is false and the file does not exist, this is a no-op.

load

Decodes the merged configuration into a class or struct T.

new

Creates an empty configuration loader.

set

Sets a single string override at dotted path key (such as "db.port").

set_value

Sets a typed json.Value override at dotted path key.

source_of

Returns the source label that last wrote path, or null when absent.

value

Returns the merged configuration tree as a json.Value.