-
Notifications
You must be signed in to change notification settings - Fork 95
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
package data | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetValueFromAny(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
MbolotSuse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name string | ||
data interface{} | ||
keys []string | ||
wantValue interface{} | ||
wantSuccess bool | ||
}{ | ||
{ | ||
name: "nil map", | ||
data: nil, | ||
keys: []string{"somekey"}, | ||
wantValue: nil, | ||
wantSuccess: false, | ||
}, | ||
{ | ||
name: "key is not in map", | ||
data: map[string]interface{}{ | ||
"realKey": "realVal", | ||
}, | ||
keys: []string{"badKey"}, | ||
wantValue: nil, | ||
wantSuccess: false, | ||
}, | ||
{ | ||
name: "key is in first level of map", | ||
data: map[string]interface{}{ | ||
"realKey": "realVal", | ||
}, | ||
keys: []string{"realKey"}, | ||
wantValue: "realVal", | ||
wantSuccess: true, | ||
}, | ||
{ | ||
name: "key is nested in map", | ||
MbolotSuse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data: map[string]interface{}{ | ||
"parent": map[string]interface{}{ | ||
"child": map[string]interface{}{ | ||
"grandchild": "someValue", | ||
}, | ||
}, | ||
}, | ||
keys: []string{"parent", "child", "grandchild"}, | ||
wantValue: "someValue", | ||
wantSuccess: true, | ||
}, | ||
{ | ||
name: "incorrected nested key", | ||
data: map[string]interface{}{ | ||
"parent": map[string]interface{}{ | ||
"child": map[string]interface{}{ | ||
"grandchild": "someValue", | ||
}, | ||
}, | ||
}, | ||
keys: []string{"parent", "grandchild", "child"}, | ||
wantValue: nil, | ||
wantSuccess: false, | ||
}, | ||
{ | ||
name: "get index of slice", | ||
data: map[string]interface{}{ | ||
"parent": map[string]interface{}{ | ||
"children": []interface{}{ | ||
"alice", | ||
"bob", | ||
"eve", | ||
}, | ||
}, | ||
}, | ||
keys: []string{"parent", "children", "2"}, | ||
wantValue: "eve", | ||
wantSuccess: true, | ||
}, | ||
{ | ||
name: "get index of top level slice", | ||
data: []interface{}{ | ||
"alice", | ||
"bob", | ||
"eve", | ||
}, | ||
keys: []string{"2"}, | ||
wantValue: "eve", | ||
wantSuccess: true, | ||
}, | ||
{ | ||
name: "slice of maps", | ||
data: []interface{}{ | ||
map[string]interface{}{ | ||
"notthisone": "val", | ||
}, | ||
map[string]interface{}{ | ||
"parent": map[string]interface{}{ | ||
"children": []interface{}{ | ||
"alice", | ||
"bob", | ||
"eve", | ||
}, | ||
}, | ||
}, | ||
}, | ||
keys: []string{"1", "parent", "children", "0"}, | ||
wantValue: "alice", | ||
wantSuccess: true, | ||
}, | ||
{ | ||
name: "index is too big", | ||
data: map[string]interface{}{ | ||
"parent": map[string]interface{}{ | ||
"children": []interface{}{ | ||
"alice", | ||
"bob", | ||
"eve", | ||
}, | ||
}, | ||
}, | ||
keys: []string{"parent", "children", "3"}, | ||
wantValue: nil, | ||
wantSuccess: false, | ||
}, | ||
{ | ||
name: "index is negative", | ||
data: map[string]interface{}{ | ||
"parent": map[string]interface{}{ | ||
"children": []interface{}{ | ||
"alice", | ||
"bob", | ||
"eve", | ||
}, | ||
}, | ||
}, | ||
keys: []string{"parent", "children", "-3"}, | ||
wantValue: nil, | ||
wantSuccess: false, | ||
}, | ||
{ | ||
name: "index not parseable to int", | ||
data: map[string]interface{}{ | ||
"parent": map[string]interface{}{ | ||
"children": []interface{}{ | ||
"alice", | ||
"bob", | ||
"eve", | ||
}, | ||
}, | ||
}, | ||
keys: []string{"parent", "children", "notanint"}, | ||
wantValue: nil, | ||
wantSuccess: false, | ||
}, | ||
{ | ||
name: "slice blank index", | ||
data: []interface{}{ | ||
"bob", | ||
}, | ||
keys: []string{""}, | ||
wantValue: nil, | ||
wantSuccess: false, | ||
}, | ||
{ | ||
name: "slice no index", | ||
data: []interface{}{ | ||
"bob", | ||
}, | ||
wantValue: nil, | ||
wantSuccess: false, | ||
}, | ||
{ | ||
name: "keys nested too far", | ||
data: []interface{}{ | ||
"alice", | ||
"bob", | ||
"eve", | ||
}, | ||
keys: []string{"2", "1"}, | ||
wantValue: nil, | ||
wantSuccess: false, | ||
}, | ||
{ | ||
name: "map blank key with value", | ||
data: map[string]interface{}{ | ||
"": "bob", | ||
}, | ||
keys: []string{""}, | ||
wantValue: "bob", | ||
wantSuccess: true, | ||
}, | ||
{ | ||
name: "map blank key no value", | ||
data: map[string]interface{}{ | ||
"alice": "bob", | ||
}, | ||
keys: []string{""}, | ||
wantValue: nil, | ||
wantSuccess: false, | ||
}, | ||
{ | ||
name: "map no key", | ||
data: map[string]interface{}{ | ||
"": "bob", | ||
}, | ||
wantValue: nil, | ||
wantSuccess: false, | ||
}, | ||
} | ||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
MbolotSuse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Parallel() | ||
gotValue, gotSuccess := GetValueFromAny(test.data, test.keys...) | ||
assert.Equal(t, test.wantValue, gotValue) | ||
assert.Equal(t, test.wantSuccess, gotSuccess) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 beany
and validate that they'restring
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.