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 ¶
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.
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 ¶
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 ¶
Bindings is the data a template would be executed against, addressed by dotted path.
func FillWaived ¶
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.
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 ¶
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 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 ¶
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.
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 ¶
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.
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.