YamlVariableResolver

Helpers & Utilities · AiDotNet.Configuration

Substitutes ${NAME} references in a YAML document before it is deserialized.

For Beginners

Write apiKey: ${OPENAI_API_KEY} in your YAML file and set the OPENAI_API_KEY environment variable before running. The secret stays out of the file, and the file is safe to commit. Use ${LOG_DIR:-./runs} when you want a sensible default that a machine can override.

How It Works

A configuration file is meant to be checked in, and some of what a run needs must not be: an API key, a licence token, a machine-specific output directory, a run identifier that differs per environment. This resolver lets the file name those values instead of containing them. ${NAME} is replaced by the environment variable NAME; ${NAME:-fallback} uses fallback when the variable is unset or empty; and $${NAME} is an escape that produces the literal text ${NAME}. A reference with no value and no fallback is an error naming the variable, because the alternative - quietly substituting an empty string - turns a missing API key into an authentication failure hundreds of lines away from its cause.

Substitution happens on the document text, before parsing, so a reference can appear anywhere: in a value, in a key, or inside a longer string. That also means a substituted value carrying YAML syntax can change the shape of the document, so quote the value in the file when it might contain a colon, a hash, or a newline.

Methods

Method Summary
string Resolve(string content) Replaces every ${NAME} reference using the process environment.
string Resolve(string content, Func<string, string> lookup) Replaces every ${NAME} reference using a caller-supplied lookup.