tmpl

package
v0.234.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package tmpl parses and renders response templates: JSON bodies and header values that hold {{uuid}}, {{now}}, {{path.NAME}} and {{body.PATH}} placeholders.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PathNames

func PathNames(template string) []string

PathNames returns the {name} segments of an OpenAPI path template, in order.

func PathValues

func PathValues(template, escapedPath string) map[string]string

PathValues matches an escaped request path against template segment by segment, path-unescaping each matched value. Returns nil when the segment counts differ.

Types

type Template

type Template struct {
	// contains filtered or unexported fields
}

Template is a parsed response template. It is immutable after Parse, so Render is safe for concurrent use.

func Parse

func Parse(body json.RawMessage, headers map[string]string, pathNames []string) (*Template, error)

Parse checks body (JSON, may be empty) and header values. The pathNames are the parameter names in the operation's template; {{path.X}} with X not in it is an error.

func (*Template) BodyPointers

func (t *Template) BodyPointers() []string

BodyPointers returns the JSON pointers ("/a/0/b") of every string value that is a single {{body.*}} placeholder, sorted. Such a value takes the type of the request field when served.

func (*Template) Render

func (t *Template) Render(v Vars) (body []byte, headers map[string]string, err error)

Render returns the rendered body (nil when the template body was empty) and headers.

func (*Template) TextPointers

func (t *Template) TextPointers(unresolved []string) []string

TextPointers returns the JSON pointers, sorted, of every other templated string value whose text depends on the request: it embeds a {{body.*}} placeholder or names a path parameter in unresolved. Such a value is always a string, but its text is only known when served.

type Vars

type Vars struct {
	UUID string
	Now  time.Time
	Path map[string]string
	Body []byte
	// Sample marks a registration-time render: a whole-string body
	// placeholder with no value renders "" in place instead of being dropped.
	Sample bool
}

Vars are the per-call inputs. Body is the raw request body (may be nil or non-JSON).

Jump to

Keyboard shortcuts

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