前端:优化 Manifest 视图文件显示
- 文件名区域改为左右布局 - 左侧显示文件名(如 000001.sst) - 右侧显示级别标签(如 L0、L1) - 添加级别标签样式,使用主题色背景
This commit is contained in:
153
examples/struct_schema/README.md
Normal file
153
examples/struct_schema/README.md
Normal file
@@ -0,0 +1,153 @@
|
||||
# StructToFields 示例
|
||||
|
||||
这个示例展示如何使用 `StructToFields` 方法从 Go 结构体自动生成 Schema。
|
||||
|
||||
## 功能特性
|
||||
|
||||
- ✅ 从结构体自动生成 Field 列表
|
||||
- ✅ 支持 struct tags 定义字段属性
|
||||
- ✅ 支持索引标记
|
||||
- ✅ 支持字段注释
|
||||
- ✅ 自动类型映射
|
||||
- ✅ 支持忽略字段
|
||||
|
||||
## Struct Tag 格式
|
||||
|
||||
### srdb tag
|
||||
|
||||
所有配置都在 `srdb` tag 中,使用分号 `;` 分隔:
|
||||
|
||||
```go
|
||||
type User struct {
|
||||
// 基本用法:指定字段名
|
||||
Name string `srdb:"name"`
|
||||
|
||||
// 标记为索引字段
|
||||
Email string `srdb:"email;indexed"`
|
||||
|
||||
// 完整格式:字段名;索引;注释
|
||||
Age int64 `srdb:"age;comment:年龄"`
|
||||
|
||||
// 带索引和注释
|
||||
Phone string `srdb:"phone;indexed;comment:手机号"`
|
||||
|
||||
// 忽略该字段
|
||||
Internal string `srdb:"-"`
|
||||
|
||||
// 不使用 tag,默认使用 snake_case 转换
|
||||
Score float64 // 字段名: score
|
||||
UserID string // 字段名: user_id
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### Tag 格式说明
|
||||
|
||||
格式:`srdb:"字段名;选项1;选项2;..."`
|
||||
|
||||
- **字段名**(第一部分):指定数据库中的字段名,省略则自动将结构体字段名转为 snake_case
|
||||
- **indexed**:标记该字段需要建立索引
|
||||
- **comment:注释内容**:字段注释说明
|
||||
|
||||
### 默认字段名转换(snake_case)
|
||||
|
||||
如果不指定字段名,会自动将驼峰命名转换为 snake_case:
|
||||
|
||||
- `UserName` → `user_name`
|
||||
- `EmailAddress` → `email_address`
|
||||
- `IsActive` → `is_active`
|
||||
- `HTTPServer` → `http_server`
|
||||
- `ID` → `id`
|
||||
|
||||
## 类型映射
|
||||
|
||||
| Go 类型 | FieldType |
|
||||
|---------|-----------|
|
||||
| int, int64, int32, int16, int8 | FieldTypeInt64 |
|
||||
| uint, uint64, uint32, uint16, uint8 | FieldTypeInt64 |
|
||||
| string | FieldTypeString |
|
||||
| float64, float32 | FieldTypeFloat |
|
||||
| bool | FieldTypeBool |
|
||||
|
||||
## 完整示例
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"code.tczkiot.com/wlw/srdb"
|
||||
)
|
||||
|
||||
// 定义结构体
|
||||
type User struct {
|
||||
Name string `srdb:"name;indexed;comment:用户名"`
|
||||
Age int64 `srdb:"age;comment:年龄"`
|
||||
Email string `srdb:"email;indexed;comment:邮箱"`
|
||||
Score float64 `srdb:"score;comment:分数"`
|
||||
IsActive bool `srdb:"is_active;comment:是否激活"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 1. 从结构体生成 Field 列表
|
||||
fields, err := srdb.StructToFields(User{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// 2. 创建 Schema
|
||||
schema := srdb.NewSchema("users", fields)
|
||||
|
||||
// 3. 创建表
|
||||
table, err := srdb.OpenTable(&srdb.TableOptions{
|
||||
Dir: "./data/users",
|
||||
Name: schema.Name,
|
||||
Fields: schema.Fields,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer table.Close()
|
||||
|
||||
// 4. 插入数据
|
||||
err = table.Insert(map[string]any{
|
||||
"name": "张三",
|
||||
"age": int64(25),
|
||||
"email": "zhangsan@example.com",
|
||||
"score": 95.5,
|
||||
"is_active": true,
|
||||
})
|
||||
|
||||
// 5. 查询数据(自动使用索引)
|
||||
rows, _ := table.Query().Eq("email", "zhangsan@example.com").Rows()
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
data := rows.Row().Data()
|
||||
// 处理数据...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 运行示例
|
||||
|
||||
```bash
|
||||
cd examples/struct_schema
|
||||
go run main.go
|
||||
```
|
||||
|
||||
## 优势
|
||||
|
||||
1. **类型安全**: 使用结构体定义,编译时检查类型
|
||||
2. **简洁**: 不需要手动创建 Field 列表
|
||||
3. **可维护**: 结构体和 Schema 在一起,便于维护
|
||||
4. **灵活**: 支持 tag 自定义字段属性
|
||||
5. **自动索引**: 通过 `indexed` tag 自动创建索引
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. 只有导出的字段(首字母大写)会被包含
|
||||
2. 使用 `srdb:"-"` 可以忽略字段
|
||||
3. 如果不指定字段名,默认使用小写的字段名
|
||||
4. 不支持嵌套结构体(需要手动展开)
|
||||
5. 不支持切片、map 等复杂类型
|
||||
1
examples/struct_schema/data/users/CURRENT
Normal file
1
examples/struct_schema/data/users/CURRENT
Normal file
@@ -0,0 +1 @@
|
||||
MANIFEST-000001
|
||||
BIN
examples/struct_schema/data/users/MANIFEST-000001
Normal file
BIN
examples/struct_schema/data/users/MANIFEST-000001
Normal file
Binary file not shown.
40
examples/struct_schema/data/users/schema.json
Normal file
40
examples/struct_schema/data/users/schema.json
Normal file
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"version": 1,
|
||||
"timestamp": 1760013049,
|
||||
"checksum": "fad0aaaa6fc5b94364c0c0b07a7567f71e8bb4f8ed8456c1954cffa7538a6a42",
|
||||
"schema": {
|
||||
"Name": "users",
|
||||
"Fields": [
|
||||
{
|
||||
"Name": "name",
|
||||
"Type": 2,
|
||||
"Indexed": true,
|
||||
"Comment": "用户名"
|
||||
},
|
||||
{
|
||||
"Name": "age",
|
||||
"Type": 1,
|
||||
"Indexed": false,
|
||||
"Comment": "年龄"
|
||||
},
|
||||
{
|
||||
"Name": "email",
|
||||
"Type": 2,
|
||||
"Indexed": true,
|
||||
"Comment": "邮箱"
|
||||
},
|
||||
{
|
||||
"Name": "score",
|
||||
"Type": 3,
|
||||
"Indexed": false,
|
||||
"Comment": "分数"
|
||||
},
|
||||
{
|
||||
"Name": "is_active",
|
||||
"Type": 4,
|
||||
"Indexed": false,
|
||||
"Comment": "是否激活"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
1
examples/struct_schema/data/users/wal/CURRENT
Normal file
1
examples/struct_schema/data/users/wal/CURRENT
Normal file
@@ -0,0 +1 @@
|
||||
3
|
||||
130
examples/struct_schema/main.go
Normal file
130
examples/struct_schema/main.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"code.tczkiot.com/wlw/srdb"
|
||||
)
|
||||
|
||||
// User 用户结构体
|
||||
// 使用 struct tags 定义 Schema
|
||||
type User struct {
|
||||
Name string `srdb:"name;indexed;comment:用户名"`
|
||||
Age int64 `srdb:"age;comment:年龄"`
|
||||
Email string `srdb:"email;indexed;comment:邮箱"`
|
||||
Score float64 `srdb:"score;comment:分数"`
|
||||
IsActive bool `srdb:"is_active;comment:是否激活"`
|
||||
Internal string `srdb:"-"` // 不会被包含在 Schema 中
|
||||
}
|
||||
|
||||
// Product 产品结构体
|
||||
// 不使用 srdb tag,字段名会自动转为 snake_case
|
||||
type Product struct {
|
||||
ProductID string // 字段名: product_id
|
||||
ProductName string // 字段名: product_name
|
||||
Price int64 // 字段名: price
|
||||
InStock bool // 字段名: in_stock
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 示例 1: 使用结构体创建 Schema
|
||||
fmt.Println("=== 示例 1: 从结构体创建 Schema ===")
|
||||
|
||||
// 从 User 结构体生成 Field 列表
|
||||
fields, err := srdb.StructToFields(User{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// 创建 Schema
|
||||
schema := srdb.NewSchema("users", fields)
|
||||
|
||||
// 打印 Schema 信息
|
||||
fmt.Printf("Schema 名称: %s\n", schema.Name)
|
||||
fmt.Printf("字段数量: %d\n", len(schema.Fields))
|
||||
fmt.Println("\n字段列表:")
|
||||
for _, field := range schema.Fields {
|
||||
indexed := ""
|
||||
if field.Indexed {
|
||||
indexed = " [索引]"
|
||||
}
|
||||
fmt.Printf(" - %s (%s)%s: %s\n",
|
||||
field.Name, field.Type.String(), indexed, field.Comment)
|
||||
}
|
||||
|
||||
// 示例 2: 使用 Schema 创建表
|
||||
fmt.Println("\n=== 示例 2: 使用 Schema 创建表 ===")
|
||||
|
||||
table, err := srdb.OpenTable(&srdb.TableOptions{
|
||||
Dir: "./data/users",
|
||||
Name: schema.Name,
|
||||
Fields: schema.Fields,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer table.Close()
|
||||
|
||||
// 插入数据
|
||||
err = table.Insert(map[string]any{
|
||||
"name": "张三",
|
||||
"age": int64(25),
|
||||
"email": "zhangsan@example.com",
|
||||
"score": 95.5,
|
||||
"is_active": true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = table.Insert(map[string]any{
|
||||
"name": "李四",
|
||||
"age": int64(30),
|
||||
"email": "lisi@example.com",
|
||||
"score": 88.0,
|
||||
"is_active": true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("✓ 插入 2 条数据")
|
||||
|
||||
// 查询数据
|
||||
rows, err := table.Query().Eq("email", "zhangsan@example.com").Rows()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
fmt.Println("\n查询结果 (email = zhangsan@example.com):")
|
||||
for rows.Next() {
|
||||
data := rows.Row().Data()
|
||||
fmt.Printf(" 姓名: %s, 年龄: %v, 邮箱: %s, 分数: %v, 激活: %v\n",
|
||||
data["name"], data["age"], data["email"], data["score"], data["is_active"])
|
||||
}
|
||||
|
||||
// 示例 3: 使用默认字段名(snake_case)
|
||||
fmt.Println("\n=== 示例 3: 使用默认字段名(snake_case)===")
|
||||
|
||||
productFields, err := srdb.StructToFields(Product{})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println("Product 字段(使用默认 snake_case 名称):")
|
||||
for _, field := range productFields {
|
||||
fmt.Printf(" - %s (%s)\n", field.Name, field.Type.String())
|
||||
}
|
||||
|
||||
// 示例 4: 获取索引字段
|
||||
fmt.Println("\n=== 示例 4: 获取索引字段 ===")
|
||||
indexedFields := schema.GetIndexedFields()
|
||||
fmt.Printf("User Schema 中的索引字段(共 %d 个):\n", len(indexedFields))
|
||||
for _, field := range indexedFields {
|
||||
fmt.Printf(" - %s: %s\n", field.Name, field.Comment)
|
||||
}
|
||||
|
||||
fmt.Println("\n✓ 所有示例执行成功!")
|
||||
}
|
||||
Reference in New Issue
Block a user