gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
76.13k stars 7.54k forks source link

Add debug.List #9148

Open bep opened 3 years ago

bep commented 3 years ago

See https://discourse.gohugo.io/t/advice-on-how-to-inspect-a-page-context/35504/4

I suggest we add a debug.List method that uses reflection to list all fields and method names on structs/pointers, or all keys if it's a map -- non-recursive; returning a string slice would be good, I think (it prints nicely).

mohsalsaleem commented 3 years ago

Hello @bep, lets assume the following is the struct

type Address {
    street string
    city     string
}
type User struct {
    name string
    address Address  
}

Then, is name, address the expected output? Or, should it get the output recursively, in this case the fields of address as well?

bep commented 3 years ago

@mohsalsaleem to be more precise:

type Address {
    Street string
    City     string
}
type User struct {
    Name string
    Address Address
    foo     string
}

func (u *User) M1() string
func (u *User) M3() string
func (u *User) M2(v string) string
func (u *User) m4(v string) string

For User, the expected output (I think) is:

["Address", "Name", "M1", "M2", "M3"]

As in, only the exported fields and methods.

The above would some less than useful entries for the end user, but so be it.

For maps list just the keys sorted (again, non-recursive).

mohsalsaleem commented 3 years ago

Thanks for the explanation @bep. I've implemented the changes in the commit https://github.com/mohsalsaleem/hugo/commit/2994a16d7c116f89f8a79f4dd03e11089aa378a1 Can you please help me with writing tests? I see that, in tpl/debug/init.go, an example needs to be added, but I'm a bit lost as to how I can structure the example.

johannesengl commented 1 year ago

@bep @mohsalsaleem I see there hasn't been any activity on this issue since November 2021. Is this issue still valid? Is there help wanted on this? I am looking for a good first issue to pickup.

johannesengl commented 1 year ago

@bep Added a PR for this issue: https://github.com/gohugoio/hugo/pull/10806 Would appreciate a review :)

shubmjagtap commented 1 year ago

Hello @bep can i work on this issue ?

cubxxw commented 1 year ago

Is this issue end-of-life?