fix: 修正 Example 函数命名与输出注释位置,让 go vet / go test 恢复可用
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 即可运行。
This commit is contained in:
+16
-16
@@ -6,8 +6,8 @@ import (
|
|||||||
"git.fsdpf.net/go/reflux"
|
"git.fsdpf.net/go/reflux"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ExampleSet_errorMessages 演示设置失败时的详细错误信息
|
// ExampleR_Set_errorMessages 演示设置失败时的详细错误信息
|
||||||
func ExampleSet_errorMessages() {
|
func ExampleR_Set_errorMessages() {
|
||||||
type Person struct {
|
type Person struct {
|
||||||
Name string
|
Name string
|
||||||
Age int
|
Age int
|
||||||
@@ -28,8 +28,8 @@ func ExampleSet_errorMessages() {
|
|||||||
r.Set("Age", map[string]int{"invalid": 123})
|
r.Set("Age", map[string]int{"invalid": 123})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleSet_fieldNotFound 演示字段不存在的错误信息
|
// ExampleR_Set_fieldNotFound 演示字段不存在的错误信息
|
||||||
func ExampleSet_fieldNotFound() {
|
func ExampleR_Set_fieldNotFound() {
|
||||||
type Person struct {
|
type Person struct {
|
||||||
Name string
|
Name string
|
||||||
Age int
|
Age int
|
||||||
@@ -48,8 +48,8 @@ func ExampleSet_fieldNotFound() {
|
|||||||
r.Set("InvalidField", "value")
|
r.Set("InvalidField", "value")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleSet_sliceIndexOutOfRange 演示切片索引越界的错误信息
|
// ExampleR_Set_sliceIndexOutOfRange 演示切片索引越界的错误信息
|
||||||
func ExampleSet_sliceIndexOutOfRange() {
|
func ExampleR_Set_sliceIndexOutOfRange() {
|
||||||
type Data struct {
|
type Data struct {
|
||||||
Items []int
|
Items []int
|
||||||
}
|
}
|
||||||
@@ -67,8 +67,8 @@ func ExampleSet_sliceIndexOutOfRange() {
|
|||||||
r.Set("Items.10", 999)
|
r.Set("Items.10", 999)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleSet_unexportedField 演示未导出字段的错误信息
|
// ExampleR_Set_unexportedField 演示未导出字段的错误信息
|
||||||
func ExampleSet_unexportedField() {
|
func ExampleR_Set_unexportedField() {
|
||||||
type Person struct {
|
type Person struct {
|
||||||
Name string
|
Name string
|
||||||
age int // 未导出字段
|
age int // 未导出字段
|
||||||
@@ -87,8 +87,8 @@ func ExampleSet_unexportedField() {
|
|||||||
r.Set("age", 40)
|
r.Set("age", 40)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleAppend_typeConversionError 演示 Append 时的类型转换错误
|
// ExampleR_Append_typeConversionError 演示 Append 时的类型转换错误
|
||||||
func ExampleAppend_typeConversionError() {
|
func ExampleR_Append_typeConversionError() {
|
||||||
type Data struct {
|
type Data struct {
|
||||||
Items []int
|
Items []int
|
||||||
}
|
}
|
||||||
@@ -96,19 +96,19 @@ func ExampleAppend_typeConversionError() {
|
|||||||
d := &Data{Items: []int{1, 2, 3}}
|
d := &Data{Items: []int{1, 2, 3}}
|
||||||
r := reflux.New(d).Get("Items")
|
r := reflux.New(d).Get("Items")
|
||||||
|
|
||||||
|
// 尝试追加无法转换为 int 的 map
|
||||||
defer func() {
|
defer func() {
|
||||||
if rec := recover(); rec != nil {
|
if rec := recover(); rec != nil {
|
||||||
fmt.Println(rec)
|
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: ...
|
// Output: rfx: failed to append item at index 0 to slice: rfx: cannot convert, value of type map[string]int to target type int: unable to cast map[string]int{"invalid":123} of type map[string]int to int
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// 尝试追加无法转换为 int 的 map
|
|
||||||
r.Append(map[string]int{"invalid": 123})
|
r.Append(map[string]int{"invalid": 123})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleSet_nestedPathError 演示嵌套路径中的错误信息
|
// ExampleR_Set_nestedPathError 演示嵌套路径中的错误信息
|
||||||
func ExampleSet_nestedPathError() {
|
func ExampleR_Set_nestedPathError() {
|
||||||
type Address struct {
|
type Address struct {
|
||||||
City string
|
City string
|
||||||
}
|
}
|
||||||
@@ -133,8 +133,8 @@ func ExampleSet_nestedPathError() {
|
|||||||
r.Set("Address.Country", "USA")
|
r.Set("Address.Country", "USA")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleSet_successfulTypeConversion 演示成功的类型转换
|
// ExampleR_Set_successfulTypeConversion 演示成功的类型转换
|
||||||
func ExampleSet_successfulTypeConversion() {
|
func ExampleR_Set_successfulTypeConversion() {
|
||||||
type Person struct {
|
type Person struct {
|
||||||
Name string
|
Name string
|
||||||
Age int
|
Age int
|
||||||
|
|||||||
@@ -166,8 +166,8 @@ func ExampleSchema_Generate_complexNested() {
|
|||||||
// user.contact.phone: N/A
|
// user.contact.phone: N/A
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleSchema_Unmarshal 演示直接使用 json.Unmarshal 创建 Schema
|
// ExampleSchema_unmarshalJSON 演示直接使用 json.Unmarshal 创建 Schema
|
||||||
func ExampleSchema_Unmarshal() {
|
func ExampleSchema_unmarshalJSON() {
|
||||||
// JSON Schema 字符串
|
// JSON Schema 字符串
|
||||||
jsonStr := `{
|
jsonStr := `{
|
||||||
"name": {
|
"name": {
|
||||||
|
|||||||
+10
-10
@@ -342,8 +342,8 @@ func ExampleNew_withMap() {
|
|||||||
// New timeout: 30
|
// New timeout: 30
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleStructMap_Scope_noArgs 演示如何使用 Scope() 不传参数来复制整个对象
|
// ExampleR_Scope_noArgs 演示如何使用 Scope() 不传参数来复制整个对象
|
||||||
func ExampleStructMap_Scope_noArgs() {
|
func ExampleR_Scope_noArgs() {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Host string
|
Host string
|
||||||
Port int
|
Port int
|
||||||
@@ -376,8 +376,8 @@ func ExampleStructMap_Scope_noArgs() {
|
|||||||
// Clone - Host: 127.0.0.1, Port: 9090
|
// Clone - Host: 127.0.0.1, Port: 9090
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleStructMap_Scope_withPath 演示如何使用 Scope() 传入路径来复制嵌套对象
|
// ExampleR_Scope_withPath 演示如何使用 Scope() 传入路径来复制嵌套对象
|
||||||
func ExampleStructMap_Scope_withPath() {
|
func ExampleR_Scope_withPath() {
|
||||||
type Address struct {
|
type Address struct {
|
||||||
City string
|
City string
|
||||||
Street string
|
Street string
|
||||||
@@ -449,8 +449,8 @@ func ExampleNew_withNestedMap() {
|
|||||||
// User (dot notation): Alice
|
// User (dot notation): Alice
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleStructMap_lowercaseFields 演示如何使用小写字段名访问 struct
|
// ExampleR_lowercaseFields 演示如何使用小写字段名访问 struct
|
||||||
func ExampleStructMap_lowercaseFields() {
|
func ExampleR_lowercaseFields() {
|
||||||
type Person struct {
|
type Person struct {
|
||||||
Name string
|
Name string
|
||||||
Age int
|
Age int
|
||||||
@@ -480,8 +480,8 @@ func ExampleStructMap_lowercaseFields() {
|
|||||||
// Updated - Name: Bob, Age: 35
|
// Updated - Name: Bob, Age: 35
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleStructMap_lowercaseNestedFields 演示如何使用小写字段名访问嵌套 struct
|
// ExampleR_lowercaseNestedFields 演示如何使用小写字段名访问嵌套 struct
|
||||||
func ExampleStructMap_lowercaseNestedFields() {
|
func ExampleR_lowercaseNestedFields() {
|
||||||
type Address struct {
|
type Address struct {
|
||||||
City string
|
City string
|
||||||
Street string
|
Street string
|
||||||
@@ -520,8 +520,8 @@ func ExampleStructMap_lowercaseNestedFields() {
|
|||||||
// Updated City: Shanghai, Street: New Street
|
// Updated City: Shanghai, Street: New Street
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExampleStructMap_castConversion 演示使用 cast 进行智能类型转换
|
// ExampleR_castConversion 演示使用 cast 进行智能类型转换
|
||||||
func ExampleStructMap_castConversion() {
|
func ExampleR_castConversion() {
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Port int
|
Port int
|
||||||
Host string
|
Host string
|
||||||
|
|||||||
Reference in New Issue
Block a user