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 ¶
- func ExtractAllFieldRefsByPrefix(templateStr string, prefix string) ([]string, error)
- func ExtractFieldRefsByPrefix(templateStr string, prefix string) ([]string, error)
- func HasDynamicScope(templateStr string) (bool, error)
- func HasRootReference(templateStr string) (bool, error)
- func HasTemplateActions(str string) (bool, error)
- func LookupFieldPath(data any, path []string) (any, bool)
- type FieldRef
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractAllFieldRefsByPrefix ¶
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 ¶
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 ¶
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 ¶
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 ¶
HasTemplateActions checks if a string contains Go template actions. This is a more robust version that uses AST parsing instead of simple string matching.
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 ¶
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
ExtractPlainFieldRef returns the field reference when the template is exactly one plain field action, such as "{{ .locals.default_tags }}".