Skip to content

Commit 5d2f2b7

Browse files
committed
Fixed an issue where unexported fields were being returned as a part of the result.
1 parent 51b6233 commit 5d2f2b7

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

Diff for: tpl/debug/debug.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,30 @@ func (ns *Namespace) List(val interface{}) []string {
5151
// If the type is struct
5252
if v.Kind() == reflect.Struct {
5353
for i := 0; i < v.NumField(); i++ {
54-
values = append(values, v.Type().Field(i).Name)
54+
if v.Type().Field(i).IsExported() {
55+
values = append(values, v.Type().Field(i).Name)
56+
}
5557
}
5658

5759
for i := 0; i < v.NumMethod(); i++ {
58-
values = append(values, v.Type().Method(i).Name)
60+
if v.Type().Method(i).IsExported() {
61+
values = append(values, v.Type().Method(i).Name)
62+
}
5963
}
6064
}
6165

6266
// If the type is pointer
6367
if v.Kind() == reflect.Ptr {
6468
for i := 0; i < reflect.Indirect(v).NumField(); i++ {
65-
values = append(values, v.Elem().Type().Field(i).Name)
69+
if v.Elem().Type().Field(i).IsExported() {
70+
values = append(values, v.Elem().Type().Field(i).Name)
71+
}
6672
}
6773

6874
for i := 0; i < v.NumMethod(); i++ {
69-
values = append(values, v.Type().Method(i).Name)
75+
if v.Type().Method(i).IsExported() {
76+
values = append(values, v.Type().Method(i).Name)
77+
}
7078
}
7179
}
7280

Diff for: tpl/debug/debug_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ func TestList(t *testing.T) {
3535
t.Run("struct", func(t *testing.T) {
3636
c := qt.New(t)
3737
result := ns.List(user)
38+
c.Assert(len(result), qt.Equals, 4)
3839
c.Assert(result[0], qt.Equals, "GetName")
3940
c.Assert(result[1], qt.Equals, "GetPhone")
4041
c.Assert(result[2], qt.Equals, "Name")
4142
c.Assert(result[3], qt.Equals, "Phone")
42-
c.Assert(result[4], qt.Equals, "city")
4343
})
4444

4545
t.Run("pointer", func(t *testing.T) {
4646
c := qt.New(t)
4747
result := ns.List(&user)
48+
c.Assert(len(result), qt.Equals, 5)
4849
c.Assert(result[0], qt.Equals, "GetName")
4950
c.Assert(result[1], qt.Equals, "GetPhone")
5051
c.Assert(result[2], qt.Equals, "GetPhoneAndCity")
5152
c.Assert(result[3], qt.Equals, "Name")
5253
c.Assert(result[4], qt.Equals, "Phone")
53-
c.Assert(result[5], qt.Equals, "city")
5454
})
5555

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

0 commit comments

Comments
 (0)