template

package
v1.4.0 Latest Latest
Warning

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

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

Documentation

Overview

Package template evaluates `{{ }}` expressions and renders templates with typed variables.

Index

Constants

This section is empty.

Variables

View Source
var ErrFileAccessDenied = errors.New("file access denied")

ErrFileAccessDenied reports a file outside the allowed root.

Functions

This section is empty.

Types

type Env

type Env struct {
	Vars Vars
	Now  func() time.Time
	UUID func() string
	// ReadFile reads a file named in the request file. It returns an error
	// wrapping ErrFileAccessDenied for a path outside the allowed root.
	ReadFile func(name string) ([]byte, error)
	// Missing, when set, is consulted by Eval for a variable Vars does
	// not define, instead of failing with an UndefinedVariable error: it
	// returns the value to use in its place and whether it applied
	// (false falls through to the normal undefined-variable error, so
	// Missing may itself be selective). A run never sets this (nil,
	// meaning every undefined variable is an error, as always); it
	// exists for engine.RenderCurl, which renders a curl command line
	// for a variable a capture would only define once the entry actually
	// ran, and must not fail just because that hasn't happened. Vars
	// itself is never mutated to remember a Missing result: each call
	// site (an entry, in RenderCurl's case) gets its own answer, so nothing
	// leaks to a different context sharing the same Env.
	Missing func(name string) (value.Value, bool)
}

Env is the evaluation environment of an entry: its variables, the sources of the template functions and file access. Now and UUID default to the system clock and a random version 4 UUID; without ReadFile no file can be read.

func (*Env) Eval

func (e *Env) Eval(x syntax.Expr) (value.Value, error)

Eval evaluates an expression: a variable lookup or a function call.

func (*Env) File

func (e *Env) File(name *syntax.Template) ([]byte, error)

File renders a file name and reads the file.

func (*Env) Regex

func (e *Env) Regex(n syntax.Node, span syntax.Span) (value.Regex, error)

Regex returns the regex of a filter, query or predicate argument: a /regex/ literal, or a string template compiled after rendering. span locates errors on a literal.

func (*Env) Render

func (e *Env) Render(t *syntax.Template) (string, error)

Render renders a template. Text after a placeholder's expression (as in `{{a b}}`) is ignored.

func (*Env) RenderExpr

func (e *Env) RenderExpr(x syntax.Expr) (string, error)

RenderExpr evaluates an expression and renders its value as text.

func (*Env) RenderJSON

func (e *Env) RenderJSON(v syntax.JSONValue, keepSpace bool) (string, error)

RenderJSON renders a JSON value with its placeholders; keepSpace keeps the white space as written.

func (*Env) RenderMultiline

func (e *Env) RenderMultiline(m *syntax.MultilineString) (string, error)

RenderMultiline renders a multiline string. Raw strings are not templated; a GraphQL string becomes the JSON document `{"query":…,"variables":…}`.

type Variable

type Variable struct {
	Value  value.Value
	Secret bool
}

Variable is a named value; secret values are redacted from every output.

type Vars

type Vars map[string]Variable

Vars maps variable names to values.

func (Vars) Get

func (v Vars) Get(name string) (value.Value, bool)

Get returns the value of a variable.

func (Vars) Set

func (v Vars) Set(name string, val value.Value)

Set defines a public variable.

func (Vars) SetSecret

func (v Vars) SetSecret(name, s string)

SetSecret defines a secret string variable.

Jump to

Keyboard shortcuts

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