fn

package
v0.8.5 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: Apache-2.0 Imports: 42 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateRealSize added in v0.0.7

func CalculateRealSize(v interface{}) int

计算真实数据总和

func CamelToSnake added in v0.1.2

func CamelToSnake(s string) string

func CheckModelValid added in v0.1.2

func CheckModelValid(model map[string]any) error

CheckModelValid 检查模型中每个键是否合法:非空且只允许英文、数字、下划线以及中文字符

func CleanMap added in v0.1.6

func CleanMap(m map[string]any) map[string]any

func CollectToMap added in v0.1.2

func CollectToMap[T any, K comparable, V any](
	in []T,
	keyOf func(T) K,
	valOf func(T) V,
) map[K]V

func CompileExpr added in v0.1.9

func CompileExpr[E any](code string) (*vm.Program, error)

func ConvertByJSON added in v0.0.7

func ConvertByJSON[A any, B any](input A) (B, error)

ConvertByJSON Convert 转换任意类型 A 到类型 B

func DeRef added in v0.1.2

func DeRef(row []interface{}) []interface{}

func DefaultTemplateRender added in v0.1.2

func DefaultTemplateRender(inputText string, sourceMap map[string]any, result *string) error

func DownloadToTempDir

func DownloadToTempDir(url string) (*os.File, error)

func Dtr added in v0.1.9

func Dtr(row interface{}) interface{}

func Eval added in v0.1.9

func Eval[E any](code string, env E) (float64, error)

func EvalWithTrace added in v0.1.9

func EvalWithTrace[E any](code string, env E) (float64, string, error)

EvalWithTrace returns the numeric result and the expression string used for compilation. (Using expr v1 API; no AST dump available here.)

func FileExists

func FileExists(filePath string) bool

func Filter added in v0.0.7

func Filter[T any](endpoints []T, f func(endpoint T) bool) []T

func Find added in v0.1.2

func Find[T any](list []T, predicate func(T) bool) (T, bool)

func FlatMap added in v0.1.6

func FlatMap[T any, R any](in []T, f func(T) []R) []R

FlatMap 把每个元素映射成一个 slice,然后展开合并成一个 slice。

func GenerateShortID added in v0.1.2

func GenerateShortID() (string, error)

func GenerateShortIDWithDigits added in v0.1.2

func GenerateShortIDWithDigits(digits uint8) (string, error)

func GetByPath added in v0.1.2

func GetByPath(data any, path []any) any

GetByPath 从嵌套结构中通过解析后的路径获取值

func GetProgram added in v0.1.9

func GetProgram[E any](code string) (*cachedProgram, error)

func GetSuffix added in v0.1.2

func GetSuffix(str string) string

func GetValue added in v0.1.2

func GetValue(data map[string]any, expr string) any

GetValue 从 map 中通过路径表达式获取值(支持中文、嵌套、数组)

func GroupBy added in v0.1.6

func GroupBy[T any, K comparable](items []T, keyFn func(T) K) map[K][]T

func GzipCompress

func GzipCompress(input []byte) []byte

func GzipDecompress

func GzipDecompress(input []byte) []byte

func HashPassword added in v0.1.2

func HashPassword(password string) (string, error)

HashPassword 使用 bcrypt 对密码进行哈希处理

func InLike added in v0.1.2

func InLike(target any, list any) bool

func InSlice added in v0.1.2

func InSlice(target any, list any) bool

func InitSnowflake added in v0.1.2

func InitSnowflake(nodeID int64) (*snowflake.Node, error)

InitSnowflake 初始化 Snowflake 节点

func Int added in v0.1.9

func Int(p *int) int

func IsDataEmpty added in v0.1.2

func IsDataEmpty(v any) bool

func IsEmpty

func IsEmpty(s string) bool

func MakeStructExtractor added in v0.1.6

func MakeStructExtractor[T any](keys []string) func(T) []interface{}

MakeStructExtractor 预编译一个从 struct T 按 keys 提取值的函数(按 json tag 或字段名匹配,支持匿名/嵌套)

func MapKeys added in v0.1.2

func MapKeys[M ~map[K]V, K comparable, V any](m M) []string

func MapNotNull added in v0.1.6

func MapNotNull[T any, R any](in []T, f func(T) *R) []R

func MapToJSON added in v0.1.6

func MapToJSON(m map[string]any) (datatypes.JSON, error)

func MaxMapInt added in v0.1.9

func MaxMapInt(m map[string]int) (max int, ok bool)

MaxMapInt 返回 map 中的最大 value ok=false 表示 map 为空

func MustJSON added in v0.1.2

func MustJSON(v any) datatypes.JSON

func NumericStringify added in v0.1.2

func NumericStringify(v any) string

func ParseList added in v0.1.2

func ParseList(v any) []string

func ParsePathExpr added in v0.1.2

func ParsePathExpr(expr string) []any

ParsePathExpr 将路径字符串(如 用户.好友[0].昵称)解析为 ["用户", "好友", 0, "昵称"]

func ParseSmartTime added in v0.1.7

func ParseSmartTime(v any) (time.Time, error)

ParseSmartTime 自动识别 string / number / timestamp 转换为 time.Time

func ParseTemplate added in v0.1.2

func ParseTemplate(templateText string) (map[string]TemplateNeedle, error)

ParseTemplate 从模板文本中提取所有占位符,返回键值对:键为参数名称,值为占位符文本

func ParseTemplateTest added in v0.1.2

func ParseTemplateTest()

func Ptr added in v0.1.2

func Ptr[T any](v T) *T

func ReadStringField added in v0.1.6

func ReadStringField(m map[string]any, key string) string

func RegexReplace added in v0.1.2

func RegexReplace(s, pattern, repl string) string

RegexReplace 用正则表达式将字符串 s 中所有匹配 pattern 的部分替换为 repl

func RenderControlNodes added in v0.1.7

func RenderControlNodes(nodes []Node, model map[string]any) string

func RenderTemplateStrictly added in v0.1.2

func RenderTemplateStrictly(templateText string, slots map[string]TemplateNeedle, model map[string]any, strict bool) string

RenderTemplateStrictly 根据传入的模型,将模板中的占位符替换为实际值

func RenderTemplateWithControl added in v0.1.7

func RenderTemplateWithControl(input string, model map[string]any) (string, error)

func RunParallel added in v0.1.2

func RunParallel[I any, O any](
	ctx context.Context,
	inputs []I,
	workers int,
	rowId func(I) string,
	function func(context.Context, I) (O, error),
) (map[string]O, []error)

RunParallel 并发执行 fn,按输入顺序返回结果。 - inputs: 要处理的输入切片 - workers: 工作者数量(<=0 视为 1) - fn: 你的任务函数,形如 func(ctx, I) (O, error)

func SaveToWAVFile

func SaveToWAVFile(filename string, data []byte) (*os.File, error)

func ScryptHash added in v0.1.2

func ScryptHash(password, salt string) (string, error)

func SendFileChunks

func SendFileChunks(file *os.File, firstSize int, chunkSize int, ch chan []byte)

func SeqToSlice added in v0.1.6

func SeqToSlice[V any](seq iter.Seq[V]) []V

func SimplifyHeader added in v0.0.7

func SimplifyHeader(header map[string][]string) map[string]string

SimplifyHeader 压缩 Header

func StreamMap added in v0.1.2

func StreamMap[T any, R any](in []T, f func(T) R) []R

func StreamMapDistinct added in v0.1.6

func StreamMapDistinct[T any, R comparable](in []T, fn func(T) R) []R

func StreamMapDistinctBy added in v0.1.9

func StreamMapDistinctBy[T any, K comparable](in []T, keyFn func(T) K) []T

StreamMapDistinctBy 按 keyFn 去重,返回原始元素,保持顺序

func Stringify added in v0.1.2

func Stringify[A any](input A) string

func SumBy added in v0.1.6

func SumBy[T any](items []T, addFn func(dst *T, src T)) T

func SumMapBy added in v0.1.6

func SumMapBy[T any, K comparable](records []T, keyFn func(T) K, addFn func(dst *T, src T)) map[K]T

func Ternary

func Ternary[T any](cond bool, trueVal T, falseVal T) T

Ternary 三元表达式

func TernaryFunc added in v0.1.2

func TernaryFunc[T any](cond bool, trueVal func() T, falseVal T) T

func TimingMiddlewareLogging added in v0.1.2

func TimingMiddlewareLogging(phase string, log string) func()

func ToFloat64 added in v0.1.2

func ToFloat64(v any) (float64, bool)

func Unique added in v0.1.7

func Unique[T comparable](arr []T) []T

func UniqueList added in v0.1.2

func UniqueList(items []string) []string

func UniqueStrings added in v0.1.2

func UniqueStrings(input []string) []string

func Uuid added in v0.1.2

func Uuid() string

Types

type DebounceManager added in v0.1.7

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

func NewDebounceManager added in v0.1.7

func NewDebounceManager(delay time.Duration) *DebounceManager

func (*DebounceManager) Debounce added in v0.1.7

func (m *DebounceManager) Debounce(taskID uint64, fn func(uint64))

type EchoNode added in v0.1.7

type EchoNode struct {
	Expr string
}

type Event added in v0.1.7

type Event[T any] struct {
	ID   int64
	Data T
}

Event holds an ID and data of type T.

type EventBus added in v0.1.7

type EventBus[T any] struct {
	// contains filtered or unexported fields
}

EventBus manages multiple topics for type T.

func NewBus added in v0.1.7

func NewBus[T any]() *EventBus[T]

NewBus creates a new EventBus.

func (*EventBus[T]) CreateTopic added in v0.1.7

func (b *EventBus[T]) CreateTopic(name string, maxCache int) (*Topic[T], error)

CreateTopic creates a topic and returns it.

func (*EventBus[T]) GetTopic added in v0.1.7

func (b *EventBus[T]) GetTopic(name string) *Topic[T]

func (*EventBus[T]) Publish added in v0.1.7

func (b *EventBus[T]) Publish(topic string, data T) error

Publish sends data to a named topic.

func (*EventBus[T]) Subscribe added in v0.1.7

func (b *EventBus[T]) Subscribe(topic string, buffer int) (*Subscriber[T], error)

Subscribe returns a subscriber to a topic.

type FlexibleFloat64 added in v0.1.2

type FlexibleFloat64 float64

func (*FlexibleFloat64) UnmarshalJSON added in v0.1.2

func (f *FlexibleFloat64) UnmarshalJSON(b []byte) error

type FlexibleInt8 added in v0.1.2

type FlexibleInt8 int8

func (*FlexibleInt8) UnmarshalJSON added in v0.1.2

func (f *FlexibleInt8) UnmarshalJSON(b []byte) error

type FlexibleInt64 added in v0.1.2

type FlexibleInt64 int64

func (*FlexibleInt64) UnmarshalJSON added in v0.1.2

func (f *FlexibleInt64) UnmarshalJSON(b []byte) error

type FlexibleString added in v0.1.6

type FlexibleString string

func (FlexibleString) String added in v0.1.6

func (s FlexibleString) String() string

转回 string 用

func (*FlexibleString) UnmarshalJSON added in v0.1.6

func (s *FlexibleString) UnmarshalJSON(b []byte) error

type HandlerFunc added in v0.1.2

type HandlerFunc[T any] func(items []T)

type IfNode added in v0.1.7

type IfNode struct {
	Conds  []string // if + elseif 共用
	Blocks [][]Node // 每个条件对应一个 block
	Else   []Node   // else 内容
}

type IntTuple added in v0.1.6

type IntTuple [2]int

func (*IntTuple) Scan added in v0.1.6

func (p *IntTuple) Scan(value interface{}) error

func (IntTuple) Value added in v0.1.6

func (p IntTuple) Value() (driver.Value, error)

type KeyFunc added in v0.1.2

type KeyFunc[T any] func(item T) string

type LRUCache added in v0.1.2

type LRUCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LRUCache 是一个并发安全的泛型 LRU 缓存

func NewLRUCache added in v0.1.2

func NewLRUCache[K comparable, V any](cap int) *LRUCache[K, V]

NewLRUCache 新建一个最大容量为 cap 的 LRUCache

func (*LRUCache[K, V]) Get added in v0.1.2

func (c *LRUCache[K, V]) Get(key K) (V, bool)

Get 取值;存在时把该条目移到队首(最近使用),返回 true;否则返回 false

func (*LRUCache[K, V]) Len added in v0.1.2

func (c *LRUCache[K, V]) Len() int

Len 返回当前缓存大小

func (*LRUCache[K, V]) Put added in v0.1.2

func (c *LRUCache[K, V]) Put(key K, value V)

Put 插入或更新:如果已存在,更新值并移到队首;否则插入新条目,超出容量时驱逐最旧的

func (*LRUCache[K, V]) Trash added in v0.1.2

func (c *LRUCache[K, V]) Trash(key K)

Trash 删除

type Node added in v0.1.7

type Node interface{}

-------- 控制流 AST --------

func ParseControlBlocks added in v0.1.7

func ParseControlBlocks(input string) ([]Node, error)

type Option added in v0.1.2

type Option[K comparable, V any] func(*LRUCache[K, V])

type PageQueryFunc added in v0.1.2

type PageQueryFunc[T any] func(ctx context.Context, pageNum, pageSize int) ([]T, error)

type Pair added in v0.1.2

type Pair[O any] struct {
	Out O
	Err error
}

type SafeHashSet added in v0.1.2

type SafeHashSet[T comparable] struct {
	// contains filtered or unexported fields
}

func NewSafeHashSet added in v0.1.2

func NewSafeHashSet[T comparable]() *SafeHashSet[T]

func ScanAllPages added in v0.1.2

func ScanAllPages[T any](
	ctx context.Context,
	pageSize int,
	query PageQueryFunc[T],
	keyFunc KeyFunc[T],
	handle HandlerFunc[T],
) (*SafeHashSet[string], error)

func (*SafeHashSet[T]) Add added in v0.1.2

func (s *SafeHashSet[T]) Add(val T)

func (*SafeHashSet[T]) Has added in v0.1.2

func (s *SafeHashSet[T]) Has(val T) bool

func (*SafeHashSet[T]) Len added in v0.1.2

func (s *SafeHashSet[T]) Len() int

func (*SafeHashSet[T]) Remove added in v0.1.2

func (s *SafeHashSet[T]) Remove(val T)

func (*SafeHashSet[T]) Values added in v0.1.2

func (s *SafeHashSet[T]) Values() []T

type SmartTime added in v0.1.7

type SmartTime struct {
	time.Time
}

SmartTime 可识别多种时间格式,统一序列化输出格式

func (SmartTime) MarshalJSON added in v0.1.7

func (t SmartTime) MarshalJSON() ([]byte, error)

MarshalJSON 统一输出格式

func (*SmartTime) UnmarshalJSON added in v0.1.7

func (t *SmartTime) UnmarshalJSON(b []byte) error

UnmarshalJSON 可自动识别多种时间格式

type StringSlice added in v0.1.6

type StringSlice []string

StringSlice 就是一个可以自动 JSON ↔ Go 切片的类型

func (*StringSlice) Scan added in v0.1.6

func (s *StringSlice) Scan(src any) error

Scan 把数据库中读出来的 JSON 反序列化到 StringSlice

func (StringSlice) ToStringSlice added in v0.1.6

func (s StringSlice) ToStringSlice() []string

func (StringSlice) Value added in v0.1.6

func (s StringSlice) Value() (driver.Value, error)

Value 把 Go 切片序列化成 JSON,写回数据库

type Subscriber added in v0.1.7

type Subscriber[T any] struct {
	ID     int64
	Ch     chan Event[T]
	Cursor int64
}

Subscriber receives events of type T.

type TemplateNeedle added in v0.1.2

type TemplateNeedle struct {
	Template     string `json:"template"`
	DefaultValue string `json:"defaultValue"`
}

type TextNode added in v0.1.7

type TextNode struct {
	Text string
}

type Token added in v0.1.7

type Token struct {
	Kind string // text | if | else | end
	Val  string
}

type Topic added in v0.1.7

type Topic[T any] struct {
	// contains filtered or unexported fields
}

Topic manages subscriptions and publishing for type T.

func NewTopic added in v0.1.7

func NewTopic[T any](name string, maxCache int) *Topic[T]

NewTopic creates a new Topic.

func (*Topic[T]) DroppedCount added in v0.1.7

func (t *Topic[T]) DroppedCount() int64

func (*Topic[T]) Publish added in v0.1.7

func (t *Topic[T]) Publish(data T) error

Publish sends data to all subscribers and stores it in history.

func (*Topic[T]) Snapshot added in v0.1.7

func (t *Topic[T]) Snapshot() []Event[T]

Snapshot returns a copy of history.

func (*Topic[T]) Subscribe added in v0.1.7

func (t *Topic[T]) Subscribe(buffer int) *Subscriber[T]

Subscribe adds a new subscriber with buffer size.

func (*Topic[T]) Unsubscribe added in v0.1.7

func (t *Topic[T]) Unsubscribe(subID int64)

Unsubscribe removes subscriber and closes channel.

Jump to

Keyboard shortcuts

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