Documentation
¶
Overview ¶
Package render turns a release event into the body a sink delivers, with the subscription's Go text/template or, when it has none, the event's CloudEvents JSON (architecture.md §3.5, ADR 0016, docs/templates.md).
A template sees the event envelope as its JSON object: the dot is the decoded CloudEvents structured-mode event, so the fields have their spec names (.type, .id, .subject, .data.package). A missing key is an error, not "<no value>"; labels, which a watch may not have, are read with index: {{index .data.watch.labels "team" | default "none"}}.
Templates are written by whoever controls the configuration, but they run inside the dispatcher, so execution is bounded:
- Compile rejects templates over Limits.MaxTemplateBytes and the define, template and block actions, then instruments every range body with a deadline check;
- execution stops with ErrTimeout after Limits.Timeout (or when the context ends, if earlier), checked in every loop iteration, every output write and every function call, and with ErrOutputTooLarge when the output, or any string a function builds, would pass Limits.MaxOutputBytes;
- the functions are a curated set (FuncNames): no environment, file, network or clock access; the formatting builtins are replaced by size-checked versions that take scalar arguments only.
Error messages (Error) give a position and a reason from a fixed set, never the template text.
Template.Check runs a template against the sample events (Samples), which is how configuration loading validates it.
Index ¶
Examples ¶
Constants ¶
const ( DefaultTimeout = time.Second DefaultMaxOutputBytes = 256 << 10 DefaultMaxTemplateBytes = 32 << 10 // CheckTimeout bounds each sample render of Template.Check, so a slow // template cannot hold up configuration loading. CheckTimeout = 250 * time.Millisecond )
Default limits (ADR 0016).
const ( // ContentTypeCloudEvents is the default body: the event in CloudEvents // JSON structured mode. ContentTypeCloudEvents = "application/cloudevents+json; charset=utf-8" // ContentTypeJSON is a template output that is valid JSON. ContentTypeJSON = "application/json; charset=utf-8" // ContentTypeText is any other template output. ContentTypeText = "text/plain; charset=utf-8" )
Content types of a Rendered body.
Variables ¶
var ( // ErrParse: the template does not parse. ErrParse = errors.New("render: template does not parse") // ErrNotAllowed: the template uses something Compile rejects (size, // define, template, block). ErrNotAllowed = errors.New("render: template not allowed") // ErrExec: execution failed (missing key, nil value, function error). ErrExec = errors.New("render: template execution failed") // ErrTimeout: execution ran past its deadline or its context ended. ErrTimeout = errors.New("render: template execution timed out") // ErrOutputTooLarge: the output or a string a function builds would pass // MaxOutputBytes. ErrOutputTooLarge = errors.New("render: template output too large") // ErrInvalidEvent: the event to render is not a JSON object. ErrInvalidEvent = errors.New("render: event is not a JSON object") )
Error kinds; an *Error unwraps to one of them.
Functions ¶
Types ¶
type Error ¶
type Error struct {
// Line and Col locate the error in the template, 0 when unknown.
Line, Col int
// Reason says what went wrong.
Reason string
// contains filtered or unexported fields
}
Error is a template error. Its message gives the position and the reason but never quotes the template text, which may hold something the configuration's credential check did not catch.
type Rendered ¶
type Rendered struct {
Body []byte
// ContentType is ContentTypeCloudEvents without a template; with one,
// ContentTypeJSON when Body is valid JSON and ContentTypeText otherwise.
ContentType string
// Templated tells whether a subscription template produced Body.
Templated bool
}
Rendered is the body a sink delivers.
func Render ¶
Render renders the event envelope (the JSON of an event.Envelope, as stored in event.payload_json) with t, or returns the envelope unchanged as the CloudEvents body when t is nil.
Example (Default) ¶
Without a template the body is the CloudEvents JSON event.
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/angelia-dev/angelia/internal/render"
)
func fullSample() []byte {
b, err := json.Marshal(render.Samples()[0].Envelope)
if err != nil {
panic(err)
}
return b
}
func main() {
r, err := render.Render(context.Background(), nil, fullSample())
if err != nil {
panic(err)
}
fmt.Println(r.ContentType, r.Templated)
}
Output: application/cloudevents+json; charset=utf-8 false
type Sample ¶
type Sample struct {
// Name says what the sample covers: "full" or "sparse".
Name string
Envelope event.Envelope
}
Sample is an event a template is checked against.
func Samples ¶
func Samples() []Sample
Samples are the events Check renders: "full" has every nullable field set, notes and labels included; "sparse" is a first version with every nullable field null, no watch name and no labels. A template that renders both handles any event the spec allows, as long as it guards nullable fields with "with" or "if" (docs/templates.md).
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template is a compiled subscription template. It is safe for concurrent use.
func Compile ¶
Compile parses src as a message template. src must not be empty: a subscription without a template renders the default (Render with a nil Template).
func (*Template) Check ¶
Check renders t against every sample event and returns the first failure, naming the sample. Configuration loading calls it so that a template that cannot render, or renders past the limits, is rejected before any delivery uses it. It does no I/O and each sample is bounded by CheckTimeout (or Limits.Timeout if shorter), so it takes no context.
func (*Template) CheckOutput ¶
CheckOutput is Check that also passes the output for each sample event to check (when not nil); its error is returned naming the sample.
func (*Template) Render ¶
Render executes t on the event envelope JSON.
Example ¶
A chat message.
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/angelia-dev/angelia/internal/render"
)
func fullSample() []byte {
b, err := json.Marshal(render.Samples()[0].Envelope)
if err != nil {
panic(err)
}
return b
}
func main() {
t, err := render.Compile(`{{.data.package}} {{.data.version}} is out{{with .data.previous_version}} (was {{.}}){{end}}
{{with .data.urls.release}}{{.}}{{end}}`, render.Limits{})
if err != nil {
panic(err)
}
if err := t.Check(); err != nil {
panic(err)
}
r, err := t.Render(context.Background(), fullSample())
if err != nil {
panic(err)
}
fmt.Println(r.ContentType)
fmt.Println(string(r.Body))
}
Output: text/plain; charset=utf-8 requests 2.33.0 is out (was 2.32.5) https://pypi.org/project/requests/2.33.0/
Example (Json) ¶
A JSON body: toJSON quotes and escapes every value.
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/angelia-dev/angelia/internal/render"
)
func fullSample() []byte {
b, err := json.Marshal(render.Samples()[0].Envelope)
if err != nil {
panic(err)
}
return b
}
func main() {
t, err := render.Compile(`{"text": {{printf "%s %s released" .data.package .data.version | toJSON}}, "team": {{index .data.watch.labels "team" | default "none" | toJSON}}}`, render.Limits{})
if err != nil {
panic(err)
}
r, err := t.Render(context.Background(), fullSample())
if err != nil {
panic(err)
}
fmt.Println(r.ContentType)
fmt.Println(string(r.Body))
}
Output: application/json; charset=utf-8 {"text": "requests 2.33.0 released", "team": "platform"}