template

package
v1.202.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: Apache-2.0 Imports: 4 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 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.

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 (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