Documentation
¶
Overview ¶
* @Author: kamalyes 501893067@qq.com * @Date: 2026-07-30 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-07-30 00:00:00 * @FilePath: \go-toolbox\pkg\sqlbuilder\types\compressed.go * @Description: 数据库压缩存储类型——字段级透明 gzip 压缩,多 DB 方言适配 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-05-08 15:00:00 * @FilePath: \go-toolbox\pkg\sqlbuilder\types\helpers.go * @Description: 内部辅助函数 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-05-08 15:05:00 * @FilePath: \go-toolbox\pkg\sqlbuilder\types\json.go * @Description: JSON - 泛型 JSON 类型,支持任意可序列化类型的数据库存储 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-05-08 15:15:19 * @FilePath: \go-toolbox\pkg\sqlbuilder\types\map_any.go * @Description: MapAny - 任意类型 Map,支持数据库 JSON 序列化 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2026-07-25 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-07-25 00:00:00 * @FilePath: \go-toolbox\pkg\sqlbuilder\types\optional.go * @Description: Optional[T] - 通用可选值容器,支持 nil-safe 链式操作 * * Copyright (c) 2026 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2026-05-09 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-05-11 12:57:50 * @FilePath: \go-toolbox\pkg\sqlbuilder\types\proto_json.go * @Description: ProtoJSON - 泛型 protobuf JSON 类型,支持 protobuf 消息的数据库存储 * * Copyright (c) 2026 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-05-08 15:00:00 * @FilePath: \go-toolbox\pkg\sqlbuilder\types\slice.go * @Description: Slice - 泛型切片类型,支持数据库 JSON 序列化 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2025-11-11 00:00:00 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2026-05-08 15:00:00 * @FilePath: \go-toolbox\pkg\sqlbuilder\types\string_slice.go * @Description: StringSlice - 字符串切片,支持数据库 JSON 序列化和分隔符解析 * * Copyright (c) 2025 by kamalyes, All Rights Reserved.
Index ¶
- func MapAnyToJSONString(payload MapAny) string
- func MapAnyToStruct(payload MapAny) *structpb.Struct
- func MapAnyToStructTarget(m MapAny, target interface{}) error
- func MapJSON[T any, U any](j JSON[T], fn func(T) *U) *U
- func MapPtr[T any, U any](ptr *T, fn func(*T) *U) *U
- func Ptr[T any](v T) *T
- func StructToMapString(payload *structpb.Struct) map[string]string
- type CompressedText
- type CompressedTextMap
- type JSON
- func (j *JSON[T]) Append(items ...any)
- func (j JSON[T]) Clone() JSON[T]
- func (j *JSON[T]) Get() T
- func (j JSON[T]) GetOrDefault(defaultValue T) T
- func (j JSON[T]) GetOrZero() T
- func (j JSON[T]) IfPresent(fn func(T))
- func (j JSON[T]) IsEmpty() bool
- func (j JSON[T]) IsNil() bool
- func (j JSON[T]) IsPresent() bool
- func (j JSON[T]) Len() int
- func (j *JSON[T]) Scan(value interface{}) error
- func (j *JSON[T]) Set(data T)
- func (j JSON[T]) ToOptional() Optional[T]
- func (j JSON[T]) Value() (driver.Value, error)
- type MapAny
- func (m MapAny) Clone() MapAny
- func (m MapAny) Delete(key string) MapAny
- func (m MapAny) Each(fn func(key string, value any))
- func (m MapAny) Filter(fn func(key string, value any) bool) MapAny
- func (m MapAny) Get(key string, defaultValue ...any) any
- func (m MapAny) GetBool(key string, defaultValue ...bool) bool
- func (m MapAny) GetInt(key string, defaultValue ...int) int
- func (m MapAny) GetInt32(key string, defaultValue ...int32) int32
- func (m MapAny) GetMap(key string, defaultValue ...MapAny) MapAny
- func (m MapAny) GetString(key string, defaultValue ...string) string
- func (m MapAny) Has(key string) bool
- func (m MapAny) IsEmpty() bool
- func (m MapAny) Keys() []string
- func (m MapAny) Len() int
- func (m MapAny) Map(fn func(key string, value any) (string, any)) MapAny
- func (m MapAny) Merge(other MapAny) MapAny
- func (m *MapAny) Scan(value interface{}) error
- func (m MapAny) Set(key string, value any) MapAny
- func (m MapAny) Value() (driver.Value, error)
- func (m MapAny) Values() []any
- type Optional
- func (o Optional[T]) Filter(pred func(T) bool) Optional[T]
- func (o Optional[T]) Get() *T
- func (o Optional[T]) GetOrDefault(defaultValue T) T
- func (o Optional[T]) GetOrZero() T
- func (o Optional[T]) IfPresent(fn func(T))
- func (o Optional[T]) IfPresentOrElse(action func(T), emptyAction func())
- func (o Optional[T]) IsNil() bool
- func (o Optional[T]) IsPresent() bool
- func (o Optional[T]) OrElse(defaultValue T) T
- type ProtoJSON
- type ProtoJSONMap
- func (p *ProtoJSONMap[T]) Clear()
- func (p *ProtoJSONMap[T]) Clone() ProtoJSONMap[T]
- func (p *ProtoJSONMap[T]) Delete(key string)
- func (p *ProtoJSONMap[T]) Get(key string) T
- func (p *ProtoJSONMap[T]) Has(key string) bool
- func (p *ProtoJSONMap[T]) IsEmpty() bool
- func (p *ProtoJSONMap[T]) Keys() []string
- func (p *ProtoJSONMap[T]) Len() int
- func (p *ProtoJSONMap[T]) Scan(value interface{}) error
- func (p *ProtoJSONMap[T]) Set(key string, data T)
- func (p ProtoJSONMap[T]) Value() (driver.Value, error)
- type Slice
- type StringSlice
- func (s *StringSlice) Append(items ...string) StringSlice
- func (s StringSlice) Clone() StringSlice
- func (s StringSlice) Contains(item string) bool
- func (s StringSlice) Filter(fn func(string) bool) StringSlice
- func (s *StringSlice) FromDelimitedString(input string, delimiters ...string) StringSlice
- func (s StringSlice) IndexOf(item string) int
- func (s StringSlice) Join(sep string) string
- func (s StringSlice) Map(fn func(string) string) StringSlice
- func (s *StringSlice) Remove(item string) StringSlice
- func (s *StringSlice) RemoveAt(index int) StringSlice
- func (s *StringSlice) Scan(value interface{}) error
- func (s StringSlice) Unique() StringSlice
- func (s StringSlice) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MapAnyToJSONString ¶
MapAnyToJSONString 将 MapAny 转换为 JSON 字符串
func MapAnyToStruct ¶
MapAnyToStruct 将 MapAny 转换为 structpb.Struct
func MapAnyToStructTarget ¶
MapAnyToStructTarget 将 MapAny 转换为 Go struct target 必须是指针类型
func MapJSON ¶
MapJSON 当 Data 非 nil 时应用转换函数,返回 *U Data 为 nil 时返回 nil;fn 内部可返回 nil 主动中断链路 适用于链式访问嵌套指针字段,配合 MapPtr 实现深层取值 (命名为 MapJSON 是为避免与 slice.go 中 Slice 的 Map 函数冲突)
示例(单层):
fb := types.MapJSON(agentLine.Tracking, func(t *TrackingConfig) *FacebookConfig {
return t.Facebook
})
if fb != nil {
params.TrackID = fb.PixelId
}
注:Go 方法不支持额外类型参数,因此以泛型函数形式提供
func MapPtr ¶
MapPtr 对普通指针 *T 应用转换函数,返回 *U ptr 为 nil 时返回 nil;fn 接收 *T(指针)便于调用指针接收者方法 fn 内部可返回 nil 主动中断链路 用于在 MapJSON 之后继续链式穿透嵌套指针字段
示例(深层链式:Tracking → Facebook → PixelId):
fb := types.MapJSON(agentLine.Tracking, func(t *TrackingConfig) *FacebookConfig {
return t.Facebook
})
pixel := types.MapPtr(fb, func(f *FacebookConfig) *string {
return types.Ptr(f.PixelId)
})
if pixel != nil {
params.TrackID = *pixel
}
Types ¶
type CompressedText ¶
type CompressedText string
func (CompressedText) GormDBDataType ¶
GormDBDataType 按方言返回列类型:ClickHouse→String,MySQL→LONGTEXT,其他→TEXT
func (CompressedText) GormDataType ¶
func (CompressedText) GormDataType() string
GormDataType 实现 schema.GormDataTypeInterface:声明逻辑数据类型为 string 必须实现:否则 gorm schema 解析时 DataType 为空,字段会被误判为关联关系(parseRelation 报错)
func (*CompressedText) Scan ¶
func (c *CompressedText) Scan(value interface{}) error
Scan 实现 sql.Scanner:透明解压(兼容未压缩历史数据)
type CompressedTextMap ¶
func CompressedTextMapFromMap ¶
func CompressedTextMapFromMap(m map[string][]string) CompressedTextMap
FromMap 从原生 map[string][]string 创建(零拷贝)
func (CompressedTextMap) GormDBDataType ¶
GormDBDataType 按方言返回列类型:全部返回 TEXT/String(map 压缩为 JSON 字符串存储)
func (CompressedTextMap) GormDataType ¶
func (CompressedTextMap) GormDataType() string
GormDataType 实现 schema.GormDataTypeInterface:声明逻辑数据类型为 string 必须实现:Map 底层类型无法被 gorm 推导 DataType,空 DataType 会被误判为关联关系导致解析失败
func (*CompressedTextMap) Scan ¶
func (c *CompressedTextMap) Scan(value interface{}) error
Scan 实现 sql.Scanner:透明解压后 JSON 反序列化
func (CompressedTextMap) ToMap ¶
func (c CompressedTextMap) ToMap() map[string][]string
ToMap 转为原生 map[string][]string(零拷贝,底层类型相同)
type JSON ¶
type JSON[T any] struct { Data T }
JSON 泛型 JSON 类型,支持任意可序列化类型
func (JSON[T]) GetOrDefault ¶
func (j JSON[T]) GetOrDefault(defaultValue T) T
GetOrDefault 获取数据,如果 Data 为 nil 则返回 defaultValue
func (JSON[T]) IfPresent ¶
func (j JSON[T]) IfPresent(fn func(T))
IfPresent 当 Data 非 nil 时执行回调(Java Optional 风格) 用于消除 `if x.Data != nil { ... }` 这类样板代码
func (JSON[T]) IsNil ¶
IsNil 判断 Data 是否为 nil 仅对指针、接口、切片、map、channel、函数类型有效(通过反射安全判断) 值类型(struct、int、string 等)永远返回 false
func (JSON[T]) ToOptional ¶
ToOptional 将 JSON[T] 转换为 Optional[T],便于使用 MapOpt/FlatMapOpt 等单子操作 Data 为 nil 时返回空 Optional
type MapAny ¶
MapAny 任意类型的 Map,支持数据库 JSON 序列化
func StructToMapAny ¶
StructToMapAny 将 structpb.Struct 转换为 MapAny
func StructToMapAnyFromStruct ¶
func StructToMapAnyFromStruct(v interface{}) MapAny
StructToMapAny 将 Go struct 转换为 MapAny 使用 JSON 序列化/反序列化实现,支持任意 struct 类型
type Optional ¶
type Optional[T any] struct { // contains filtered or unexported fields }
Optional[T] 通用可选值容器,支持 nil-safe 链式操作
设计要点:
- 内部用 present 标记区分"空"与"零值",对值类型(int/struct)也能正确表达缺失
- 对指针类型 T=*V 同样适用,present=false 优先判定为空
- 提供 Map / FlatMap / Filter 等单子操作,便于深层嵌套字段的链式访问
与 JSON[T] 的关系:JSON[T] 主要服务于数据库读写,Optional[T] 服务于业务层 nil-safe 访问 通过 JSON[T].ToOptional() 进行桥接
func FlatMapOpt ¶
FlatMapOpt 单子绑定,fn 返回 Optional[U]
func MapOpt ¶
MapOpt 转换为 Optional[U],fn 返回 *U 便于主动中断链路(返回 nil 即视为空) 注意:Go 方法不支持额外类型参数,因此单子操作以泛型函数形式提供
示例:
fb := types.MapOpt(types.OfNullable(tracking), func(t *TrackingConfig) *FacebookConfig {
return t.Facebook
})
func OfNullable ¶
OfNullable 接受指针 *T,nil 时返回空 Optional;非 nil 时解引用装入 Optional[T] 适用于"从指针提取值"场景
func OfNullablePtr ¶
OfNullablePtr 接受指针 *T,nil 时返回空 Optional[*T];非 nil 时装入指针本身 与 OfNullable 的区别:OfNullable 解引用得到 Optional[T],OfNullablePtr 保留 Optional[*T] 适用于"链式穿透多层可空指针字段"场景(配合 FlatMapOpt)
func (Optional[T]) GetOrDefault ¶
func (o Optional[T]) GetOrDefault(defaultValue T) T
GetOrDefault 获取值,空 Optional 返回 defaultValue
func (Optional[T]) IfPresentOrElse ¶
func (o Optional[T]) IfPresentOrElse(action func(T), emptyAction func())
IfPresentOrElse 有值时执行 action,无值时执行 emptyAction
type ProtoJSON ¶
type ProtoJSON[T any] struct { Data T }
ProtoJSON 泛型 protobuf JSON 类型,支持 protobuf 消息的数据库存储
type ProtoJSONMap ¶
ProtoJSONMap - 泛型 protobuf JSON 类型,支持 protobuf 消息的数据库存储
func NewProtoJSONMap ¶
func NewProtoJSONMap[T proto.Message]() *ProtoJSONMap[T]
NewProtoJSONMap - 创建一个新的 protobuf JSON 类型映射
func (*ProtoJSONMap[T]) Get ¶
func (p *ProtoJSONMap[T]) Get(key string) T
Get - 获取指定键的 protobuf JSON 类型值
func (*ProtoJSONMap[T]) Scan ¶
func (p *ProtoJSONMap[T]) Scan(value interface{}) error
func (*ProtoJSONMap[T]) Set ¶
func (p *ProtoJSONMap[T]) Set(key string, data T)
Set - 设置指定键的 protobuf JSON 类型值
type StringSlice ¶
type StringSlice []string
StringSlice 字符串切片,支持数据库 JSON 序列化
func ParseStringSlice ¶
func ParseStringSlice(input []string) StringSlice
ParseStringSlice 从混合格式解析字符串切片 支持数组或分隔字符串 (分号;换行符\n)
func (StringSlice) Filter ¶
func (s StringSlice) Filter(fn func(string) bool) StringSlice
Filter 过滤元素
func (*StringSlice) FromDelimitedString ¶
func (s *StringSlice) FromDelimitedString(input string, delimiters ...string) StringSlice
FromDelimitedString 从分隔符字符串创建 StringSlice 支持多种分隔符: 分号(;)、换行符(\n)、逗号(,) 自动去除空白字符和空字符串
func (*StringSlice) Remove ¶
func (s *StringSlice) Remove(item string) StringSlice
Remove 移除指定元素(第一个匹配的)
func (*StringSlice) RemoveAt ¶
func (s *StringSlice) RemoveAt(index int) StringSlice
RemoveAt 移除指定索引的元素
func (*StringSlice) Scan ¶
func (s *StringSlice) Scan(value interface{}) error