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

added flatten filter #33102

Merged
merged 9 commits into from Dec 15, 2017
Merged

added flatten filter #33102

merged 9 commits into from Dec 15, 2017

Conversation

bcoca
Copy link
Member

@bcoca bcoca commented Nov 20, 2017

SUMMARY

filter that flattens ... much easier to use than the lookup and has a 'level' interface

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

filters/flatten

ANSIBLE VERSION
2.5

@ansibot ansibot added affects_2.5 This issue/PR affects Ansible v2.5 feature_pull_request needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Nov 20, 2017
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Nov 20, 2017
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Nov 28, 2017
@ansibot ansibot added docs_pull_request and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Dec 13, 2017
@ansibot ansibot added support:community This issue/PR relates to code supported by the Ansible community. test This PR relates to tests. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Dec 13, 2017
@ansibot ansibot added needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. labels Dec 14, 2017

{{ [3, [4, 2] ]|flatten }}

Flatten only the first level of a list (aking to the `items` lookup)::
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo? aking -> akin

elif levels is None:
ret.extend(flatten(element))
else:
break
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems wrong to me.

Consider levels=1. You decrement that to levels=0, then the if levels >= 0: branch gets taken and you recursively call flatten(element, levels=0). Here you find a nested subelement that is a list, so you check if levels: which is False, then you check if levels is None, which is also False, so you break, truncating mylist at the 1st item that happens to be a list.

Copy link
Member Author

Choose a reason for hiding this comment

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

hmm, not sure how to check that initial input in levels is non 0

Copy link
Member Author

Choose a reason for hiding this comment

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

@mgedmin should be fixed now

@ansibot ansibot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Dec 14, 2017
@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Dec 14, 2017
@bcoca bcoca merged commit 2b7799f into ansible:devel Dec 15, 2017
@bcoca bcoca deleted the flat_filter branch December 15, 2017 19:40
@ansibot ansibot added docs This issue/PR relates to or includes documentation. feature This issue/PR relates to a feature request. and removed docs_pull_request labels Mar 4, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.5 This issue/PR affects Ansible v2.5 docs This issue/PR relates to or includes documentation. feature This issue/PR relates to a feature request. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team. test This PR relates to tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants