Documentation
¶
Index ¶
- Constants
- Variables
- func Kind(dest any) reflect.Type
- func NewDuplicateDBNameError(structName, fieldName1, fieldName2 string) error
- func NewUnsupportedDataTypeError(dest any) error
- func ParseTagSetting(str string, sep string) map[string]string
- func ToArray(v any) (r []any)
- func ToInt(i any) (r int64)
- func ToInt32(i any) int32
- func ToString(value any) string
- func ValueOf(i any) reflect.Value
- func Warm(dests ...any) error
- func WarmWithOptions(opts *Options, dests ...any) error
- type Field
- func (field *Field) DBName() string
- func (field *Field) Get(value reflect.Value) reflect.Value
- func (field *Field) GetEmbeddedFields() (r []*Field)
- func (field *Field) GetName(ks ...string) string
- func (field *Field) GetTagName(k string) string
- func (field *Field) JSName() string
- func (field *Field) ReflectValueOf(value reflect.Value) reflect.Value
- func (field *Field) Set(value reflect.Value, v any) error
- func (field *Field) SetBool(value reflect.Value, v any) error
- func (field *Field) SetFloat(value reflect.Value, v any) error
- func (field *Field) SetInt(value reflect.Value, v any) error
- func (field *Field) SetString(value reflect.Value, v any) error
- func (field *Field) SetUint(value reflect.Value, v any) error
- type GetIndexes
- type Index
- type IndexField
- type IndexPartialParse
- type Indexes
- type Namer
- type NamingStrategy
- type Options
- type Replacer
- type Schema
- func (schema *Schema) GetValue(obj any, key string, keys ...any) (r any)
- func (schema *Schema) JSName(k string) (r string)
- func (schema *Schema) LookIndex(name string) *Index
- func (schema *Schema) LookUpField(name string) *Field
- func (schema *Schema) Make() reflect.Value
- func (schema *Schema) New() reflect.Value
- func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field
- func (schema *Schema) ParseIndexes() map[string]*Index
- func (schema *Schema) Range(cb func(*Field) bool)
- func (schema *Schema) SetValue(obj any, val any, key string, keys ...any) (err error)
- func (schema *Schema) String() string
- type Tabler
Constants ¶
const ( IndexTag = "index" IndexName = "NAME" IndexSort = "SORT" IndexUnique = "UNIQUE" IndexSparse = "SPARSE" IndexPartial = "PARTIAL" //部分索引 index:"PARTIAL:{\"invite\":{\"$gt\":0}}"` IndexPriority = "PRIORITY" )
https://www.mongodb.com/zh-cn/docs/manual/core/index-partial/#std-label-index-type-partial 部分索引 使用一条查询语句
Variables ¶
var ( ErrUnsupportedDataType = errors.New("unsupported data type") ErrDuplicateDBName = errors.New("duplicate database field name") )
var SchemaInitTimeout = 30 * time.Second
SchemaInitTimeout 等待其他 goroutine 完成 Schema 初始化的最大时长。 仅用于防御同 goroutine 自引用 struct 造成的死锁;正常并发构建等待是 μs 级, 基本不会触发此上限。超时后返回明确错误,避免静默卡住。
Functions ¶
func NewDuplicateDBNameError ¶ added in v1.6.8
func NewUnsupportedDataTypeError ¶ added in v1.6.8
func ParseTagSetting ¶
ParseTagSetting 解析结构体标签设置
func Warm ¶ added in v1.8.0
Warm 在应用启动阶段预先解析一批类型,以消除请求期首次访问时的 Schema 构建等待。 传入每个类型的零值指针(如 &User{}, &Order{}),使用默认 Options。 遇到任一解析错误立即返回该错误,已成功的 Schema 会保留在缓存中。
典型用法:
func main() {
if err := schema.Warm(&User{}, &Order{}, &Product{}); err != nil {
log.Fatal(err)
}
// ... 后续请求不会再触发同步构建等待
}
func WarmWithOptions ¶ added in v1.8.0
WarmWithOptions 与 Warm 相同,但使用自定义 Options。 对每个 dest 调用 opts.Parse(dest),发生错误立即返回。
Types ¶
type Field ¶
type Field struct {
// 核心字段:字段标识和类型信息
Name string // 字段名称
FieldType reflect.Type // 字段类型
IndirectFieldType reflect.Type // 间接字段类型(去除指针)
StructField reflect.StructField // 原始结构体字段
// 关联信息:与 Schema 和嵌入字段的关系
Schema *Schema // 所在的父 Schema
Embedded *Schema // 嵌入子对象的 Schema
// 访问信息:字段访问路径和缓存
Index []int // 字段索引路径
// contains filtered or unexported fields
}
Field 表示结构体的一个字段,包含字段的元数据和操作方法
func (*Field) GetEmbeddedFields ¶ added in v1.1.0
func (*Field) GetTagName ¶ added in v1.2.0
func (*Field) ReflectValueOf ¶
type GetIndexes ¶ added in v1.8.1
type GetIndexes interface {
GetIndexes() []*IndexField
}
type Index ¶
type Index struct {
Name string
Unique bool //唯一
Sparse bool //稀疏索引
Partial []string
Fields []*IndexField
// contains filtered or unexported fields
}
func (*Index) Build ¶
func (this *Index) Build(whereParser ...IndexPartialParse) (index *mongo.IndexModel, err error)
type IndexField ¶
type IndexField struct {
Sort string // DESC, ASC
Name string // index name
DBName []string // a.b.c
Unique bool //唯一
Sparse bool //稀疏索引:仅包含具有索引字段的文档,节省存储空间,适用于字段存在性较少的场景
Partial string //部分索引:基于指定的过滤条件创建索引,只包含满足条件的文档,提高查询性能并减少存储开销,格式如:{"rating":{"$gt":5}}
Priority int //排序字段之间的排序ASC
}
func (*IndexField) GetDBName ¶
func (this *IndexField) GetDBName() string
type IndexPartialParse ¶ added in v1.2.0
type Indexes ¶ added in v1.8.1
func (Indexes) AddField ¶ added in v1.8.1
func (is Indexes) AddField(sch *Schema, field *IndexField)
type NamingStrategy ¶
type NamingStrategy struct {
TablePrefix string
SingularTable bool
NameReplacer Replacer
NoLowerCase bool
}
NamingStrategy tables, columns naming strategy
func (NamingStrategy) ColumnName ¶
func (ns NamingStrategy) ColumnName(table, column string) string
ColumnName convert string to column name
func (NamingStrategy) TableName ¶
func (ns NamingStrategy) TableName(str string) string
TableName convert string to table name
type Schema ¶
type Schema struct {
Name string
Table string
Embedded []*Field //匿名嵌入字段的 Field 列表
ModelType reflect.Type
// Fields 按 Go 字段名(含嵌入提升)索引的全量字段表,公开访问。
Fields map[string]*Field
// contains filtered or unexported fields
}
func GetOrParse ¶ added in v1.1.0
GetOrParse 从目标结构体解析 Schema 信息,支持自定义选项
参数:
dest: 目标结构体实例或指针,用于解析其字段信息 opts: 解析选项,包含缓存、表名生成等配置
返回:
*Schema: 解析生成的 Schema 实例,包含结构体的所有字段信息 error: 解析过程中发生的错误,如类型不支持等
示例:
type User struct {
ID int `json:"id"`
Name string `json:"name"`
}
opts := &schema.Options{...}
schema, err := schema.GetOrParse(&User{}, opts)
func Parse ¶
Parse 从目标结构体解析 Schema 信息
参数:
dest: 目标结构体实例或指针,用于解析其字段信息
返回:
*Schema: 解析生成的 Schema 实例,包含结构体的所有字段信息 error: 解析过程中发生的错误,如类型不支持等
示例:
type User struct {
ID int `json:"id"`
Name string `json:"name"`
}
schema, err := schema.Parse(&User{})
func ParseWithSpecialTableName ¶
ParseWithSpecialTableName 从目标结构体解析 Schema 信息,支持自定义表名。 缓存命中时走零分配快路径(不设置 defer/recover),仅缓存未命中时进入完整解析。
func (*Schema) LookUpField ¶
LookUpField 按名字查找字段,支持 Go 字段名、db 标签名、json 标签名任一(优先级依此顺序)。 通过构建期合并的 unifiedFields 单次 map 查询完成。
func (*Schema) ParseField ¶
func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field
func (*Schema) ParseIndexes ¶
ParseIndexes parse schema indexes
func (*Schema) Range ¶ added in v1.1.0
Range 按注册顺序遍历所有带 db 标签的字段(用于 ORM 持久化场景)。 slice 迭代比 map 更快,且顺序稳定。