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

Fix ObjectTraverser when accessing array values #1561

Merged
merged 1 commit into from
Mar 15, 2017

Conversation

adamleff
Copy link
Contributor

@adamleff adamleff commented Mar 14, 2017

When attempting to access array values via the json resource:

describe json('/tmp/test.json') do
      its(['array',0]) { should eq "zero" }
end

... the resulting data would be an array of the size of the original array
with all the values replaced with nils:

     expected: "zero"
          got: [nil, nil, nil]

This was due to a bug in the ObjectTraverser mixin that mapped array values
back through extract_value rather than properly handling the passed-in
key(s). This worked fine for the specific data format created by the csv
resource but did not work json or any other resource that subclassed the
JsonConfig resource.

This change fixes the logic when dealing with an array when it's encountered,
and fixes up the csv resource with its own value method.

This change also adds tests for ObjectTraverser.

Fixes #1560

@adamleff adamleff force-pushed the adamleff/fix-object-traverser-for-arrays branch 3 times, most recently from 8dea5bb to ea9e750 Compare March 14, 2017 20:02
Copy link
Contributor

@chris-rock chris-rock left a comment

Choose a reason for hiding this comment

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

Thank you @adamleff

end

it 'captures a hashmap of entries of a line' do
_(resource.params[0]).must_be_kind_of Hash
end

it 'gets params by header fields' do
_(resource.params[0]['addressable']).must_equal 'ast'
_(resource.params[0]['name']).must_equal 'addressable'
Copy link
Contributor

Choose a reason for hiding this comment

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

man, who wrote this in the first place :-)

@chris-rock
Copy link
Contributor

giphy 3

When attempting to access array values via the `json` resource:

```
describe json('/tmp/test.json') do
      its(['array',0]) { should eq "zero" }
end
```

... the resulting data would be an array of the size of the original array
with all the values replaced with nils:

```
     expected: "zero"
          got: [nil, nil, nil]
```

This was due to a bug in the ObjectTraverser mixin that mapped array values
back through `extract_value` rather than properly handling the passed-in
key(s). This worked fine for the specific data format created by the `csv`
resource but did not work `json` or any other resource that subclassed the
`JsonConfig` resource.

This change fixes the logic when dealing with an array when it's encountered,
and fixes up the `csv` resource with its own `value` method.

This change also adds tests for ObjectTraverser.

Signed-off-by: Adam Leff <adam@leff.co>
@chris-rock chris-rock force-pushed the adamleff/fix-object-traverser-for-arrays branch from ea9e750 to 4f2b663 Compare March 15, 2017 10:36
@chris-rock chris-rock merged commit 56986d1 into master Mar 15, 2017
@chris-rock chris-rock deleted the adamleff/fix-object-traverser-for-arrays branch March 15, 2017 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

json resource array index access not working
2 participants