template

package
v1.11.1-0...-ed78b6e Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package template splits a property value into literal text and $( ) expressions, and walks a properties tree to reach every such value.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Call

func Call(fn, arg string) string

Call names one qualifier call, fn("arg"), as it is recorded, delivered and looked up.

func HasExpression

func HasExpression(v interface{}) bool

HasExpression reports whether a decoded properties tree contains anything to substitute, so a caller can skip the work entirely when it does not.

func IndexPath

func IndexPath(prefix string, i int) string

IndexPath extends a property path with a list index.

func JoinPath

func JoinPath(prefix, key string) string

JoinPath extends a property path with a map key.

func Map

func Map(node interface{}, path string, fn func(path, raw string) (interface{}, error)) (interface{}, error)

Map rebuilds a properties blob, replacing each string leaf with whatever fn returns for it.

A new tree is built rather than the input rewritten. Callers hold on to what they pass and may evaluate one tree more than once, so consuming the input would make the second read of it see something different from the first.

func MayContainExpr

func MayContainExpr(raw string) bool

MayContainExpr reports whether a value is worth parsing. It is false for a plain string, which is almost every property, and never false for a value that does hold an expression or an escape.

func QualifierSegment

func QualifierSegment(q string) string

QualifierSegment encodes a qualifier as a path segment.

func SegmentQualifier

func SegmentQualifier(segment string) (string, bool)

SegmentQualifier reports the qualifier a path segment encodes, if it is one.

func SplitCall

func SplitCall(call string) (string, string)

SplitCall is the inverse of Call.

func Walk

func Walk(node interface{}, path string, fn func(path, raw string) error) error

Walk visits every string leaf, carrying the path it was found at. It stops at the first error.

Non-string leaves are skipped rather than reported: an expression is always a string, and a caller wanting every scalar wants a different function.

Types

type Fragment

type Fragment struct {
	Text string
	// Expr is the expression source, without the surrounding $( ).
	Expr string
}

Fragment is one piece of a property value: either literal text or an expression to evaluate.

func (Fragment) IsExpr

func (f Fragment) IsExpr() bool

IsExpr reports whether this fragment needs evaluating.

type Parsed

type Parsed struct {
	Fragments []Fragment
}

Parsed is a property value broken into fragments.

func Parse

func Parse(raw string) (Parsed, error)

Parse splits a property value into literal and expression fragments.

`$$(` is a literal `$(`, so a value that genuinely contains the delimiter can still be written. Nesting is not supported: the expression ends at the first unbalanced `)`, counting parens so that `$(f((a)))` works.

func (Parsed) HasExpr

func (p Parsed) HasExpr() bool

HasExpr reports whether the value contains anything to evaluate. A value with none is left exactly as it was.

func (Parsed) Literal

func (p Parsed) Literal() string

Literal renders the value's text with no expressions evaluated, which is what a value with nothing to evaluate should become.

Not the raw string: `$$(` is the escape for a literal `$(`, which is what an author writes to keep Kubernetes' own `$(VAR)` expansion, so it collapses here as it does in a value that also holds an expression. A value with no delimiter comes back byte-identical.

func (Parsed) SoleExpr

func (p Parsed) SoleExpr() (string, bool)

SoleExpr returns the expression of a whole value. It is the expression fragment, which is not necessarily the first: leading whitespace is a text fragment and still leaves the value whole.

func (Parsed) Whole

func (p Parsed) Whole() bool

Whole reports whether the value is a single expression and nothing else.

This is what decides the substituted value's type. A whole value is replaced by the expression's typed result, so `replicas: '$(source["s"].count)'` yields an int even though YAML carried it as a string. An expression embedded in surrounding text can only produce a string, since that is what concatenation means. Terraform and Helm draw the same line.

Surrounding whitespace does not count as text. Without that, a single trailing space - invisible in YAML and easy to leave behind when editing - would flip the substituted type from int to string, and the resulting mismatch would surface against the parameter, far from the space that caused it. Adding a visible character is a deliberate act; adding a space is not.

type Reference

type Reference struct {
	// Root is the top-level name the read starts from.
	Root string
	// Path is the rest, one segment per field, index or qualifier.
	Path []string
	// Defaulted records that the read is guarded against absence, by has(),
	// `"k" in m`, or a ternary arm, so it survives the value being missing.
	Defaulted bool
}

Reference is one path an expression reads.

func (Reference) String

func (r Reference) String() string

String renders a reference the way an error message names it.

Segments are rendered so the result is a valid expression, because the errors that use it tell the author what to write, such as a has() guard around the read. Joined with dots, an index would come out as `outputs.0.name`, which does not parse.

A segment is rendered from its text alone, so a key that is all digits, such as cfg["0"], renders as an index, cfg[0]. Lookups decide by the value's kind and are unaffected; only the rendered name is ambiguous.

Jump to

Keyboard shortcuts

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