vars

package
v1.5.7 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package vars defines first-class, typed template variables and the resolver that turns variable definitions plus caller-supplied values into a resolution scope used to render deployment sources.

Variables may be plain (string/int/bool/enum) with defaults and validation, secret-typed (resolved to a github.com/xraph/ctrlplane/provider.SecretBinding and never inlined into rendered output), or computed from a Go text/template expression over previously-resolved variables and derived context.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidDefinition indicates a malformed variable definition.
	ErrInvalidDefinition = errors.New("vars: invalid variable definition")

	// ErrMissingRequired indicates a required variable had no value or default.
	ErrMissingRequired = errors.New("vars: required variable not provided")

	// ErrInvalidValue indicates a value failed type, enum, or pattern validation.
	ErrInvalidValue = errors.New("vars: invalid variable value")

	// ErrCycle indicates computed variables form an unresolvable cycle or
	// reference a variable that is never defined.
	ErrCycle = errors.New("vars: computed variable cycle or undefined reference")
)

Functions

func ValidateDefinitions

func ValidateDefinitions(defs []Definition) error

ValidateDefinitions checks a set of variable definitions for structural validity and duplicate names without resolving any values. Useful for validating a template's variables at author time.

Types

type Definition

type Definition struct {
	Name        string              `json:"name"                  validate:"required"`
	Description string              `json:"description,omitempty"`
	Type        Type                `json:"type"                  validate:"required"`
	Required    bool                `json:"required,omitempty"`
	Default     any                 `json:"default,omitempty"`
	Enum        []string            `json:"enum,omitempty"`
	Secret      *provider.SecretRef `json:"secret,omitempty"`
	Expression  string              `json:"expression,omitempty"`
	Pattern     string              `json:"pattern,omitempty"`
}

Definition declares a single template variable. It is serialized into a template's variables JSONB column, so only JSON tags are needed.

type InstanceContext

type InstanceContext struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

InstanceContext is the per-instance derived context exposed to templates as {{ .instance.id }} and {{ .instance.name }}.

type Resolver

type Resolver struct{}

Resolver turns variable definitions and caller-supplied values into a resolved Scope plus the secret bindings the provider must materialize.

func NewResolver

func NewResolver() *Resolver

NewResolver returns a Resolver.

func (*Resolver) Resolve

func (r *Resolver) Resolve(
	_ context.Context,
	defs []Definition,
	values map[string]any,
	derived Scope,
) (Scope, []provider.SecretBinding, error)

Resolve validates definitions, applies values and defaults, evaluates computed expressions, and collects secret bindings. derived seeds the instance, tenant, and region context. The returned Scope's Var map holds every plain and computed variable; secret variables are excluded from Var and returned as bindings.

type Scope

type Scope struct {
	Var      map[string]any
	Instance InstanceContext
	Tenant   TenantContext
	Region   string
}

Scope is the resolved variable context handed to the renderer. Var holds every resolved plain and computed variable; secret variables are excluded (they are returned as bindings instead).

func (Scope) Root

func (s Scope) Root() map[string]any

Root builds the case-sensitive template root so expressions use lowercase selectors: {{ .var.x }}, {{ .instance.id }}, {{ .tenant.id }}, {{ .region }}. It is exported so the render package can evaluate templates against the same scope shape the resolver uses for computed expressions.

type TenantContext

type TenantContext struct {
	ID string `json:"id"`
}

TenantContext is the tenant-scoped derived context exposed as {{ .tenant.id }}.

type Type

type Type string

Type enumerates the variable value kinds.

const (
	// TypeString is a free-form string variable.
	TypeString Type = "string"

	// TypeInt is an integer variable.
	TypeInt Type = "int"

	// TypeBool is a boolean variable.
	TypeBool Type = "bool"

	// TypeEnum is a string variable constrained to its Enum members.
	TypeEnum Type = "enum"

	// TypeSecret is a secret reference resolved to a binding, never inlined.
	TypeSecret Type = "secret"
)

Jump to

Keyboard shortcuts

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