tmpl

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: MIT Imports: 13 Imported by: 0 modules, 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 tmpl 是通知的模板引擎:Go 的 text/template,加上一组受控的函数与防护。 用户写的模板在通知管线里执行,所以要保证它很快跑完、输出有限:源码、输出与函数的中间结果都有上限,不能定义子模板, range 只能遍历声明为列表的变量,整份模板按最坏情况算的执行步数与声明的变量个数有上限, 一次渲染里所有函数处理的字节数合计也有上限。

Index

Constants

View Source
const (
	// MaxSource 是模板源码的上限(字节)
	MaxSource = 8 << 10
	// MaxList 是一个列表变量最多的项数:事件建出来时就截到这么多,迭代预算按它算
	MaxList = 100
	// MaxRangeDepth 是 range 最多嵌套几层
	MaxRangeDepth = 2
	// MaxCost 是整份模板按最坏情况算的执行步数上限:每个节点、每条命令、每个参数各算一步,range 的循环体按 MaxList 遍算。
	// 内置函数(len、slice、and、or、not)、取字段、取变量每一步都很快,不另外计量,靠它限住总的步数。
	MaxCost = 200000
	// MaxVars 是一份模板最多声明几个变量:取变量要从最近声明的一个往回找,变量越多每一步越慢
	MaxVars = 64
)

Variables

View Source
var ErrInvalid = errors.New("模板不对")

ErrInvalid 包着所有「模板写得不对」的错误(解析、防护、渲染):接口据此回 400,发送时据此退回内置文字。

Functions

This section is empty.

Types

type Kind

type Kind int

Kind 是模板的用途,决定输出上限。

const (
	// Title 是消息标题
	Title Kind = iota
	// Text 是消息正文
	Text
	// Body 是 Webhook 的请求体
	Body
	// URL 是 Webhook 的地址
	URL
	// Reply 是 ChatOps 命令回复与 AI 助手回复的正文(助手的回答可能很长)
	Reply
)

func (Kind) Limit

func (k Kind) Limit() int

Limit 是这种用途的输出上限(字节)。

type Template

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

Template 是解析好、通过了防护检查的模板。

func Parse

func Parse(src string, kind Kind, lists map[string]bool) (*Template, error)

Parse 解析模板并做结构检查。lists 是可以 range 的列表变量名(事件目录里声明为列表的变量)。

func (*Template) Check

func (t *Template) Check(data any) (string, error)

Check 渲染一遍,变量名写错时报错:保存与预览用(示例数据里有全部变量)。

func (*Template) Execute

func (t *Template) Execute(data any) (string, error)

Execute 渲染模板:变量缺了按零值处理(数据里已经放好所有事件的全部变量,正常不会缺)。

Jump to

Keyboard shortcuts

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