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

valk-config

.env parsing/loading and layered typed configuration (JSON, TOML, YAML, .env, environment variables, and overrides) into $tag-annotated classes and structs for Valk.

Requires Valk 0.9.1 or newer.

Install

vman install github.com/ctxcode/valk-config

Example

use config

class DatabaseConfig {
    host: String ("localhost")
    port: uint (5432)
    password: String ("") $tag(env: "DB_PASSWORD", secret, required)
}

class AppConfig {
    name: String ("service")
    debug: bool (false)
    features: Array[String] (.{})
    db: DatabaseConfig
    internal: String ("local") $tag(config: "-")
}

fn main() {
    let loader = config.Loader.new()
    loader.add_file("config.toml") ! panic(E.message)
    loader.add_file("config.local.yaml", false) ! panic(E.message)
    loader.add_dotenv_file(".env") ! panic(E.message)
    loader.add_env("APP_")
    loader.set("db.port", "6432")

    let cfg = loader.load[AppConfig]() ! panic(E.message)
    println(config.redact_debug(cfg, true))
}

Or load a single file (.json, .toml, .yaml, .yml, or .env) directly:

let cfg = config.load_file[AppConfig]("config.toml") ! panic(E.message)

`.env` parsing & loading

// Parse .env text without modifying the process environment
let vars = config.parse_dotenv(text) ! panic(E.message)

// Load .env into the process environment (does not overwrite existing vars by default)
config.load_dotenv(".env") ! panic(E.message)

// Silently returns an empty map if .env does not exist
config.load_dotenv_if_exists(".env", false) ! panic(E.message)

Supported .env syntax:

  • Comments (# ...) and blank lines
  • Optional export KEY=VALUE prefix
  • Unquoted values (trimmed, inline # comment after whitespace stripped)
  • Single-quoted literal strings ('...', multiline supported, no escapes or interpolation)
  • Double-quoted strings ("...", multiline supported, \n, \r, \t, \", \\, \$)
  • Variable interpolation: ${VAR}, ${VAR:-default}, ${VAR-default}, referencing earlier entries in the same file first and falling back to the process environment

Layered configuration

Loader merges layers in the exact order they are added (later sources win). Nested objects/tables are deep-merged, so overriding db.port via APP_DB__PORT=6432 preserves db.host from config.toml.

  • loader.add_json(text) / loader.add_json_file(path, required: true)
  • loader.add_toml(text) / loader.add_toml_file(path, required: true)
  • loader.add_yaml(text) / loader.add_yaml_file(path, required: true)
  • loader.add_dotenv(text) / loader.add_dotenv_file(path: ".env", required: false)
  • loader.add_file(path, required: true) — dispatches .json, .toml, .yaml, .yml, .env, and .env.*
  • loader.add_env(prefix: "", separator: "__") — maps prefixed env vars like APP_DB__PORT to db.port
  • loader.add_map(values, separator: ".") / loader.set(key, value) / loader.set_value(key, json_val)
  • loader.source_of(path) — reports which source supplied a key (e.g. "config.toml", "env:APP_DB__PORT", "override")

Field tags

Both $tag(...) and backtick shorthand ( `...` ) are supported:

  • $tag(config: "name") — rename the configuration key (falls back to json, toml, yaml, then prop.name)
  • $tag(config: "-") — skip the field during decoding (uses its default value) and redact_debug
  • $tag(env: "VAR_NAME") — bind an explicit environment variable (or .env entry) to the field
  • $tag(secret) — replace the value with "[REDACTED]" in redact_debug / redact_value, and redact values in type error messages
  • $tag(required) — require the field to be present and non-empty even if it has a default value

Type coercion

Strings coming from environment variables, .env, add_map, and set coerce automatically into target field types:

  • bool: true/1/yes/on vs false/0/no/off (case-insensitive)
  • Signed & unsigned integers (int, uint, i8..i64, u8..u64) with range validation
  • Floating-point numbers (f32, f64) with f32 overflow checking
  • Array[T], HashSet[T], Deque[T]: comma-separated strings (e.g. APP_PORTS=80,443,8080) or native arrays from JSON/TOML/YAML
  • Nested structs/classes, Map[V], FlatMap[String, V], nullable ?T, integer enums, json.Value, and field defaults

Errors

config.Error is a tagged union with variants .io(message, path, source), .syntax(message, source, line), .format(message, source), .missing(message, path, source), and .type(message, path, source), with E.message, E.path, E.source, E.line, and E.to_string() getters:

let cfg = loader.load[AppConfig]() ! {
    match E {
        .missing(msg, path, _) => println("missing " + path + ": " + msg)
        .type(msg, path, src) => println("invalid " + path + " (" + src + "): " + msg)
        default => println(E.message)
    }
    return
}

Development

make test runs the test suite, make lint checks the sources with --deny-warnings, make example runs the example, and make docs regenerates the API docs.