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

Data filter falsey-master #55733

Merged
merged 1 commit into from
Dec 26, 2019

Conversation

github-abcde
Copy link
Contributor

This PR ports the changes from #52499 to master.

What does this PR do?

This adds a function filter_falsey to salt.utils.data. The function can be used to filter values from a list or dict that evaluate to false (but are not bool(False)). Optionally, a level of recursion can be set to process nested dicts/lists. Also optionally, a list of types to not filter can be passed in ignore_types.

What issues does this PR fix or reference?

None that I know of.

Previous Behavior

The functionality does not exist.

New Behavior

We are now able to filter falsey values from dicts or lists.
This is especially handy when having to process a lot of kwargs that are explicitly specified in the function argument list, which default to None, but need not be passed on if they are None.
For example:

def some_function(foo=None, bar=None, baz=None):
    new_kwargs = {}
    if foo is not None:
        new_kwargs.update('Foo': foo)
    if bar is not None:
        new_kwargs.update('Bar': bar)
    if baz is not None:
        new_kwargs.update('Baz': baz)
    some_other_function(**new_kwargs)

will become

def some_function(foo=None, bar=None, baz=None):
    new_kwargs = salt.utils.data.filter_falsey({
        'Foo': foo,
        'Bar': bar,
        'Baz': baz,
    })
    some_other_function(**new_kwargs)

Which is a typical use-case for most Boto calls.

Tests written?

Yes

Commits signed with GPG?

Yes

@github-abcde github-abcde requested a review from a team as a code owner December 24, 2019 12:21
@ghost ghost requested a review from DmitryKuzmenko December 24, 2019 12:21
@github-abcde github-abcde changed the title Port changes from PR 52499 to master Data filter falsey-master Dec 24, 2019
Copy link
Contributor

@dwoz dwoz left a comment

Choose a reason for hiding this comment

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

@github-abcde Approved, though I would like to see this broken up into two separate methods in the future. One that handles dictionaries and on that handles list like objects. filter_falsey could stick around as a convenience method.

@dwoz dwoz merged commit eecd4d0 into saltstack:master Dec 26, 2019
@github-abcde
Copy link
Contributor Author

@dwoz Do you have a preference on how those two separate methods should be called?

@github-abcde github-abcde deleted the data_filter_falsey-master branch January 2, 2020 16:20
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.

2 participants