-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add reflection-related template functions #4081
Comments
Adds KindIs, KindOf, TypeIs, TypeIsLike, and TypeOf funcs in a reflect namespace. Fixes gohugoio#4081
Adds KindIs, KindOf, TypeIs, TypeIsLike, and TypeOf funcs in a reflect namespace. Fixes gohugoio#4081
This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help. |
Oh no! Please keep this. |
I have to resort to this:
My current solution (above) is very hacky. As you see, there's no easy way to figure out if a variable is a slice/map/struct/interface. |
Proposal for
|
That sounds great, but couldn't we have a function returning the type? (similar to javascript's This means we could both "retrieve" the type and test it in conjunction with
|
@regisphilibert That would be fine too, but I would still weigh on the It would be nicer to see
vs
Also, there are only handful data Types that we care about here.. so the effort to get all those |
@regisphilibert you can do similar with printf today, e.g. As @kaushalmodi points out, you really do not care too much if it is a "slice of apples", just that it is a slice that you can iterate over, a map with keys/values etc. |
He probably meant a "getKind", again not to confuse with page kinds :) But yes, talking about the myriad of types, I am probably covering a veery small fraction of all Hugo types here ;-) |
Yeah, I in the end, all we really need is a I agree the boolean version of @kaushalmodi is cleaner and I'd happily settle for a |
I have a subset of @kaushalmodi's proposal ready. I'm omitting I'm omitting I currently have Any comments before I submit a PR? Cc: @bep |
What is |
Add a reflect namespace that offers a two boolean functions for testing if a value is a map or slice. Fixes gohugoio#4081
There has unfortunately been some pushback with adding general debugging tools to Hugo gohugoio/hugo#4081 (comment) gohugoio/hugo#3957 (comment) I've found this can make things a lot more difficult than it needs to be. There appears to be a belief that adding debugging tools will lead to people using things that are undocumented. Unfortunately in my experience I've had trouble with values that are documented but where the documentation isn't very clear. Even where debugging tools are provided the output is minified and requests to change this have so far been rejected for the same reason as above. A user has developed a theme which can work around many of these issues and add a convenient debugging partial to allow you to clearly see the contents of most variables. (see README changes for more specifics.)
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
See https://discourse.gohugo.io/t/how-to-test-if-a-variable-is-of-a-certain-type/9207.
Related to #2427.
The text was updated successfully, but these errors were encountered: