strict

package
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: Apache-2.0 Imports: 5 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package strict decides whether a template's variable reads are satisfied by the variables supplied to it.

The package is pure: it performs no file I/O, runs no compiler and writes no logs. Its inputs are a parsed template and the data that would be handed to Execute; its output is a Verdict naming every unsatisfied read at once.

The vocabulary:

Policy     what counts as void, and what a waiver must carry
Reference  one site in a template that reads a variable
Bindings   the data a template would be executed against
Verdict    the offences found, or none
Auditor    the port a renderer depends on to obtain a Verdict

A site that is deliberately allowed to render blank declares itself:

delim[[ optional .field "reason the blank is acceptable" ]]

What counts as void, precisely:

absent      the key is in no binding                        REFUSED
nil         the key is bound and carries no value           REFUSED
empty       the key is bound to an empty or blank string    REFUSED
zero        0, 0.0, false                                   allowed
empty list  an empty slice or map                           allowed

Scope of the audit: References resolves reads against the root data only. Inside the body of a range or a with the dot is rebound, so those reads are left to the template option missingkey=error, which fails the execution on the first miss and cannot be waived by optional.

Index

Constants

View Source
const FuncName = "optional"

FuncName is the template builtin that declares a blank to be deliberate. It has exactly one spelling, the command form:

delim[[ optional .field "reason the blank is acceptable" ]]

The pipe form is refused by the audit, because a pipe would hand optional its arguments in the opposite order and silently print the reason.

View Source
const MissingKeyOption = "missingkey=error"

MissingKeyOption is the text/template option a strict render executes under. It is the backstop for reads the static audit cannot resolve, not the primary check: it stops at the first miss and cannot see a bound-but-empty value at all.

Variables

This section is empty.

Functions

func Funcs

func Funcs() template.FuncMap

Funcs returns the template functions a strict render must install.

optional renders a void value as nothing. The reason is read by the audit, not by the render; requiring it here keeps a waiver that states none from executing even if the audit was bypassed.

Types

type Auditor

type Auditor interface {
	// Audit returns every refused reference at once, never only the first.
	Audit(template string, refs []Reference, vars Bindings) Verdict
}

Auditor decides whether a template's variable reads are satisfied.

It is the port a renderer depends on. Implementations must be pure: no file I/O, no compilation, no logging.

type Bindings

type Bindings map[string]any

Bindings is the data a template would be executed against, addressed by dotted path.

func FillWaived

func FillWaived(vars Bindings, refs []Reference) Bindings

FillWaived binds every waived reference that is absent to the empty string, so a declared blank survives execution under MissingKeyOption. It mutates and returns vars.

func (Bindings) Lookup

func (b Bindings) Lookup(key string) (value any, found bool)

Lookup resolves a dotted key. found is false when any segment of the path is missing, or when a non-final segment holds something that cannot be indexed by name.

func (Bindings) Set

func (b Bindings) Set(key string, value any) bool

Set binds a dotted key, creating intermediate maps as needed. It reports false when a non-final segment already holds something that is not a map.

type Cause

type Cause string

Cause names why a reference is an offence.

const (
	// CauseAbsent: the key is in no binding.
	CauseAbsent Cause = "absent from the supplied variables"
	// CauseNil: the key is bound to nothing.
	CauseNil Cause = "bound to nil"
	// CauseEmpty: the key is bound to an empty or whitespace-only string.
	CauseEmpty Cause = "bound to an empty string"
	// CauseUnexplainedWaiver: the site waived strictness without stating why.
	CauseUnexplainedWaiver Cause = "waived without a stated reason"
	// CauseMalformedWaiver: the site used the waiver builtin in a shape the
	// engine cannot honour.
	CauseMalformedWaiver Cause = "an unusable " + FuncName + " form"
)

type MissingVariablesError

type MissingVariablesError struct {
	Template string
	Offences []Offence
}

MissingVariablesError reports every unsatisfied variable read in a template.

func (*MissingVariablesError) Error

func (e *MissingVariablesError) Error() string

Error lists every offence, one per line, with its source position.

func (*MissingVariablesError) Keys

func (e *MissingVariablesError) Keys() []string

Keys lists the distinct offending keys.

type Offence

type Offence struct {
	Reference
	Cause Cause
}

Offence is one reference the policy refuses, with the cause.

type Policy

type Policy struct {
	// Void is the set of VoidKind values this policy refuses.
	Void VoidKind
	// RequireWaiverReason makes a waiver that states no reason an offence.
	RequireWaiverReason bool
}

Policy is the value that decides what counts as void and what a waiver must carry. It is data: build one, pass it, compare it.

Policy deliberately has no "lenient" setting. A render either holds every read to the policy or it does not; a blank that is intended is declared at the site that produces it, never by relaxing the policy for the document.

func Strict

func Strict() Policy

Strict is the default policy: every void kind is refused and every waiver must state a reason.

func (Policy) Classify

func (p Policy) Classify(value any, found bool) (cause Cause, ok bool)

Classify names the void kind of a looked-up value and reports whether this policy refuses it. A value that is not void, or a void kind this policy does not refuse, yields ok == false.

Values that are NOT void under any policy: a zero number, a false boolean, an empty collection. Each is a stated fact, not an unstated one.

func (Policy) Refuses

func (p Policy) Refuses(kind VoidKind) bool

Refuses reports whether the policy treats kind as void.

type Reference

type Reference struct {
	// Key is the dotted path read at this site, e.g. "section.range.start".
	Key string
	// Line is the 1-based source line of the site.
	Line int
	// Column is the 1-based source column of the site.
	Column int
	// Waived reports that the site declared a blank here to be deliberate.
	Waived bool
	// Reason is the justification the site stated for the waiver.
	Reason string
	// Malformed reports that the site used the waiver builtin in a shape the
	// engine cannot honour.
	Malformed bool
}

Reference is one site in a template that reads a variable off the root data.

Line and Column are 1-based positions in the template source.

func References

func References(tmpl *template.Template, source string) []Reference

References collects every root-scoped variable read in a parsed template.

source must be the text the template was parsed from; it is used only to turn byte offsets into line and column numbers.

Reads whose dot has been rebound (the body of a range or a with) are NOT collected, because their key is relative to a value this function cannot know. Those reads are held strict at execution time by the template option missingkey=error instead.

type StrictAuditor

type StrictAuditor struct {
	Policy Policy
}

StrictAuditor holds every reference to a Policy.

func Default

func Default() StrictAuditor

Default returns the Auditor a renderer uses when no other is injected.

func NewAuditor

func NewAuditor(policy Policy) StrictAuditor

NewAuditor returns an Auditor enforcing policy.

func (StrictAuditor) Audit

func (a StrictAuditor) Audit(template string, refs []Reference, vars Bindings) Verdict

Audit checks every reference against vars and returns all offences.

type Verdict

type Verdict struct {
	// Template names the template the references were read from.
	Template string
	// Offences holds every refused reference, ordered by position.
	Offences []Offence
}

Verdict is the whole result of auditing one template: every offence, not the first one.

func (Verdict) Err

func (v Verdict) Err() error

Err returns a *MissingVariablesError describing every offence, or nil.

func (Verdict) Keys

func (v Verdict) Keys() []string

Keys lists the distinct offending keys in order of first appearance.

func (Verdict) OK

func (v Verdict) OK() bool

OK reports that nothing was refused.

type VoidKind

type VoidKind uint8

VoidKind classifies one way a variable read can put nothing on the page. The kinds are distinct facts and are named separately so a report can say which one happened.

const (
	// VoidAbsent: no binding carries the key at all.
	VoidAbsent VoidKind = 1 << iota
	// VoidNil: a binding carries the key and its value is nil.
	VoidNil
	// VoidEmpty: a binding carries the key and its value is a string that is
	// empty or contains only whitespace.
	VoidEmpty
)

Jump to

Keyboard shortcuts

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