Skip to content

Commit

Permalink
Fixed an issue where unexported fields were being returned as a part …
Browse files Browse the repository at this point in the history
…of the result.
  • Loading branch information
mohsalsaleem committed Nov 13, 2021
1 parent 51b6233 commit 5d2f2b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 12 additions & 4 deletions tpl/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,30 @@ func (ns *Namespace) List(val interface{}) []string {
// If the type is struct
if v.Kind() == reflect.Struct {
for i := 0; i < v.NumField(); i++ {
values = append(values, v.Type().Field(i).Name)
if v.Type().Field(i).IsExported() {
values = append(values, v.Type().Field(i).Name)
}
}

for i := 0; i < v.NumMethod(); i++ {
values = append(values, v.Type().Method(i).Name)
if v.Type().Method(i).IsExported() {
values = append(values, v.Type().Method(i).Name)
}
}
}

// If the type is pointer
if v.Kind() == reflect.Ptr {
for i := 0; i < reflect.Indirect(v).NumField(); i++ {
values = append(values, v.Elem().Type().Field(i).Name)
if v.Elem().Type().Field(i).IsExported() {
values = append(values, v.Elem().Type().Field(i).Name)
}
}

for i := 0; i < v.NumMethod(); i++ {
values = append(values, v.Type().Method(i).Name)
if v.Type().Method(i).IsExported() {
values = append(values, v.Type().Method(i).Name)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions tpl/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ func TestList(t *testing.T) {
t.Run("struct", func(t *testing.T) {
c := qt.New(t)
result := ns.List(user)
c.Assert(len(result), qt.Equals, 4)
c.Assert(result[0], qt.Equals, "GetName")
c.Assert(result[1], qt.Equals, "GetPhone")
c.Assert(result[2], qt.Equals, "Name")
c.Assert(result[3], qt.Equals, "Phone")
c.Assert(result[4], qt.Equals, "city")
})

t.Run("pointer", func(t *testing.T) {
c := qt.New(t)
result := ns.List(&user)
c.Assert(len(result), qt.Equals, 5)
c.Assert(result[0], qt.Equals, "GetName")
c.Assert(result[1], qt.Equals, "GetPhone")
c.Assert(result[2], qt.Equals, "GetPhoneAndCity")
c.Assert(result[3], qt.Equals, "Name")
c.Assert(result[4], qt.Equals, "Phone")
c.Assert(result[5], qt.Equals, "city")
})

t.Run("map", func(t *testing.T) {
Expand Down

0 comments on commit 5d2f2b7

Please sign in to comment.