template

package module
v0.0.0-...-f092874 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2025 License: MIT Imports: 20 Imported by: 0

README

template

利用模板发送网络请求

怎么用

参考 example 文件夹下的例子

看不懂

我又幻想了,都没有人会来用,何谈看不懂?如果你真的想用,请随意发 issues

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyInput     = errors.New("template: empty input")
	ErrNotString      = errors.New("template: version must be a string")
	ErrInvalidFormat  = errors.New("template: invalid version format: expected vX.Y.Z")
	ErrEmptyComponent = errors.New("template: empty version component")
)
View Source
var BuiltinFuncMap = template.FuncMap{
	"json": func(v any) (string, error) {
		b, err := json.Marshal(v)
		return string(b), err
	},
	"gjson": func(json, path string) any {
		return gjson.Get(json, path).Value()
	},
	"gjson2": func(json, path string) (any, error) {
		if r := gjson.Get(json, path); r.Exists() {
			return r.Value(), nil
		}
		return nil, fmt.Errorf("path not found: \"%s\"", 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,
}

内置函数

View Source
var ErrEmptyURL = errors.New("template: step URL is empty")
View Source
var ErrInvalidDecoder = errors.New("template: invalid Decoder")

Functions

func GoodName

func GoodName(name string) bool

copy from text/template.goodName

func Plaintext

func Plaintext(text string) (string, error)

获取纯净文本

func ToBuffer

func ToBuffer(tmpl *template.Template, text string, data any) (*bytes.Buffer, error)

模板转缓冲器

func ToString

func ToString(tmpl *template.Template, text string, data any) (string, error)

模板转文本

func Unmarshal

func Unmarshal(uses string, tmpl *Template) error

递归解析子模板

Types

type DatabaseDecoder

type DatabaseDecoder struct {
	*gorm.DB
}

数据库解码器 可以从数据库 steps 表中解析模板

func (*DatabaseDecoder) UnmarshalTemplate

func (d *DatabaseDecoder) UnmarshalTemplate(uses string, tmpl *Template) error

type Env

type Env map[string]any

环境

func AppendEnv

func AppendEnv(e Env, elems ...Env) Env

附加环境

func ToEnv

func ToEnv(tmpl *template.Template, text string, data any, env Env) (out Env, err error)

模板转变量

func (Env) Get

func (e Env) Get(key string) func() any

func (Env) Set

func (e Env) Set(tmpl *template.Template, prefix string) error

保存环境变量

type FileDecoder

type FileDecoder struct{}

文件解码器 可以从 JSON 和 YAML 文件中解析模板

func (FileDecoder) UnmarshalTemplate

func (FileDecoder) UnmarshalTemplate(uses string, tmpl *Template) error

type SafeTemplate

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

并发安全模板

func NewSafeTemplate

func NewSafeTemplate(v any) *SafeTemplate

func (*SafeTemplate) Do

func (st *SafeTemplate) Do(rt *Template) (out Env, result []byte, err error)

运行模板

type Step

type Step struct {
	Template   `yaml:",inline"`
	TemplateID uint64 `json:"-"       yaml:"-"`                             // 模板外键
	Skip       string `json:"skip"    yaml:"skip"`                          // 跳过步骤
	Uses       string `json:"uses"    yaml:"uses"`                          // 使用模板
	Method     string `json:"method"  yaml:"method"`                        // 请求方法
	URL        string `json:"url"     yaml:"url"`                           // 请求地址
	Body       string `json:"body"    yaml:"body"`                          // 请求内容
	Header     Env    `json:"header"  yaml:"header" gorm:"serializer:json"` // 请求头部
	Set        Env    `json:"set"     yaml:"set"    gorm:"serializer:json"` // 设置环境变量
	Out        Env    `json:"out"     yaml:"out"    gorm:"serializer:json"` // 导出环境变量
}

步骤

func (*Step) Request

func (s *Step) Request(tmpl *template.Template, data any) (*http.Request, error)

步骤转请求

type Template

type Template struct {
	ID          uint64  `json:"-"           yaml:"-"         gorm:"primaryKey;autoIncrement"`  // 模板标识符
	Description string  `json:"description" yaml:"description"`                                // 模板介绍
	Author      string  `json:"author"      yaml:"author"    gorm:"index:idx_uses,priority:1"` // 模板作者
	Namespace   string  `json:"namespace"   yaml:"namespace" gorm:"index:idx_uses,priority:2"` // 模板命名
	Version     Version `json:"version"     yaml:"version"   gorm:"embedded"`                  // 模板版本
	Env         Env     `json:"env"         yaml:"env"       gorm:"serializer:json"`           // 模板环境
	Steps       []Step  `json:"steps"       yaml:"steps"     gorm:"foreignkey:TemplateID"`     // 模板步骤
}

模板

func (*Template) Do

func (t *Template) Do(tmpl *template.Template, data any) (out Env, result []byte, err error)

执行模板

func (Template) Index

func (t Template) Index() string

func (Template) String

func (t Template) String() string

func (Template) StringIndent

func (t Template) StringIndent(indent string) string

func (*Template) Unmarshal

func (t *Template) Unmarshal(uses string) error

type Unmarshaler

type Unmarshaler interface {
	UnmarshalTemplate(uses string, tmpl *Template) error
}
var Decoder Unmarshaler

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"`
}

版本号

func (Version) Equal

func (v Version) Equal(other Version) bool

func (Version) IsZero

func (v Version) IsZero() bool

func (Version) Less

func (v Version) Less(other Version) bool

func (Version) MarshalJSON

func (v Version) MarshalJSON() ([]byte, error)

func (Version) MarshalText

func (v Version) MarshalText() ([]byte, error)

func (Version) MarshalYAML

func (v Version) MarshalYAML() (any, error)

func (Version) String

func (v Version) String() string

func (*Version) UnmarshalJSON

func (v *Version) UnmarshalJSON(b []byte) error

func (*Version) UnmarshalText

func (v *Version) UnmarshalText(data []byte) error

func (*Version) UnmarshalYAML

func (v *Version) UnmarshalYAML(value *yaml.Node) error

Directories

Path Synopsis
cmd
template command

Jump to

Keyboard shortcuts

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