Skip to content
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

tpl: Add reflection functions #2427

Closed
wants to merge 1 commit into from

Conversation

anagio
Copy link
Contributor

@anagio anagio commented Sep 10, 2016

This adds template functions to check types. Originally from https://github.com/Masterminds/sprig#reflection

typeOf: Takes an interface and returns a string representation of the type. For pointers, this will return a type prefixed with an asterisk(*). So a pointer to type Foo will be *Foo.

typeIs: Compares an interface with a string name, and returns true if they match. Note that a pointer will not match a reference. For example *Foo will not match Foo.

typeIsLike: returns true if the interface is of the given type, or is a pointer to the given type.

kindOf: Takes an interface and returns a string representation of its kind.

kindIs: Returns true if the given string matches the kind of the given interface.

@anagio
Copy link
Contributor Author

anagio commented Sep 10, 2016

I'll update the docs once this is reviewed and before merged

@moorereason
Copy link
Contributor

@anagio, I'm curious what your use case is for these. Are you just adding function to add functions or is this useful for you?

@anagio
Copy link
Contributor Author

anagio commented Sep 10, 2016

@moorereason these are useful to me and i'm sure will be to others. I have a data file (JSON) where a property in each object is usually a string but in some cases it may store another object. While looping over this data file these functions will help determine if I have a string or map returned.

@anagio
Copy link
Contributor Author

anagio commented Sep 10, 2016

Thanks I'll update the docs soon

@moorereason
Copy link
Contributor

Please update your commit message subject to following the contributing guide.

tpl: Add reflection functions

@anagio anagio changed the title add reflection functions tpl: Add reflection functions Sep 10, 2016
@anagio
Copy link
Contributor Author

anagio commented Sep 10, 2016

@moorereason updated title, and squashed commits

@moorereason
Copy link
Contributor

@anagio, you're close. I meant for you to update your commit message subject, not the PR title.


### typeOf

Takes an interface and returns a string representation of the type. For pointers, this will return a type prefixed with an asterisk(*). So a pointer to type Foo will be *Foo.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use backticks to keep the markdown from thinking you mean to use italics around the asterisks. For example,

with an asterisk(`*`).  So a pointer to a type `Foo` will be `*Foo`.

@anagio
Copy link
Contributor Author

anagio commented Sep 10, 2016

@moorereason thanks just made those updates


### typeIs

Compares an interface with a string name, and returns true if they match. Note that a pointer will not match a reference. For example `*`Foo will not match Foo.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be:

For example `*Foo` will not match `Foo`.

@moorereason
Copy link
Contributor

@anagio, it would be helpful to add example usages for all functions.

@anagio
Copy link
Contributor Author

anagio commented Sep 12, 2016

@moorereason docs are updated with an example of each function

return defaultT(translationID, args...)
if translated := defaultT(translationID, args...); translated != translationID {
return translated
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't include this in your commit.

@anagio
Copy link
Contributor Author

anagio commented Sep 12, 2016

@moorereason sorry about that it's removed

@moorereason
Copy link
Contributor

LGTM

@anagio
Copy link
Contributor Author

anagio commented Sep 28, 2016

Can this get merged in to make the release of 0.17 ?

@kaushalmodi
Copy link
Contributor

Hello @anagio. Thanks for this PR. This PR was referenced in this discussion. I need what you have as kindIs in your PR for use in templates/partials.

What was the blocker for merging this?

@anagio
Copy link
Contributor Author

anagio commented Nov 14, 2017

@kaushalmodi I was not able to merge it on my own. moorereason said LGTM but it was just never merged. Ping him for more info

@kaushalmodi
Copy link
Contributor

@anagio Well you should have pinged him a reminder to merge this. If you reopen this PR, may be @moorereason can take further steps.

@anagio
Copy link
Contributor Author

anagio commented Nov 14, 2017

I really didn't feel the need to ping anyone about merging it

@kaushalmodi
Copy link
Contributor

kaushalmodi commented Nov 14, 2017

Alright. Would you like to reopen this PR and wait for it to be merged? Or should I just open a new PR? I just wanted to give credits where they were due by having your commit merged if feasible.

@moorereason
Copy link
Contributor

We didn't merge because it would have permanently added some rather esoteric funcs to the global func map. Now that we have template func namespacing, we can revisit this. A lot has changed since this PR, so it would be better to start a new one.

@anagio
Copy link
Contributor Author

anagio commented Nov 14, 2017

@kaushalmodi feel free to submit a new PR

@github-actions
Copy link

github-actions bot commented Feb 8, 2022

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants