作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我想构建一个将结构作为接口(interface){}
的方法,并在提供的结构的任何字段为 nil 时返回 true
。
这是我目前拥有的:
// ContainsNil returns true if any fields within the supplied structure are nil.
//
// If the supplied object is not a struct, the method will panic.
// Nested structs are inspected recursively.
// Maps and slices are not inspected deeply. This may change.
func ContainsNil(obj interface{}) bool {
if obj == nil {
return true
}
s := reflect.Indirect(reflect.ValueOf(obj))
for i := 0; i < s.NumField(); i++ {
f := s.Type().Field(i)
field := s.Field(i)
if fieldIsExported(f) { // Exported-check must be evaluated first to avoid panic.
if field.Kind() == reflect.Struct {
if ContainsNil(field.Addr()) {
return true
}
} else {
if field.IsNil() {
return true
}
if field.Interface() == nil {
return true
}
}
}
}
return false
}
func fieldIsExported(field reflect.StructField) bool {
log.Println(field.Name)
return field.Name[0] >= 65 == true && field.Name[0] <= 90 == true
}
还有一个失败的测试:
func Test_ContainsNil_NilNestedValue_ReturnsTrue(t *testing.T) {
someNestedStruct := &c.SomeNestedStruct{
SomeStruct: c.SomeStruct{
SomeString: nil,
},
}
result := util.ContainsNil(someNestedStruct)
assert.True(t, result)
}
测试代码执行时没有出现 panic,但失败了,因为该方法返回 false
而不是 true
。
我遇到的问题是我不知道如何正确地将嵌套结构传递回对 ContainsNil
的递归调用。
当对嵌套结构进行递归调用时,fieldIsExported
方法返回 false
,因为它没有收到我期望它收到的值。
我希望 fieldIsExported
在第一次调用时收到“SomeStruct”,并在第二次(递归)调用时收到“SomeString”。第一次调用按预期进行,但在第二次调用时,fieldIsExported
收到“typ”,而我希望它收到“SomeString”。
我已经做了很多关于在结构上使用反射的研究,但我还没有弄清楚这个问题。想法?
引用资料:
最佳答案
您检查当前字段是否是一个结构值,但是当它是一个结构的reflect.Ptr
或其他东西时,您永远不会考虑这种情况,所以对于这种情况,您的函数永远不会递归。这是缺少部分的函数。
https://play.golang.org/p/FdLxeee9UU
// ContainsNil returns true if any fields within the supplied structure are nil.
//
// If the supplied object is not a struct, the method will panic.
// Nested structs are inspected recursively.
// Maps and slices are not inspected deeply. This may change.
func ContainsNil(obj interface{}) bool {
if obj == nil {
return true
}
s := reflect.Indirect(reflect.ValueOf(obj))
for i := 0; i < s.NumField(); i++ {
f := s.Type().Field(i)
field := s.Field(i)
if fieldIsExported(f) { // Exported-check must be evaluated first to avoid panic.
if field.Kind() == reflect.Ptr { // case when it's a pointer or struct pointer
if field.IsNil() {
return true
}
if ContainsNil(field.Interface()) {
return true
}
}
if field.Kind() == reflect.Struct {
if ContainsNil(field.Addr()) {
return true
}
} else {
if field.IsNil() {
return true
}
if field.Interface() == nil {
return true
}
}
}
}
return false
}
关于recursion - 递归遍历嵌套结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45828996/
我是一名优秀的程序员,十分优秀!