Documentation
¶
Overview ¶
Package template splits a property value into literal text and $( ) expressions, and walks a properties tree to reach every such value.
Index ¶
- func Call(fn, arg string) string
- func HasExpression(v interface{}) bool
- func IndexPath(prefix string, i int) string
- func JoinPath(prefix, key string) string
- func Map(node interface{}, path string, fn func(path, raw string) (interface{}, error)) (interface{}, error)
- func MayContainExpr(raw string) bool
- func QualifierSegment(q string) string
- func SegmentQualifier(segment string) (string, bool)
- func SplitCall(call string) (string, string)
- func Walk(node interface{}, path string, fn func(path, raw string) error) error
- type Fragment
- type Parsed
- type Reference
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 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 ¶
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 ¶
QualifierSegment encodes a qualifier as a path segment.
func SegmentQualifier ¶
SegmentQualifier reports the qualifier a path segment encodes, if it is one.
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.
type Parsed ¶
type Parsed struct {
Fragments []Fragment
}
Parsed is a property value broken into fragments.
func Parse ¶
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 ¶
HasExpr reports whether the value contains anything to evaluate. A value with none is left exactly as it was.
func (Parsed) Literal ¶
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 ¶
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 ¶
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 ¶
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.