interpolate

package
v0.14.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package interpolate provides string interpolation from environment variables as per the Compose specification. See Interpolation specification for the authoritative source of truth our implementation should adhere to.

This interpolation uses a Bash-like syntax, both in so-called “unbraced” and “braced” syntax:

$FOO
${FOO}

But unlike Bash, interpolation can be nested:

${FOO:-${BAR}}

The following subsitutions are supported, described below.

Default

The following substitution

${VARIABLE:-default}

evaluates to “default” if VARIABLE is unset or empty. In contrast,

${VARIABLE-default}

evaluates to default only if VARIABLE is unset, but not if it is empty.

Error

The following substitution

${VARIABLE:?err}

exits with an error message containing err if VARIABLE is unset or empty. In constrast,

${VARIABLE?err}

exits with an error message containing err only if VARIABLE is unset, but not if it is empty.

Replacement

In addition to the Composer spec-defined default and error interpolations, this package additionally supports the Docker Compose Interpolation of alternative values:

${VARIABLE:+replacement}

replaces with replacement if VARIABLE is set and non-empty, otherwise empty. In contrast,

${VARIABLE+replacement}

replaces with replacement if VARIABLE is set, otherwise empty, but not if it is empty.

Implementation Note

While the “Compose specification” Github organization provides a Compose Spec reference interpolation implementation, this module provides its own implementation. In particular, this interpolation is implemented as a dedicated parser instead of using regular expressions. A dedicated parser implementation is probably much more straightforward to carry out, understand, and maintain than regular expressions that really don't work well in the face of recursive interpolation where they need dodgy parsing helpers anyway.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Variables

func Variables(data map[string]any, vars map[string]string) (map[string]any, error)

Variables interpolates all string values in the passed (recursive) map with values from the pass variables as necessary. It returns a new (recursive) map with the interpolated results.

Types

type Path

type Path string

Path represents the path to a scalar.

func (Path) Append

func (p Path) Append(name string) Path

Append the name of a mapping key or a scalar to the path, returning the new Path.

func (Path) AppendIndex

func (p Path) AppendIndex(idx int) Path

AppendIndex appends the index of an element to the path, returning the new Path.

type PlainText

type PlainText string

PlainText is just what it says on the tin: plain text, no substitutes. In these days, we might call it organic, authentic, whatever.

func (PlainText) Text

func (pt PlainText) Text(map[string]string) (string, error)

Text returns plain text without any substitutions

type Segment

type Segment interface {
	Text(vars map[string]string) (string, error)
}

Segment produces plain text upon request with all variables replaced by their values or alternate substitution values.

type Segments

type Segments []Segment

Segments is a slice of Segment-implementing objects that produce plain text upon request while doing variable substitutions.

func (Segments) Text

func (segs Segments) Text(vars map[string]string) (string, error)

Text returns the plain text from the slice of segments, substituting variable values as necessary.

type Substitution

type Substitution struct {
	VariableName string   // Name of the variable to substitute
	Operation    string   // either "" for a simple substitution, or one of "-", "?-", etc.
	AltValue     Segments // if non-zero, the alternative value to substitute the variable name with
}

Substitution represents a particular variable substitution.

func (Substitution) Text

func (subst Substitution) Text(vars map[string]string) (string, error)

Text returns the plain text of this segment, substituting variable values recursively as necessary.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL