Documentation
¶
Overview ¶
Package render renders Go templates with the Gomplate, Sprig and Atmos function sets. It is the only package that imports gomplate, so the rest of the codebase is insulated from gomplate's library API.
Templates are parsed and executed with text/template directly, lock-free, which matters because stack files are rendered concurrently. Gomplate's renderer-only functions are provided without its renderer: Data reads datasources lazily and the `tmpl` namespace is bound to the parsed template. Gomplate's renderer is used only to read datasources, including the context datasources of Request.ContextSources; it updates package-global metrics, so those reads are serialized.
Index ¶
- Constants
- func CSV(value string) ([][]string, error)
- func CachedDatasource(sources *Data, alias string, args ...string) (any, error)
- func GomplateFuncs(ctx context.Context, sources *Data) template.FuncMap
- func JSON(value string) (map[string]any, error)
- func SprigFuncMap() template.FuncMap
- func TOML(value string) (any, error)
- func WithExperimental(ctx context.Context) context.Context
- type Data
- func (d *Data) Datasource(alias string, args ...string) (any, error)
- func (d *Data) DatasourceExists(alias string) bool
- func (d *Data) DatasourceReachable(alias string, args ...string) bool
- func (d *Data) Define(alias string, source Datasource) error
- func (d *Data) DefineDatasource(alias, value string) (string, error)
- func (d *Data) Include(alias string, args ...string) (string, error)
- func (d *Data) ListDatasources() []string
- type Datasource
- type DatasourceCache
- type Engine
- type FuncsWrapper
- type Option
- type Renderer
- type Request
- type Source
Constants ¶
const ( // MissingKeyError makes execution fail when a map is indexed with a missing key. MissingKeyError = "error" // MissingKeyDefault substitutes the zero value when a map is indexed with a missing key. MissingKeyDefault = "default" // DefaultLeftDelim is the default left action delimiter. DefaultLeftDelim = "{{" // DefaultRightDelim is the default right action delimiter. DefaultRightDelim = "}}" )
Variables ¶
This section is empty.
Functions ¶
func CachedDatasource ¶
CachedDatasource reads a datasource through the process-wide cache that backs `atmos.GomplateDatasource`, so a datasource referenced from many stacks and components is fetched once.
func GomplateFuncs ¶
GomplateFuncs returns Gomplate's function map bound to ctx, with the datasource functions served by sources (an empty Data when nil) and, on Windows, the native file namespace. It does not include the `tmpl` namespace, which gomplate only provides through its renderer.
func SprigFuncMap ¶
SprigFuncMap returns the shared Sprig function map. Callers must not mutate it.
func WithExperimental ¶
WithExperimental enables gomplate's experimental functions for renders bound to ctx. Use it instead of gomplate.SetExperimental: it sets both this package's marker (which selects the shared function maps) and Gomplate's own flag. Contexts enabled with gomplate.SetExperimental alone are recognized too, as long as Gomplate's flag can be located (see upstreamExperimentalKey).
Types ¶
type Data ¶
type Data struct {
Ctx context.Context
Sources map[string]*Source
ExtraHeaders map[string]http.Header
// contains filtered or unexported fields
}
Data holds the datasources belonging to a single template evaluation and reads them lazily, the way gomplate's `datasource`/`ds`/`include` functions do. Reads go through gomplate's own datasource reader, captured once per alias from a private gomplate renderer, so datasource values keep gomplate's types (including integers and EJSON values) and the template being rendered never needs gomplate's renderer itself.
Like a gomplate Renderer, a Data value must not be used concurrently.
func (*Data) Datasource ¶
Datasource reads and parses a datasource without serializing its native return value.
func (*Data) DatasourceExists ¶
DatasourceExists reports whether the alias has been registered.
func (*Data) DatasourceReachable ¶
DatasourceReachable preserves Gomplate's registered-alias probe behavior.
func (*Data) Define ¶
func (d *Data) Define(alias string, source Datasource) error
Define registers a configured datasource definition, parsing its URL as Request.Datasources does (including Windows drive-letter file URLs).
func (*Data) DefineDatasource ¶
DefineDatasource registers an alias once, using Gomplate's own URL parsing rules.
func (*Data) ListDatasources ¶
ListDatasources returns the registered aliases in stable order.
type Datasource ¶
type Datasource struct {
// URL is the datasource URL (any scheme gomplate supports). A bare absolute
// path is treated as a file:// URL.
URL string
// Headers are extra HTTP headers sent to http(s) datasources.
Headers map[string][]string
}
Datasource is a named gomplate datasource definition.
type DatasourceCache ¶
type DatasourceCache struct {
// contains filtered or unexported fields
}
DatasourceCache memoizes datasource reads by alias and arguments so that a datasource referenced many times across a stack is fetched once. It keeps at most a fixed number of entries, evicting the least recently used.
func NewDatasourceCache ¶
func NewDatasourceCache() *DatasourceCache
NewDatasourceCache creates an empty DatasourceCache.
func (*DatasourceCache) Read ¶
Read returns the parsed value of the datasource registered under alias in sources, reading it through sources only on a cache miss. The cache key includes the alias's effective definition, so two stacks that define the same alias differently never see each other's values. Nil values are not cached.
type Engine ¶
type Engine interface {
Renderer
}
Engine is the Renderer that serves Gomplate, Sprig and caller functions.
type FuncsWrapper ¶
FuncsWrapper transforms the assembled function map for one render.
type Option ¶
type Option func(*engine)
Option configures an Engine.
func WithDeprecationReporter ¶
func WithDeprecationReporter(reporter compatibility.DeprecationReporter) Option
WithDeprecationReporter overrides the reporter notified when a template uses a gomplate v3 name kept alive by the compatibility shims. The default logs a warning via pkg/logger, deduplicated per template per name.
func WithGomplate ¶
WithGomplate enables or disables the Gomplate function set (and datasources).
type Request ¶
type Request struct {
// Name is the template name used in error messages.
Name string
// Text is the template source.
Text string
// Data is the value bound to `.` when the template executes. It may be a
// map or any Go value; it is never serialized.
Data any
// Funcs are functions merged last, so they override Gomplate and Sprig
// functions with the same name (e.g. the `atmos` namespace).
Funcs template.FuncMap
// LeftDelim and RightDelim override the default action delimiters.
LeftDelim string
RightDelim string
// MissingKey is the text/template "missingkey" option; empty means MissingKeyError.
MissingKey string
// Datasources are read lazily when the template calls `datasource`/`ds`/`include`.
// They are added to Sources (or to a fresh Data when Sources is nil).
Datasources map[string]Datasource
// Sources serves the template's datasource functions. Pass the same Data to
// functions that read datasources themselves (such as the `atmos`
// namespace) so they share its definitions. Nil means a fresh Data.
Sources *Data
// ContextSources are read eagerly before execution and exposed on `.`; the
// alias "." replaces the whole context, as in gomplate's renderer. When set,
// Data is ignored. They are also readable as datasources (for example
// `ds "config"`).
ContextSources map[string]Datasource
// WrapFuncs, when set, receives the fully assembled function map (Gomplate,
// Sprig, compatibility shims and Funcs) and the render's datasources, and
// returns the map the template is parsed with. Policy admission uses it to
// guard every function call. Gomplate's `tmpl` namespace and `tpl` alias
// need the parsed root, so they are passed through it in a second call of
// their own once they are bound.
WrapFuncs FuncsWrapper
// Prepare, when set, runs on the parsed template before it executes, for
// callers that rewrite or guard the parse tree. Gomplate's `tmpl`/`tpl`
// functions are unavailable when it is set, so run-time templates can't
// escape it. It does not apply to ContextSources renders.
Prepare func(*template.Template)
}
Request describes one template render.