-
Notifications
You must be signed in to change notification settings - Fork 449
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 method to safely get or check the existence of attributes #796
Conversation
c603f09
to
b1377e2
Compare
🎉 👏 👍 |
b1377e2
to
28826eb
Compare
@thommay minor change to require at least one parameter (o/w |
+1 |
Thoughts from @chef/client-core ? :) |
@mcquin good catch! |
Hmm. Should we instead try to make this match with the way we did I like the idea... but I don't dig the implementation. |
@jaymzh whoa attributes with |
Yeah, sorry, it's very early, I only read the description, and hadn't gotten to the code. Which I've now read. Minus the |
28826eb
to
348d355
Compare
Updated to remove support for |
def safe_get_attribute(*keys) | ||
attrs = @data | ||
keys.each do |key| | ||
return nil unless attrs.respond_to?(:has_key?) && attrs.has_key?(key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
array indexes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arrays don't respond to has_key?
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copied and adapted from some other code of mine (still a bit Before Caffeine for me so I may be overlooking something):
begin
keys.inject(@data) { |memo, key| memo[key] }
rescue NoMethodError
nil
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh that is so nice. It breaks in the case where some intermediate key is not a hash, though. Hm...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha rescue NoMethodError, TypeError
works
348d355
to
dc4fc04
Compare
dc4fc04
to
cc9a68d
Compare
👍 |
There is an RFC declaring |
@coderanger I thought about that. But I didn't want to mix |
Sure, but the former should be fixed, not the latter :) |
🚧 |
I think its time to argue about RFC 18 tomorrow AM: |
To be clear, I'm still 👍 on this as written. Just if we did want to support the single-string syntax it should be with |
👍 |
e34ffa4
to
71e5256
Compare
@chef/client-core can we get another set of eyes on this since there's been some changes since the initial 👍 s |
@@ -183,6 +185,18 @@ def method_missing(name, *args) | |||
|
|||
private | |||
|
|||
def safe_get_attribute(*keys) | |||
keys.inject(@data) do |attrs, key| | |||
unless attrs.nil? || attrs.is_a?(Hash) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|| attrs.is_a?(Array)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we want to allow Array access here. The idea is to prevent accessing values as subattribute keys. We expect the structure to be a Mash until we reach a value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah but Mashes turn into Arrays once you traverse them, and attrs[key]
is fine to run on an Array.
I think if you set it up and try to do something like
safe_get_attributes("etc", "passwd", "group", "wheel", "members", 0)
It'll throw a TypeError instead of giving you back "root"
(on my Mac anyway).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mashes turn into Arrays once you traverse them
Whoa, ugh. Ok.
71e5256
to
dedee01
Compare
👍 |
Resolves #794 by extending the
attribute?
andget_attribute
DSL methods to apply to sub-attributes. Attributes can be specified as a/
-delimited string, a list of string keys, a list of symbol keys, or a combination thereof.Open to suggestions like:
/
-delimiter stuff, that's too fancy)set_attribute
in this PR (was gonna do it separate since the implementation is different).