新增 fieldx 包,提供基于预定义 Schema 生成 map[string]any 对象的功能。 主要特性: - 支持固定值字段 (string)、字段引用 (field) 和嵌套对象 (object) - 支持点号分隔的嵌套路径访问 (如 "user.name") - 提供多种 Schema 创建方式 (JSON、Map、编程方式) - 完善的错误处理和文档示例
297 lines
6.2 KiB
Go
297 lines
6.2 KiB
Go
package fieldx_test
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
|
||
"git.fsdpf.net/go/reflux/fieldx"
|
||
)
|
||
|
||
// ExampleSchemaFromJSON 演示从 JSON 字符串创建 Schema 并生成对象
|
||
func ExampleSchemaFromJSON() {
|
||
// JSON Schema 定义
|
||
jsonSchema := `{
|
||
"name": {
|
||
"type": "field",
|
||
"value": "userName"
|
||
},
|
||
"status": {
|
||
"type": "string",
|
||
"value": "active"
|
||
},
|
||
"profile": {
|
||
"type": "object",
|
||
"fields": {
|
||
"email": {
|
||
"type": "field",
|
||
"value": "email"
|
||
},
|
||
"role": {
|
||
"type": "string",
|
||
"value": "user"
|
||
}
|
||
}
|
||
}
|
||
}`
|
||
|
||
// 从 JSON 创建 Schema
|
||
schema, _ := fieldx.SchemaFromJSON(jsonSchema)
|
||
|
||
// 源数据
|
||
source := map[string]any{
|
||
"userName": "Alice",
|
||
"email": "alice@example.com",
|
||
}
|
||
|
||
// 生成对象
|
||
result, _ := schema.Generate(source)
|
||
|
||
// 输出结果
|
||
fmt.Printf("name: %s\n", result["name"])
|
||
fmt.Printf("status: %s\n", result["status"])
|
||
profile := result["profile"].(map[string]any)
|
||
fmt.Printf("profile.email: %s\n", profile["email"])
|
||
fmt.Printf("profile.role: %s\n", profile["role"])
|
||
|
||
// Output:
|
||
// name: Alice
|
||
// status: active
|
||
// profile.email: alice@example.com
|
||
// profile.role: user
|
||
}
|
||
|
||
// ExampleSchema_Generate_nestedPath 演示使用嵌套路径访问源数据
|
||
func ExampleSchema_Generate_nestedPath() {
|
||
// 定义 Schema(使用点号分隔的路径)
|
||
schema := fieldx.Schema{
|
||
"userId": fieldx.Field{
|
||
Type: fieldx.FieldTypeField,
|
||
Value: "user.id",
|
||
},
|
||
"userName": fieldx.Field{
|
||
Type: fieldx.FieldTypeField,
|
||
Value: "user.name",
|
||
},
|
||
"userEmail": fieldx.Field{
|
||
Type: fieldx.FieldTypeField,
|
||
Value: "user.contact.email",
|
||
},
|
||
}
|
||
|
||
// 嵌套的源数据
|
||
source := map[string]any{
|
||
"user": map[string]any{
|
||
"id": 123,
|
||
"name": "Alice",
|
||
"contact": map[string]any{
|
||
"email": "alice@example.com",
|
||
},
|
||
},
|
||
}
|
||
|
||
// 生成对象
|
||
result, _ := schema.Generate(source)
|
||
|
||
// 输出结果
|
||
fmt.Printf("userId: %v\n", result["userId"])
|
||
fmt.Printf("userName: %s\n", result["userName"])
|
||
fmt.Printf("userEmail: %s\n", result["userEmail"])
|
||
|
||
// Output:
|
||
// userId: 123
|
||
// userName: Alice
|
||
// userEmail: alice@example.com
|
||
}
|
||
|
||
// ExampleSchema_Generate_complexNested 演示复杂的嵌套对象生成
|
||
func ExampleSchema_Generate_complexNested() {
|
||
// 定义复杂的嵌套 Schema
|
||
schema := fieldx.Schema{
|
||
"type": fieldx.Field{
|
||
Type: fieldx.FieldTypeString,
|
||
Value: "user_profile",
|
||
},
|
||
"user": fieldx.Field{
|
||
Type: fieldx.FieldTypeObject,
|
||
Fields: fieldx.Schema{
|
||
"id": fieldx.Field{
|
||
Type: fieldx.FieldTypeField,
|
||
Value: "userId",
|
||
},
|
||
"name": fieldx.Field{
|
||
Type: fieldx.FieldTypeField,
|
||
Value: "userName",
|
||
},
|
||
"contact": fieldx.Field{
|
||
Type: fieldx.FieldTypeObject,
|
||
Fields: fieldx.Schema{
|
||
"email": fieldx.Field{
|
||
Type: fieldx.FieldTypeField,
|
||
Value: "email",
|
||
},
|
||
"phone": fieldx.Field{
|
||
Type: fieldx.FieldTypeString,
|
||
Value: "N/A",
|
||
},
|
||
},
|
||
},
|
||
},
|
||
},
|
||
}
|
||
|
||
// 源数据
|
||
source := map[string]any{
|
||
"userId": 456,
|
||
"userName": "Bob",
|
||
"email": "bob@example.com",
|
||
}
|
||
|
||
// 生成对象
|
||
result, _ := schema.Generate(source)
|
||
|
||
// 输出结果
|
||
fmt.Printf("type: %s\n", result["type"])
|
||
user := result["user"].(map[string]any)
|
||
fmt.Printf("user.id: %v\n", user["id"])
|
||
fmt.Printf("user.name: %s\n", user["name"])
|
||
contact := user["contact"].(map[string]any)
|
||
fmt.Printf("user.contact.email: %s\n", contact["email"])
|
||
fmt.Printf("user.contact.phone: %s\n", contact["phone"])
|
||
|
||
// Output:
|
||
// type: user_profile
|
||
// user.id: 456
|
||
// user.name: Bob
|
||
// user.contact.email: bob@example.com
|
||
// user.contact.phone: N/A
|
||
}
|
||
|
||
// ExampleSchema_Unmarshal 演示直接使用 json.Unmarshal 创建 Schema
|
||
func ExampleSchema_Unmarshal() {
|
||
// JSON Schema 字符串
|
||
jsonStr := `{
|
||
"name": {
|
||
"type": "field",
|
||
"value": "userName"
|
||
},
|
||
"status": {
|
||
"type": "string",
|
||
"value": "active"
|
||
}
|
||
}`
|
||
|
||
// 直接反序列化为 Schema
|
||
var schema fieldx.Schema
|
||
json.Unmarshal([]byte(jsonStr), &schema)
|
||
|
||
// 源数据
|
||
source := map[string]any{
|
||
"userName": "Alice",
|
||
}
|
||
|
||
// 生成对象
|
||
result, _ := schema.Generate(source)
|
||
|
||
// 输出结果
|
||
fmt.Printf("name: %s\n", result["name"])
|
||
fmt.Printf("status: %s\n", result["status"])
|
||
|
||
// Output:
|
||
// name: Alice
|
||
// status: active
|
||
}
|
||
|
||
// ExampleSchemaFromMap 演示从 map[string]any 创建 Schema
|
||
func ExampleSchemaFromMap() {
|
||
// 先解析 JSON 到 map
|
||
jsonStr := `{
|
||
"userId": {
|
||
"type": "field",
|
||
"value": "user.id"
|
||
},
|
||
"userName": {
|
||
"type": "field",
|
||
"value": "user.name"
|
||
}
|
||
}`
|
||
|
||
var data map[string]any
|
||
json.Unmarshal([]byte(jsonStr), &data)
|
||
|
||
// 从 map 创建 Schema
|
||
schema, _ := fieldx.SchemaFromMap(data)
|
||
|
||
// 源数据
|
||
source := map[string]any{
|
||
"user": map[string]any{
|
||
"id": 789,
|
||
"name": "Charlie",
|
||
},
|
||
}
|
||
|
||
// 生成对象
|
||
result, _ := schema.Generate(source)
|
||
|
||
// 输出结果
|
||
fmt.Printf("userId: %v\n", result["userId"])
|
||
fmt.Printf("userName: %s\n", result["userName"])
|
||
|
||
// Output:
|
||
// userId: 789
|
||
// userName: Charlie
|
||
}
|
||
|
||
// ExampleSchema_allFieldTypes 演示所有字段类型的使用
|
||
func ExampleSchema_allFieldTypes() {
|
||
// 定义包含所有字段类型的 Schema
|
||
schema := fieldx.Schema{
|
||
// string 类型:固定字符串值
|
||
"version": fieldx.Field{
|
||
Type: fieldx.FieldTypeString,
|
||
Value: "1.0.0",
|
||
},
|
||
// field 类型:从源数据获取值
|
||
"title": fieldx.Field{
|
||
Type: fieldx.FieldTypeField,
|
||
Value: "documentTitle",
|
||
},
|
||
// object 类型:嵌套对象
|
||
"metadata": fieldx.Field{
|
||
Type: fieldx.FieldTypeObject,
|
||
Fields: fieldx.Schema{
|
||
"created": fieldx.Field{
|
||
Type: fieldx.FieldTypeField,
|
||
Value: "createdAt",
|
||
},
|
||
"author": fieldx.Field{
|
||
Type: fieldx.FieldTypeField,
|
||
Value: "author",
|
||
},
|
||
},
|
||
},
|
||
}
|
||
|
||
// 源数据
|
||
source := map[string]any{
|
||
"documentTitle": "API Documentation",
|
||
"createdAt": "2024-01-01",
|
||
"author": "Alice",
|
||
}
|
||
|
||
// 生成对象
|
||
result, _ := schema.Generate(source)
|
||
|
||
// 输出结果
|
||
fmt.Printf("version: %s\n", result["version"])
|
||
fmt.Printf("title: %s\n", result["title"])
|
||
metadata := result["metadata"].(map[string]any)
|
||
fmt.Printf("metadata.created: %s\n", metadata["created"])
|
||
fmt.Printf("metadata.author: %s\n", metadata["author"])
|
||
|
||
// Output:
|
||
// version: 1.0.0
|
||
// title: API Documentation
|
||
// metadata.created: 2024-01-01
|
||
// metadata.author: Alice
|
||
}
|