File tree 1 file changed +43
-0
lines changed
1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 15
15
package debug
16
16
17
17
import (
18
+ "reflect"
19
+ "sort"
20
+
18
21
"github.com/sanity-io/litter"
19
22
20
23
"github.com/gohugoio/hugo/deps"
@@ -38,3 +41,43 @@ type Namespace struct {
38
41
func (ns * Namespace ) Dump (val interface {}) string {
39
42
return litter .Sdump (val )
40
43
}
44
+
45
+ // List returns the fields and methods of the struct/pointer or keys of the map.
46
+ func (ns * Namespace ) List (val interface {}) []string {
47
+ values := make ([]string , 0 )
48
+
49
+ v := reflect .ValueOf (val )
50
+
51
+ // If the type is struct
52
+ if v .Kind () == reflect .Struct {
53
+ for i := 0 ; i < v .NumField (); i ++ {
54
+ values = append (values , v .Type ().Field (i ).Name )
55
+ }
56
+
57
+ for i := 0 ; i < v .NumMethod (); i ++ {
58
+ values = append (values , v .Type ().Method (i ).Name )
59
+ }
60
+ }
61
+
62
+ // If the type is pointer
63
+ if v .Kind () == reflect .Ptr {
64
+ for i := 0 ; i < reflect .Indirect (v ).NumField (); i ++ {
65
+ values = append (values , v .Elem ().Type ().Field (i ).Name )
66
+ }
67
+
68
+ for i := 0 ; i < v .NumMethod (); i ++ {
69
+ values = append (values , v .Type ().Method (i ).Name )
70
+ }
71
+ }
72
+
73
+ // If the type is map
74
+ if v .Kind () == reflect .Map {
75
+ iter := v .MapRange ()
76
+ for iter .Next () {
77
+ values = append (values , iter .Key ().String ())
78
+ }
79
+ }
80
+
81
+ sort .Strings (values )
82
+ return values
83
+ }
You can’t perform that action at this time.
0 commit comments