allow boolean arguments for ifelse

This commit is contained in:
nicholasmhughes 2022-08-26 10:04:16 -04:00 committed by Megan Wilhite
parent 0a32c53468
commit eab3640f21
4 changed files with 21 additions and 4 deletions

View file

@ -2320,7 +2320,9 @@ Oracle's ``DECODE`` function. It must have an odd number of arguments (from
standard programming languages. Every pair of arguments before the last one
are evaluated as a pair. If the first one evaluates true then the second one
is returned, as if you had used the first one in a compound match
expression.
expression. Boolean values can also be used as the first item in a pair, as it
will be translated to a match that will always match ("*") or never match
("SALT_IFELSE_MATCH_NOTHING") a target system.
This is essentially another way to express the ``match.filter_by`` functionality
in way that's familiar to CFEngine or Oracle users. Consider using

View file

@ -421,7 +421,9 @@ def ifelse(
standard programming languages. Every pair of arguments before the last one
are evaluated as a pair. If the first one evaluates true then the second one
is returned, as if you had used the first one in a compound match
expression.
expression. Boolean values can also be used as the first item in a pair,
as it will be translated to a match that will always match ("*") or never
match ("SALT_IFELSE_MATCH_NOTHING") a target system.
This is essentially another way to express the ``filter_by`` functionality
in way that's familiar to CFEngine or Oracle users. Consider using
@ -440,7 +442,14 @@ def ifelse(
default_key = "SALT_IFELSE_FUNCTION_DEFAULT"
lookup = dict(zip(args[::2], args[1::2]))
keys = list(args[::2])
for idx, key in enumerate(keys):
if key is True:
keys[idx] = "*"
elif key is False:
keys[idx] = "SALT_IFELSE_MATCH_NOTHING"
lookup = dict(zip(keys, args[1::2]))
lookup.update({default_key: args[-1]})
return filter_by(

View file

@ -290,3 +290,8 @@ def test_ifelse():
"key1": "default1",
"key2": "default2",
}
# boolean matchers
assert (
match.ifelse(False, "nuh uhn", True, "this is true", "default value")
== "this is true"
)

View file

@ -1250,7 +1250,8 @@ def test_ifelse(minion_opts, local_salt):
"{{ ifelse('default') }}\n"
"{{ ifelse('foo*', 'fooval', 'bar*', 'barval', 'default', minion_id='foo03') }}\n"
"{{ ifelse('foo*', 'fooval', 'bar*', 'barval', 'default', minion_id='bar03') }}\n"
"{{ ifelse(False, 'fooval', True, 'barval', 'default', minion_id='foo03') }}\n"
"{{ ifelse('foo*', 'fooval', 'bar*', 'barval', 'default', minion_id='baz03') }}",
dict(opts=minion_opts, saltenv="test", salt=local_salt),
)
assert rendered == ("default\n" "fooval\n" "barval\n" "default")
assert rendered == ("default\n" "fooval\n" "barval\n" "barval\n" "default")