Documentation
¶
Index ¶
- Variables
- func Bind(tmpl *template.Template, setter *OrderedMap, getter ...*OrderedMap)
- func NewTemplate(name string) *template.Template
- func Plaintext(text string) (string, error)
- func Range(tmpl *template.Template, data any, ranger, setter *OrderedMap, ...) error
- func ToBuffer(tmpl *template.Template, text string, data any) (*bytes.Buffer, error)
- func ToString(tmpl *template.Template, text string, data any, getter ...*OrderedMap) (string, error)
- func TrimEnvPrefix(s string) (string, bool)
- func TrimSetPrefix(s string) (string, bool)
- type CycleError
- type DatabaseLoader
- type FileLoader
- type Getter
- type Loader
- type OrderedMap
- func (o *OrderedMap) Clone() *OrderedMap
- func (o *OrderedMap) Del(key string)
- func (o *OrderedMap) Get(key string) (value any, exists bool)
- func (o *OrderedMap) Iterate(yield func(key string, value any) bool)
- func (o *OrderedMap) Keys() (keys []string)
- func (o *OrderedMap) Len() (i int)
- func (o *OrderedMap) Map() (m map[string]any)
- func (o *OrderedMap) MarshalJSON() ([]byte, error)
- func (o *OrderedMap) MarshalYAML() (any, error)
- func (o *OrderedMap) Replace(f func(key string, value any) (newValue any, shouldReplace bool))
- func (o *OrderedMap) Set(key string, value any)
- func (o *OrderedMap) String() string
- func (o *OrderedMap) UnmarshalJSON(b []byte) error
- func (o *OrderedMap) UnmarshalYAML(node *yaml.Node) error
- func (o *OrderedMap) Update(n *OrderedMap) *OrderedMap
- func (o *OrderedMap) Values() (values []any)
- type Request
- func (r *Request) Body(*http.Request, reflect.Value, []reflect.StructField) (io.Reader, error)
- func (r *Request) Cookie(req *http.Request, _ reflect.Value, _ []reflect.StructField) (err error)
- func (r *Request) Header(req *http.Request, _ reflect.Value, _ []reflect.StructField) error
- func (r *Request) Method() string
- func (r *Request) RawURL() string
- type Step
- func (s Step) Do(ctx context.Context, env *OrderedMap, tmpl *template.Template, data any) (err error)
- func (s Step) Load(ctx context.Context, env *OrderedMap, tmpl *template.Template, data any) (err error)
- func (s Step) Request(ctx context.Context, env *OrderedMap, tmpl *template.Template, data any) (err error)
- func (s Step) StepName() string
- func (s Step) String() string
- type Template
- type TemplateError
- func (t *TemplateError) Add(key string, err error) *TemplateError
- func (t *TemplateError) Error() string
- func (t *TemplateError) Len() int
- func (t *TemplateError) MarshalJSON() ([]byte, error)
- func (t *TemplateError) MarshalYAML() (any, error)
- func (t *TemplateError) UnmarshalJSON(b []byte) error
- func (t *TemplateError) UnmarshalYAML(node *yaml.Node) error
- func (t *TemplateError) Unwrap() error
- func (t *TemplateError) WrapError()
- type Version
- func (v Version) MarshalJSON() ([]byte, error)
- func (v Version) MarshalText() ([]byte, error)
- func (v Version) MarshalYAML() (any, error)
- func (v *Version) Match(tx *gorm.DB) *gorm.DB
- func (v Version) String() string
- func (v *Version) UnmarshalJSON(b []byte) error
- func (v *Version) UnmarshalText(data []byte) (err error)
- func (v *Version) UnmarshalYAML(value *yaml.Node) error
Constants ¶
This section is empty.
Variables ¶
var BuiltinFuncMap = template.FuncMap{ "int": strconv.Atoi, "str": strconv.Itoa, "reg": func(pattern string, s string) (string, error) { expr, err := regexp.Compile(pattern) if err != nil { return "", err } return expr.FindString(s), nil }, "type": func(v any) string { return fmt.Sprintf("%T", v) }, "json": func(v any) (string, error) { b, err := json.Marshal(v) return string(b), err }, "gjson": func(path, json string) (any, error) { if r := gjson.Get(json, path); r.Exists() { return r.Value(), nil } return nil, fmt.Errorf("path not found: %q", path) }, "base64encode": func(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) }, "base64decode": func(s string) (string, error) { data, err := base64.StdEncoding.DecodeString(s) if err != nil { return "", err } return string(data), nil }, "plaintext": Plaintext, }
BuiltinFuncMap 内置函数
var EnvPrefix string = "$env."
EnvPrefix 环境变量前缀
var ErrEmptyVersion = errors.New("req/template: Version cannot be empty")
ErrEmptyVersion 版本号不能为空
var ErrKeyNotFound = errors.New("key not found")
var ErrNilDefaultLoader = errors.New("req/template.Load: DefaultLoader is nil")
ErrNilDefaultLoader 默认加载器为空错误
var ErrTemplateCycle = errors.New("req/template.Load: template cycle not allowed")
ErrTemplateCycle 模板引用循环
var SetPrefix string = "$set."
SetPrefix 设置变量前缀
Functions ¶
func Bind ¶
func Bind(tmpl *template.Template, setter *OrderedMap, getter ...*OrderedMap)
Bind 在模板上绑定环境变量函数
func NewTemplate ¶
func Range ¶
func Range(tmpl *template.Template, data any, ranger, setter *OrderedMap, getter ...*OrderedMap) error
Range 遍历更新模板
Types ¶
type DatabaseLoader ¶
DatabaseLoader 数据库加载器,可以查询数据库 steps 表加载模板
type OrderedMap ¶
type OrderedMap struct {
// contains filtered or unexported fields
}
OrderedMap 有序映射
func (*OrderedMap) Iterate ¶
func (o *OrderedMap) Iterate(yield func(key string, value any) bool)
Iterate 按顺序遍历键值对,返回 false 时停止迭代
func (*OrderedMap) MarshalJSON ¶
func (o *OrderedMap) MarshalJSON() ([]byte, error)
MarshalJSON 序列化 JSON
func (*OrderedMap) MarshalYAML ¶
func (o *OrderedMap) MarshalYAML() (any, error)
MarshalYAML 序列化 YAML
func (*OrderedMap) Replace ¶
func (o *OrderedMap) Replace(f func(key string, value any) (newValue any, shouldReplace bool))
Replace 替换值
func (*OrderedMap) Set ¶
func (o *OrderedMap) Set(key string, value any)
Set 设置键值对,它会检查键是否存在,存在则移动到末尾,不存在则追加到末尾,并更新值
func (*OrderedMap) UnmarshalJSON ¶
func (o *OrderedMap) UnmarshalJSON(b []byte) error
UnmarshalJSON 反序列化 JSON
func (*OrderedMap) UnmarshalYAML ¶
func (o *OrderedMap) UnmarshalYAML(node *yaml.Node) error
UnmarshalYAML 反序列化 YAML
func (*OrderedMap) Update ¶
func (o *OrderedMap) Update(n *OrderedMap) *OrderedMap
Update 更新映射,如果入参映射中的键已经存在,会将其移动到末尾,并且覆盖它的值
type Request ¶
Request 用模板、步骤和数据发送请求
type Step ¶
type Step struct {
Template `yaml:",inline"`
TemplateID uint64 `json:"-" yaml:"-"` // 模板外键
If string `json:"if" yaml:"if"` // 是否使用
Uses string `json:"uses" yaml:"uses"` // 使用模板
Method string `json:"method" yaml:"method"` // 请求方法
URL string `json:"url" yaml:"url"` // 请求地址
Body any `json:"body" yaml:"body" gorm:"serializer:json"` // 请求内容
Cookie any `json:"cookie" yaml:"cookie" gorm:"serializer:json"` // 请求 Cookie
Header map[string]any `json:"header" yaml:"header" gorm:"serializer:json"` // 请求头部
}
Step 步骤
func (Step) Do ¶
func (s Step) Do(ctx context.Context, env *OrderedMap, tmpl *template.Template, data any) (err error)
Do 执行步骤,如果步骤有 URL 则发送请求,如果有 Uses 则加载并执行子模板,否则视为起始模板
func (Step) Load ¶
func (s Step) Load(ctx context.Context, env *OrderedMap, tmpl *template.Template, data any) (err error)
Load 用步骤加载子模板
type Template ¶
type Template struct {
ID uint64 `json:"-" yaml:"-" gorm:"primaryKey;autoIncrement"` // 模板标识符
Author string `json:"author" yaml:"author" gorm:"index:idx_uses,priority:1"` // 模板作者
Name string `json:"name" yaml:"name" gorm:"index:idx_uses,priority:2"` // 模板名称
Version Version `json:"version" yaml:"version" gorm:"embedded"` // 模板版本
Desc string `json:"desc" yaml:"desc"` // 模板介绍
Steps []Step `json:"steps" yaml:"steps" gorm:"foreignkey:TemplateID"` // 模板步骤
Env *OrderedMap `json:"env" yaml:"env" gorm:"serializer:json"` // 模板环境
Output *OrderedMap `json:"output" yaml:"output" gorm:"serializer:json"` // 导出变量
}
Template 模板
type TemplateError ¶
type TemplateError OrderedMap
TemplateError 模板错误
func (*TemplateError) Add ¶
func (t *TemplateError) Add(key string, err error) *TemplateError
Add 添加错误
func (*TemplateError) MarshalJSON ¶
func (t *TemplateError) MarshalJSON() ([]byte, error)
MarshalJSON 序列化 JSON
func (*TemplateError) MarshalYAML ¶
func (t *TemplateError) MarshalYAML() (any, error)
MarshalYAML 序列化 YAML
func (*TemplateError) UnmarshalJSON ¶
func (t *TemplateError) UnmarshalJSON(b []byte) error
UnmarshalJSON 反序列化 JSON
func (*TemplateError) UnmarshalYAML ¶
func (t *TemplateError) UnmarshalYAML(node *yaml.Node) error
UnmarshalYAML 反序列化 YAML
type Version ¶
type Version struct {
// 主版本号,当你做了不兼容的 API 修改时递增
Major *uint64 `gorm:"index:idx_uses,priority:3"`
// 次版本号,当你做了向下兼容的功能性新增时递增
Minor *uint64 `gorm:"index:idx_uses,priority:4"`
// 修订号,当你做了向下兼容的问题修正时递增
Patch *uint64 `gorm:"index:idx_uses,priority:5"`
}
Version 版本号
func (*Version) UnmarshalJSON ¶
UnmarshalJSON 反序列化 JSON
func (*Version) UnmarshalText ¶
UnmarshalText 反序列化文本