template

package
v1.229.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package template provides utilities for Go template AST inspection and analysis. This package enables extraction of field references from templates, which is useful for dependency resolution (e.g., locals referencing other locals).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractAllFieldRefsByPrefix

func ExtractAllFieldRefsByPrefix(templateStr string, prefix string) ([]string, error)

ExtractAllFieldRefsByPrefix extracts all field references that start with a specific prefix, returning the full remaining path after the prefix. For example, ExtractAllFieldRefsByPrefix(tmpl, "locals") for .locals.config.nested returns ["config.nested"].

func ExtractFieldRefsByPrefix

func ExtractFieldRefsByPrefix(templateStr string, prefix string) ([]string, error)

ExtractFieldRefsByPrefix extracts field references that start with a specific prefix. For example, ExtractFieldRefsByPrefix(tmpl, "locals") returns all .locals.X references. Returns the second-level identifiers (e.g., "foo" for .locals.foo).

func HasDynamicScope

func HasDynamicScope(templateStr string) (bool, error)

HasDynamicScope reports whether a template contains a `range` or `with` action, either of which rebinds "." to something other than the template's root data for the body it encloses. Field references found inside such a body (e.g. `.name` inside `{{ range .vars.list }}{{ .name }}{{ end }}`) are NOT relative to the template's root, so a caller using ExtractFieldRefs to statically determine which root-level fields a template touches (see pkg/list/column.RequiredSections) must treat any template containing dynamic scope as unresolvable rather than misattributing those inner references to the root.

func HasRootReference

func HasRootReference(templateStr string) (bool, error)

HasRootReference reports whether a template contains a bare "." action (*parse.DotNode) -- e.g. "{{ . }}" or "{{ printf "%v" . }}" -- which accesses the entire root data value rather than a specific named field. ExtractFieldRefs only records *parse.FieldNode matches, so a template consisting solely of "{{ . }}" yields zero field references even though it exposes every field of the root, including ones no FieldNode ever names. Callers like pkg/list/column.RequiredSections that statically enumerate which root-level fields a template touches must treat any such reference as unresolvable rather than silently recording it as touching nothing.

func HasTemplateActions

func HasTemplateActions(str string) (bool, error)

HasTemplateActions checks if a string contains Go template actions. This is a more robust version that uses AST parsing instead of simple string matching.

func LookupFieldPath added in v1.223.0

func LookupFieldPath(data any, path []string) (any, bool)

LookupFieldPath resolves a field path against map-like data.

Types

type FieldRef

type FieldRef struct {
	Path []string // e.g., ["locals", "foo"] for .locals.foo
}

FieldRef represents a reference to a field in a template (e.g., .locals.foo).

func ExtractFieldRefs

func ExtractFieldRefs(templateStr string) ([]FieldRef, error)

ExtractFieldRefs parses a Go template string and extracts all field references. Handles complex expressions: conditionals, pipes, range, with blocks, nested templates. Returns nil if the string is not a valid template or contains no field references.

func ExtractPlainFieldRef added in v1.223.0

func ExtractPlainFieldRef(templateStr string) (FieldRef, bool, error)

ExtractPlainFieldRef returns the field reference when the template is exactly one plain field action, such as "{{ .locals.default_tags }}".

func (FieldRef) String

func (f FieldRef) String() string

String returns the dot-separated path of the field reference.

Jump to

Keyboard shortcuts

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