mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
improve gutil.Keys
This commit is contained in:
@ -62,6 +62,12 @@ func Keys(mapOrStruct interface{}) (keysOrAttrs []string) {
|
||||
reflectValue = reflect.ValueOf(mapOrStruct)
|
||||
reflectKind = reflectValue.Kind()
|
||||
)
|
||||
if reflectKind == reflect.Ptr {
|
||||
if !reflectValue.IsValid() || reflectValue.IsNil() {
|
||||
reflectValue = reflect.New(reflectValue.Type().Elem()).Elem()
|
||||
reflectKind = reflectValue.Kind()
|
||||
}
|
||||
}
|
||||
for reflectKind == reflect.Ptr {
|
||||
reflectValue = reflectValue.Elem()
|
||||
reflectKind = reflectValue.Kind()
|
||||
|
||||
@ -82,6 +82,16 @@ func Test_Keys(t *testing.T) {
|
||||
keys := gutil.Keys(new(T))
|
||||
t.Assert(keys, g.SliceStr{"A", "B"})
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type T struct {
|
||||
A string
|
||||
B int
|
||||
}
|
||||
var pointer *T
|
||||
keys := gutil.Keys(pointer)
|
||||
t.Assert(keys, g.SliceStr{"A", "B"})
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Values(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user