mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
fixes saltstack/salt#64600 implement jq-esque to_entries and from_entries functions
This commit is contained in:
parent
4ac4da9aa4
commit
58b3ab13c9
3 changed files with 75 additions and 0 deletions
1
changelog/64600.added.md
Normal file
1
changelog/64600.added.md
Normal file
|
@ -0,0 +1 @@
|
|||
Add jq-esque to_entries and from_entries functions
|
|
@ -1690,3 +1690,57 @@ def shuffle(value, seed=None):
|
|||
Any value which will be hashed as a seed for random.
|
||||
"""
|
||||
return sample(value, len(value), seed=seed)
|
||||
|
||||
|
||||
def to_entries(data):
|
||||
"""
|
||||
Convert a dictionary or list into a list of key-value pairs (entries).
|
||||
|
||||
Args:
|
||||
data (dict, list): The input dictionary or list.
|
||||
|
||||
Returns:
|
||||
list: A list of dictionaries representing the key-value pairs.
|
||||
Each dictionary has 'key' and 'value' keys.
|
||||
|
||||
Example:
|
||||
data = {'a': 1, 'b': 2}
|
||||
entries = to_entries(data)
|
||||
print(entries)
|
||||
# Output: [{'key': 'a', 'value': 1}, {'key': 'b', 'value': 2}]
|
||||
"""
|
||||
if isinstance(data, dict):
|
||||
ret = [{"key": key, "value": value} for key, value in data.items()]
|
||||
elif isinstance(data, list):
|
||||
ret = [{"key": idx, "value": value} for idx, value in enumerate(data)]
|
||||
else:
|
||||
raise SaltException("Input data must be a dict or list")
|
||||
return ret
|
||||
|
||||
|
||||
def from_entries(entries):
|
||||
"""
|
||||
Convert a list of key-value pairs (entries) into a dictionary.
|
||||
|
||||
Args:
|
||||
entries (list): A list of dictionaries representing the key-value pairs.
|
||||
Each dictionary must have 'key' and 'value' keys.
|
||||
|
||||
Returns:
|
||||
dict: A dictionary constructed from the key-value pairs.
|
||||
|
||||
Example:
|
||||
entries = [{'key': 'a', 'value': 1}, {'key': 'b', 'value': 2}]
|
||||
dictionary = from_entries(entries)
|
||||
print(dictionary)
|
||||
# Output: {'a': 1, 'b': 2}
|
||||
"""
|
||||
ret = {}
|
||||
for entry in entries:
|
||||
entry = CaseInsensitiveDict(entry)
|
||||
for key in ("key", "name"):
|
||||
keyval = entry.get(key)
|
||||
if keyval:
|
||||
ret[keyval] = entry.get("value")
|
||||
break
|
||||
return ret
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import pytest
|
||||
|
||||
import salt.utils.data
|
||||
from salt.exceptions import SaltException
|
||||
|
||||
|
||||
def test_get_value_simple_path():
|
||||
|
@ -87,3 +88,22 @@ def test_shuffle():
|
|||
"three",
|
||||
"one",
|
||||
]
|
||||
|
||||
|
||||
def test_to_entries():
|
||||
data = {"a": 1, "b": 2}
|
||||
entries = [{"key": "a", "value": 1}, {"key": "b", "value": 2}]
|
||||
assert salt.utils.data.to_entries(data) == entries
|
||||
|
||||
data = ["monkey", "donkey"]
|
||||
entries = [{"key": 0, "value": "monkey"}, {"key": 1, "value": "donkey"}]
|
||||
assert salt.utils.data.to_entries(data) == entries
|
||||
|
||||
with pytest.raises(SaltException):
|
||||
salt.utils.data.to_entries("RAISE ON THIS")
|
||||
|
||||
|
||||
def test_from_entries():
|
||||
entries = [{"key": "a", "value": 1}, {"key": "b", "value": 2}]
|
||||
data = {"a": 1, "b": 2}
|
||||
assert salt.utils.data.from_entries(entries) == data
|
||||
|
|
Loading…
Add table
Reference in a new issue