151 lines
3.4 KiB
Go
151 lines
3.4 KiB
Go
package reflux_test
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.fsdpf.net/go/reflux"
|
|
)
|
|
|
|
// ExampleSet_errorMessages 演示设置失败时的详细错误信息
|
|
func ExampleSet_errorMessages() {
|
|
type Person struct {
|
|
Name string
|
|
Age int
|
|
}
|
|
|
|
p := &Person{Name: "Alice", Age: 30}
|
|
r := reflux.New(p)
|
|
|
|
// 示例1: 类型不匹配错误 - 显示原有类型和传入值的类型
|
|
defer func() {
|
|
if rec := recover(); rec != nil {
|
|
fmt.Println("错误:", rec)
|
|
// 输出类似: rfx: set failed at path 'Age': cannot convert value of type map[string]int to target type int: ...
|
|
}
|
|
}()
|
|
|
|
// 尝试将 map 设置为 int 类型字段,会显示详细的类型转换错误
|
|
r.Set("Age", map[string]int{"invalid": 123})
|
|
}
|
|
|
|
// ExampleSet_fieldNotFound 演示字段不存在的错误信息
|
|
func ExampleSet_fieldNotFound() {
|
|
type Person struct {
|
|
Name string
|
|
Age int
|
|
}
|
|
|
|
p := &Person{Name: "Bob", Age: 25}
|
|
r := reflux.New(p)
|
|
|
|
defer func() {
|
|
if rec := recover(); rec != nil {
|
|
fmt.Println(rec)
|
|
// Output: rfx: set failed at path 'InvalidField': rfx: field not found, 'InvalidField' in struct reflux_test.Person
|
|
}
|
|
}()
|
|
|
|
r.Set("InvalidField", "value")
|
|
}
|
|
|
|
// ExampleSet_sliceIndexOutOfRange 演示切片索引越界的错误信息
|
|
func ExampleSet_sliceIndexOutOfRange() {
|
|
type Data struct {
|
|
Items []int
|
|
}
|
|
|
|
d := &Data{Items: []int{1, 2, 3}}
|
|
r := reflux.New(d)
|
|
|
|
defer func() {
|
|
if rec := recover(); rec != nil {
|
|
fmt.Println(rec)
|
|
// Output: rfx: set failed at path 'Items.10': rfx: index out of range, slice index 10 (len=3)
|
|
}
|
|
}()
|
|
|
|
r.Set("Items.10", 999)
|
|
}
|
|
|
|
// ExampleSet_unexportedField 演示未导出字段的错误信息
|
|
func ExampleSet_unexportedField() {
|
|
type Person struct {
|
|
Name string
|
|
age int // 未导出字段
|
|
}
|
|
|
|
p := &Person{Name: "Charlie"}
|
|
r := reflux.New(p)
|
|
|
|
defer func() {
|
|
if rec := recover(); rec != nil {
|
|
fmt.Println(rec)
|
|
// Output: rfx: set failed at path 'age': rfx: cannot set value, field 'age' (type: int) (unexported or not addressable)
|
|
}
|
|
}()
|
|
|
|
r.Set("age", 40)
|
|
}
|
|
|
|
// ExampleAppend_typeConversionError 演示 Append 时的类型转换错误
|
|
func ExampleAppend_typeConversionError() {
|
|
type Data struct {
|
|
Items []int
|
|
}
|
|
|
|
d := &Data{Items: []int{1, 2, 3}}
|
|
r := reflux.New(d).Get("Items")
|
|
|
|
defer func() {
|
|
if rec := recover(); rec != nil {
|
|
fmt.Println(rec)
|
|
// Output: rfx: failed to append item at index 0 to slice: cannot convert value of type map[string]int to target type int: ...
|
|
}
|
|
}()
|
|
|
|
// 尝试追加无法转换为 int 的 map
|
|
r.Append(map[string]int{"invalid": 123})
|
|
}
|
|
|
|
// ExampleSet_nestedPathError 演示嵌套路径中的错误信息
|
|
func ExampleSet_nestedPathError() {
|
|
type Address struct {
|
|
City string
|
|
}
|
|
type Person struct {
|
|
Name string
|
|
Address Address
|
|
}
|
|
|
|
p := &Person{
|
|
Name: "David",
|
|
Address: Address{City: "NYC"},
|
|
}
|
|
r := reflux.New(p)
|
|
|
|
defer func() {
|
|
if rec := recover(); rec != nil {
|
|
fmt.Println(rec)
|
|
// Output: rfx: set failed at path 'Address.Country': rfx: field not found, 'Country' in struct reflux_test.Address
|
|
}
|
|
}()
|
|
|
|
r.Set("Address.Country", "USA")
|
|
}
|
|
|
|
// ExampleSet_successfulTypeConversion 演示成功的类型转换
|
|
func ExampleSet_successfulTypeConversion() {
|
|
type Person struct {
|
|
Name string
|
|
Age int
|
|
}
|
|
|
|
p := &Person{Name: "Eve", Age: 20}
|
|
r := reflux.New(p)
|
|
|
|
// 字符串 "30" 可以成功转换为 int
|
|
r.Set("Age", "30")
|
|
fmt.Printf("Name: %s, Age: %d\n", p.Name, p.Age)
|
|
// Output: Name: Eve, Age: 30
|
|
}
|