templating

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package templating provides shared template evaluation, validation, normalization, and storage-reference handling for BubuStack runtimes.

Index

Constants

View Source
const (
	// TemplateExprKey marks an object value as a template expression wrapper.
	TemplateExprKey = "$bubuTemplate"
	// TemplateVarsKey carries per-expression template variables.
	TemplateVarsKey = "$bubuTemplateVars"

	// StorageRefKey marks a value as offloaded storage-backed data.
	StorageRefKey = "$bubuStorageRef"
	// StoragePathKey identifies the nested path within an offloaded storage object.
	StoragePathKey = "$bubuStoragePath"
)
View Source
const (
	// RootInputs is the template root for resolved step inputs.
	RootInputs = "inputs"
	// RootSteps is the template root for upstream step outputs.
	RootSteps = "steps"
	// RootPacket is the template root for transport packet metadata.
	RootPacket = "packet"
)

Variables

This section is empty.

Functions

func ExtractStepReferences

func ExtractStepReferences(value string) []string

ExtractStepReferences parses a Go template string and returns the names of steps referenced via `(index .steps "name")` or `.steps.name` patterns. Returns nil if no step references are found.

func ExtractStepReferencesWithError

func ExtractStepReferencesWithError(value string) ([]string, error)

ExtractStepReferencesWithError returns step references or a parse error.

func HasStorageRefsInStepVars

func HasStorageRefsInStepVars(text string, vars map[string]any) bool

HasStorageRefsInStepVars parses a template string, extracts referenced step names, and checks whether any of them contain $bubuStorageRef in their output.

func ValidateJSONTemplates

func ValidateJSONTemplates(raw []byte, scope ExpressionScope) error

ValidateJSONTemplates walks a JSON blob and validates any template strings.

func ValidateTemplateString

func ValidateTemplateString(value string, scope ExpressionScope) error

ValidateTemplateString validates a single template string against the scope.

Types

type CacheConfig

type CacheConfig struct {
	MaxSize int
	TTL     time.Duration
}

CacheConfig controls template caching.

func DefaultCacheConfig

func DefaultCacheConfig() *CacheConfig

DefaultCacheConfig returns the production-oriented cache defaults.

type CachedTemplate

type CachedTemplate struct {
	Template *template.Template
	CachedAt time.Time
	LastUsed time.Time
	HitCount int64
	Hash     string
}

CachedTemplate holds a parsed template and metadata.

type Config

type Config struct {
	EvaluationTimeout time.Duration
	MaxOutputBytes    int
	// Deterministic disables non-deterministic helpers (now/rand).
	Deterministic bool
}

Config controls template evaluation behavior.

type ErrEvaluationBlocked

type ErrEvaluationBlocked struct {
	Reason string
}

ErrEvaluationBlocked indicates evaluation is waiting on upstream data.

func (*ErrEvaluationBlocked) Error

func (e *ErrEvaluationBlocked) Error() string

type ErrOffloadedDataUsage

type ErrOffloadedDataUsage struct {
	Reason string
}

ErrOffloadedDataUsage indicates a template requires offloaded payload data.

func (*ErrOffloadedDataUsage) Error

func (e *ErrOffloadedDataUsage) Error() string

type Evaluator

type Evaluator struct {
	// contains filtered or unexported fields
}

Evaluator resolves BubuStack template expressions against JSON-like values.

func New

func New(cfg Config) (*Evaluator, error)

New creates an Evaluator with the supplied configuration.

func (*Evaluator) Close

func (e *Evaluator) Close()

Close releases the evaluator's background resources.

func (*Evaluator) EvaluateCondition

func (e *Evaluator) EvaluateCondition(ctx context.Context, expr string, vars map[string]any) (bool, error)

EvaluateCondition evaluates a boolean template.

func (*Evaluator) ResolveTemplateString

func (e *Evaluator) ResolveTemplateString(ctx context.Context, value string, vars map[string]any) (any, error)

ResolveTemplateString resolves a single template string with the provided vars.

func (*Evaluator) ResolveValue

func (e *Evaluator) ResolveValue(ctx context.Context, value any, vars map[string]any) (any, error)

ResolveValue resolves template expressions within any JSON-like value. This is a general-purpose helper for SDK callers that need to resolve templates outside of an object-only context.

func (*Evaluator) ResolveWithInputs

func (e *Evaluator) ResolveWithInputs(
	ctx context.Context,
	with map[string]any,
	vars map[string]any,
) (map[string]any, error)

ResolveWithInputs resolves template expressions inside a JSON-like map.

type ExpressionScope

type ExpressionScope struct {
	Name         string
	AllowNow     bool
	AllowRandom  bool
	AllowedRoots map[string]struct{}
	AllowRange   bool
}

ExpressionScope defines allowed template contexts for validation.

func NewExpressionScope

func NewExpressionScope(
	name string,
	allowNow bool,
	allowRandom bool,
	allowRange bool,
	roots ...string,
) ExpressionScope

NewExpressionScope builds a scope with provided roots.

type TemplateCache

type TemplateCache struct {
	// contains filtered or unexported fields
}

TemplateCache caches parsed templates.

func NewTemplateCache

func NewTemplateCache(cfg *CacheConfig) *TemplateCache

NewTemplateCache builds a cache for parsed templates.

func (*TemplateCache) GetOrParse

func (c *TemplateCache) GetOrParse(
	ctx context.Context,
	key string,
	parseFn func() (*template.Template, error),
) (*template.Template, error)

GetOrParse returns a cached template or parses and stores a new one.

func (*TemplateCache) Stop

func (c *TemplateCache) Stop()

Stop terminates the background cleanup loop.

Jump to

Keyboard shortcuts

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