-
Notifications
You must be signed in to change notification settings - Fork 91
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 support for getting array index in GetValue #321
Conversation
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.
LGTM, one question for my personal understanding
408ec3f
to
8d19cca
Compare
b470879
to
ac940a2
Compare
ac940a2
to
1861bed
Compare
Add the ability to use an index number to get an item out of a slice. The number will still be a string representation of an integer. For example, to get the last element from a nested slice like: ``` data := map[string]interface{}{ "data": []interface{}{ "first", "second", "third", }, } ``` Use keys like ``` val := GetValues(data, "data", "2") ```
1861bed
to
25bd1fc
Compare
25bd1fc
to
c29289d
Compare
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.
A few doc suggestions and additional test cases.
c29289d
to
32ba02d
Compare
32ba02d
to
1592712
Compare
Adds package comments and extra test cases
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.
@tomleb pointed out that although the function is technically backward compatible, the signature no longer is. To make this backward compatible and not force a release of wrangler v3.0, we will need to keep the interface compatible. I'll do this by making the current GetValue
in this PR a new function, and keeping the original function consistent.
ebe40d5
to
09dc61c
Compare
// For a map, a key denotes the key in the map whose value we want to retrieve. | ||
// For the slice, it denotes the index (starting at 0) of the value we want to retrieve. | ||
// Returns the retrieved value (if any) and a bool indicating if the value was found. | ||
func GetValueFromAny(data interface{}, keys ...string) (interface{}, bool) { |
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.
Wonder whether it would be a good idea to allows keys
to be any
and validate that they're string
or a number..
This way we could be sure that -1
passed in expected a list vs "-1"
which would expect a map.
This does add more complexity with this function like what to return if you receive an unexpected type, etc. So I think it's okay as is.
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 think that the way this function is used in Steve is that keys are essentially user provided input. So if we wen that direction Steve would need to do some selective parsing there, which I don't think is desirable.
That being said, I think that you are correct that the underlying library really should be more specific than this. I tried to implement a more comprehensive approach that (IIRC) did what you suggested in #325 (though I doubt that will merge). I'd say if we want an improvement like that we should consider a rewrite like that Pr looks for.
Restores GetValue to it's original interface to avoid breaking changes.
09dc61c
to
53fba45
Compare
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.
LGTM. I think we could add tests for the GetValue
function now that it's not relying on GetValueFromAny
, but up to you.
Add the ability to use an index number to get an item out of a slice.
The number will still be a string representation of an integer. For
example, to get the last element from a nested slice like:
Use keys like
rancher/rancher#42767