go vet ./... 此前有 13 处失败, 导致 go test ./...(默认带 vet)根本跑不起来,
必须加 -vet=off。这是既存问题, 与实现改动无关。
命名问题(12 处): Example 函数名的前缀必须是包内真实存在的标识符。
ExampleStructMap_* -> ExampleR_* (包里没有 StructMap)
ExampleSet_* -> ExampleR_Set_* (Set 是 R 的方法, 不是包级函数)
ExampleAppend_* -> ExampleR_Append_* (同上)
ExampleSchema_Unmarshal -> ExampleSchema_unmarshalJSON
(Schema 没有 Unmarshal 方法,
这个例子演示的是 json.Unmarshal)
输出注释位置(1 处): ExampleR_Append_typeConversionError 的 // Output: 后面
还跟着一条注释, 违反"Output 必须是最后一个注释块"。因为格式不合法, Go 一直
没把它当输出例子比对 —— 里面写的期望输出其实是错的(与实际信息不符)也没被发现。
把注释挪到 defer 之前, 并把期望输出更正为真实的 panic 信息。
已验证: 故意改错期望输出现在会导致测试失败, 说明确实在比对了。
现在 go vet ./... 干净, go test ./... 无需 -vet=off 即可运行。
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_unmarshalJSON 演示直接使用 json.Unmarshal 创建 Schema
|
|
func ExampleSchema_unmarshalJSON() {
|
|
// 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
|
|
}
|