From 5d2f2b76e8950b682fa35fc06aee20dbef97e06a Mon Sep 17 00:00:00 2001 From: Mohamed Saleem Date: Sat, 13 Nov 2021 21:26:08 +0530 Subject: [PATCH] Fixed an issue where unexported fields were being returned as a part of the result. --- tpl/debug/debug.go | 16 ++++++++++++---- tpl/debug/debug_test.go | 4 ++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/tpl/debug/debug.go b/tpl/debug/debug.go index e6e780e199f..e2d34e7565a 100644 --- a/tpl/debug/debug.go +++ b/tpl/debug/debug.go @@ -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) + } } } diff --git a/tpl/debug/debug_test.go b/tpl/debug/debug_test.go index 33bd9c482e4..3a8a7dac368 100644 --- a/tpl/debug/debug_test.go +++ b/tpl/debug/debug_test.go @@ -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) {