template

package
v3.23.8 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxRecursionDepth = 250
)

MaxRecursionDepth is the maximum allowed nesting depth for ProcessValue/ProcessStream.

Variables

View Source
var ErrMaxRecursionDepthExceeded = errors.New("maximum recursion depth exceeded")

ErrMaxRecursionDepthExceeded is returned when structure nesting exceeds MaxRecursionDepth.

View Source
var ErrRenderLimit = errors.New("template rendered past the size limit")

ErrRenderLimit is returned when a template writes more than the caller allows.

Functions

func Functions

func Functions(ctx context.Context, reg pkgplugins.Registry) map[string]any

func HasTemplatesInHeaders

func HasTemplatesInHeaders(headers map[string]string) bool

HasTemplatesInHeaders reports whether any header value contains a template.

func HasTemplatesInValue added in v3.20.0

func HasTemplatesInValue(value any) bool

HasTemplatesInValue reports whether any string anywhere inside the value contains a template.

func IsTemplateString

func IsTemplateString(s string) bool

IsTemplateString checks if a string contains template syntax. The closing delimiter must appear AFTER the opening one — "a }} b {{ c" is not a template.

Types

type Data

type Data struct {
	Request      map[string]any `json:"request"`
	Headers      map[string]any `json:"headers"`
	MessageIndex int            `json:"messageIndex"`
	RequestTime  time.Time      `json:"requestTime"`
	// Timestamp is a backward-compatibility alias for RequestTime
	Timestamp     time.Time      `json:"timestamp"`
	State         map[string]any `json:"state"`
	Requests      []any          `json:"requests"`
	AttemptNumber int            `json:"attemptNumber"` // Current attempt number (1-based)
	// AttemptIndex is a backward-compatibility alias for AttemptNumber
	AttemptIndex int `json:"attemptIndex"`
	MaxAttempts  int `json:"maxAttempts"` // Maximum number of attempts for this stub
	// TotalAttempts is a backward-compatibility alias for MaxAttempts
	TotalAttempts int    `json:"totalAttempts"`
	StubID        string `json:"stubId"` // Unique identifier of the stub
	// RequestID is a backward-compatibility alias mapped to StubID
	RequestID string `json:"requestId"`
}

Data represents the context data available for template rendering.

type Engine

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

Engine provides template rendering functionality.

func New

func New(ctx context.Context, reg plugins.Registry) *Engine

New creates a new template engine with custom functions.

func (*Engine) ProcessError

func (e *Engine) ProcessError(errorStr string, templateData Data) (string, error)

ProcessError processes error template.

func (*Engine) ProcessHeaders

func (e *Engine) ProcessHeaders(headers map[string]string, templateData Data) error

ProcessHeaders processes templates in headers.

func (*Engine) ProcessValue added in v3.22.0

func (e *Engine) ProcessValue(value any, templateData Data) (any, error)

func (*Engine) Render

func (e *Engine) Render(tmpl string, data Data) (result string, err error)

Render renders a template string with the given data.

func (*Engine) RenderLimited added in v3.22.0

func (e *Engine) RenderLimited(tmpl string, data Data, limit int) (result string, err error)

RenderLimited renders like Render but stops as soon as the output exceeds limit bytes, so a runaway template cannot buffer the whole document first.

func (*Engine) Validate added in v3.21.0

func (e *Engine) Validate(tmpl string) error

Jump to

Keyboard shortcuts

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