utils

package
v1.0.1-0...-65d85cd Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CatchAsError

func CatchAsError(err *error)

func ChainFuncs

func ChainFuncs[P func(H) H, H any](h H, ps ...P) H

搁这套娃🪆🪆🪆? P:Prototype

func Delete

func Delete(fsys fs.FS, name string) error

func DropLast1

func DropLast1[First any, Last any](f First, l Last) First

func Filter

func Filter[S []E, E any](s S, predicate func(e E) bool) []E

func IIF

func IIF[Any any](cond bool, first, second Any) Any

Go 语言多少有点儿大病,以至于我需要写这种东西。 是谁当初说不需要三元运算符的?我打断他的 🐶 腿。 https://en.wikipedia.org/wiki/IIf https://blog.twofei.com/716/#没有条件运算符

func Implements

func Implements[T any](a any) bool

func IsEmail

func IsEmail(email string) bool

func IsURL

func IsURL(Url string, addScheme bool) bool

func JoinInts

func JoinInts(ints []int64, delim string) string

func ListBackupFiles

func ListBackupFiles(fsys fs.FS, dir string) ([]*proto.BackupFileSpec, error)

Deprecated. 用 ListFiles。

func ListFiles

func ListFiles(fsys fs.FS, dir string) ([]*proto.FileSpec, error)

walk returns the file list for a directory. Directories are omitted. Returned paths are related to dir. 返回的所有路径都是相对于 dir 而言的。

func Map

func Map[T any, S []E, E any](s S, mapper func(e E) T) []T

func MemDupReader

func MemDupReader(r io.Reader) func() io.Reader

基于内存实现的可重复读的 Reader。 https://blog.twofei.com/1072/

func Must

func Must(e error)

func Must1

func Must1[A any](a A, e error) A

func MustToInt64

func MustToInt64(s string) int64

func NewDirFSWithNotify

func NewDirFSWithNotify(root string) fs.FS

func NewOverlayFS

func NewOverlayFS(layers ...fs.FS) fs.FS

func RandomString

func RandomString() string

func ReInitTestRandomSeed

func ReInitTestRandomSeed()

func ToInt64

func ToInt64(s string) (int64, error)

func Write

func Write(fsys fs.FS, spec *proto.FileSpec, r io.Reader) error

func WriteFile

func WriteFile(fs afero.Fs, path string, mode fs.FileMode, modified time.Time, size int64, r io.Reader) error

安全写文件。 TODO 不应该引入专用的 FileSpec 定义。 path 中包含的目录必须存在,否则失败。 TODO 没移除失败的文件。 NOTE:安全写:先写临时文件,再移动过去。临时文件写在目标目录,不存在跨设备移动文件的问题。 NOTE:如果 r 超过 size,会报错。

Types

type BatchDebouncer

type BatchDebouncer[K comparable] struct {
	// contains filtered or unexported fields
}

func NewBatchDebouncer

func NewBatchDebouncer[K comparable](interval time.Duration, fn func(k K)) *BatchDebouncer[K]

func (*BatchDebouncer[K]) Enter

func (b *BatchDebouncer[K]) Enter(k K)

type Debouncer

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

func NewDebouncer

func NewDebouncer(interval time.Duration, fn func()) *Debouncer

防抖神器。

仅在动作稳定一定时间后才执行。

比如:我需要在模板文件更新后刷新模板,但是切分支的时候会有收到大量文件修改通知, 此时不能每收到一个文件就触发一次刷新,需要在“稳定”(分支切换完全后、几秒内没有文件再有修改) 的情况下方可刷新。可避免掉大量无意义的刷新。

NOTE:回调函数 fn 是在独立的线程中被调用的。

func (*Debouncer) Enter

func (d *Debouncer) Enter()

type DeleteFS

type DeleteFS interface {
	fs.FS
	Delete(name string) error
}

作为对 fs.FS 的补充。 官方标准化了我就删。

type DirFSWithNotify

type DirFSWithNotify struct {
	fs.FS
	// contains filtered or unexported fields
}

func (*DirFSWithNotify) Changed

func (l *DirFSWithNotify) Changed() <-chan fsnotify.Event

type FsWithChangeNotify

type FsWithChangeNotify interface {
	fs.FS
	Changed() <-chan fsnotify.Event
}

type Maintenance

type Maintenance struct {
	Message   string
	Estimated time.Duration
	// contains filtered or unexported fields
}

func NewMaintenance

func NewMaintenance() *Maintenance

func (*Maintenance) Enter

func (m *Maintenance) Enter(message string, estimated time.Duration)

func (*Maintenance) EstimatedString

func (m *Maintenance) EstimatedString() string

func (*Maintenance) Handler

func (m *Maintenance) Handler(exception func(ctx context.Context) bool) func(http.Handler) http.Handler

func (*Maintenance) Leave

func (m *Maintenance) Leave()

func (*Maintenance) MessageString

func (m *Maintenance) MessageString() string

type PluginStorage

type PluginStorage interface {
	Set(key string, value string) error
	Get(key string) (string, error)
}

type ServeMuxChain

type ServeMuxChain struct {
	*http.ServeMux
}

func (*ServeMuxChain) Handle

func (m *ServeMuxChain) Handle(pattern string, handler http.Handler, middlewares ...func(http.Handler) http.Handler)

type TemplateLoader

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

func NewTemplateLoader

func NewTemplateLoader(fsys fs.FS, funcs template.FuncMap, refreshed func()) *TemplateLoader

refreshed: 初次加载不会调用,后面刷新调用。

func (*TemplateLoader) GetNamed

func (l *TemplateLoader) GetNamed(name string) *template.Template

func (*TemplateLoader) GetPartial

func (l *TemplateLoader) GetPartial(name string) *template.Template

type Throttler

type Throttler[Key comparable] struct {
	// contains filtered or unexported fields
}

func NewThrottler

func NewThrottler[Key comparable]() *Throttler[Key]

节流神器。

仅在动作完成一定时间后才允许再度执行。 基于时间,而不是漏桶🪣或令牌。

比如:十秒钟内只允许评论一次。

func (*Throttler[Key]) Throttled

func (t *Throttler[Key]) Throttled(key Key, interval time.Duration, update bool) bool

检测并更新

func (*Throttler[Key]) Update

func (t *Throttler[Key]) Update(key Key, interval time.Duration)

type WriteFS

type WriteFS interface {
	Write(spec *proto.FileSpec, r io.Reader) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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