improve gutil.Keys

This commit is contained in:
John
2020-11-06 21:21:09 +08:00
parent 9ae8a7ca33
commit fa8cc1d3f4
2 changed files with 16 additions and 0 deletions

View File

@ -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()

View File

@ -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) {