You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
I find uselessly difficult to manipulate multi-level dict and think that some filters may ease their manipulation and make the SLS more readable.
I usually define a dict in pillar or some yaml data file and want to filter the keys based on subkeys values, for example:
bigdict:
top1:
enabled: truedescr: this is a top level item1top2:
enabled: falsedescr: this is a top level item2
I may want:
to get the key names of bigdict where enabled is true (or sometimes false)
to get the object where enabled is true (or sometimes false)
Describe the solution you'd like
Sometimes I want only the key names:
{%- setbigdict = {'top1': {'enabled': false, 'descr': 'this is a top level item1'},
'top2': {'enabled': true, 'descr': 'this is a top level item2'}} %}
The enabled tops are {{ bigdict | FilterKeysValues("enabled") | FilterThatReturnJustTheKeyName | join(", ") }}
Sometime, I may want a subkey value:
{%- setbigdict = {'top1': {'enabled': false, 'descr': 'this is a top level item1'},
'top2': {'enabled': true, 'descr': 'this is a top level item2'}} %}
The enabled tops descriptions are:
- {{ bigdict | FilterKeysValues("enabled") | FilterThatReturnAqueryLikeTraverse(":descr") | join("\n- ") }}
The FilterThatReturnJustTheKeyName and FilterThatReturnAqueryLikeTraverse may be the same FilterTraverseLike, as an idea:
FilterTraverseLike without any argument returns {'top1': {'enabled': false, 'descr': 'this is a top level item1'}}
FilterTraverseLike(".") returns the top1 string
FilterTraverseLike(":") returns {'enabled': false, 'descr': 'this is a top level item1'}
FilterTraverseLike(":descr") returns 'this is a top level item1'
The FilterKeysValues should obviously have a proper name and may have a parameter like the FilterTraverseLike:
{%- setbigdict = {'top1': {'value': {'enabled': false, 'descr': 'this is a top level item1'}},
'top2': {'value': {'enabled': true, 'descr': 'this is a top level item2'}}} %}
The enabled tops are {{ bigdict | FilterKeysValues(":value:enabled") | FilterThatReturnAqueryLikeTraverse(".") | join(", ") }}
Describe alternatives you've considered
Actually, I'm writing quite a bunch of jinja to do it, for example:
{%- setbigdict = {'top1': {'enabled': false, 'descr': 'this is a top level item1'},
'top2': {'enabled': true, 'descr': 'this is a top level item2'}} %}{%- setfiltered = [] %}{%- fork, vinbigdict.items() %}{%- ifv.get('enabled', False) %}{%- dofiltered.append(k) %}{%- endif%}{%- endfor%}
The enabled tops are {{ filtered | join(',') }}
or
{%- setbigdict = {'top1': {'enabled': false, 'descr': 'this is a top level item1'},
'top2': {'enabled': true, 'descr': 'this is a top level item2'}} %}
The enabled tops descriptions are:
{%- fork, vinbigdict.items() %}{%- ifv.get('enabled', False) %}
- {{ v.descr }}
{%- endif%}{%- endfor%}
Instead of doing magic on first argument, the returned values could be configurable with parameters:
FilterTraverseLike without any argument returns {'top1': {'enabled': false, 'descr': 'this is a top level item1'}}
FilterTraverseLike(key=True) returns the top1 string
FilterTraverseLike(value=True) returns {'enabled': false, 'descr': 'this is a top level item1'}
FilterTraverseLike(traverse=":descr") returns 'this is a top level item1'
FilterTraverseLike(key=True, value=True) returns the tuple ('top1', {'enabled': false, 'descr': 'this is a top level item1'}) (this could be an implementation for Add a new jinja filter items #57970)
FilterTraverseLike(key=True, traverse=":descr") returns the tuple ('top1', 'this is a top level item1')
And as writing this comment I found useful to be able to do:
FilterTraverseLike(object=True) returns the object {_name: 'top1', 'enabled': false, 'descr': 'this is a top level item1'}
and this looks so promising to me that it could became a default behaviour ;-)
Is your feature request related to a problem? Please describe.
I find uselessly difficult to manipulate multi-level dict and think that some filters may ease their manipulation and make the SLS more readable.
I usually define a dict in pillar or some yaml data file and want to filter the keys based on subkeys values, for example:
I may want:
bigdict
whereenabled
istrue
(or sometimesfalse
)enabled
istrue
(or sometimesfalse
)Describe the solution you'd like
Sometimes I want only the key names:
Sometime, I may want a subkey value:
The
FilterThatReturnJustTheKeyName
andFilterThatReturnAqueryLikeTraverse
may be the sameFilterTraverseLike
, as an idea:FilterTraverseLike
without any argument returns{'top1': {'enabled': false, 'descr': 'this is a top level item1'}}
FilterTraverseLike(".")
returns thetop1
stringFilterTraverseLike(":")
returns{'enabled': false, 'descr': 'this is a top level item1'}
FilterTraverseLike(":descr")
returns'this is a top level item1'
The
FilterKeysValues
should obviously have a proper name and may have a parameter like theFilterTraverseLike
:Describe alternatives you've considered
Actually, I'm writing quite a bunch of jinja to do it, for example:
or
Additional context
items
#57970.The text was updated successfully, but these errors were encountered: