Hello team,
I’ve been using this plugin a lot and it’s extremely useful to manipulate data easily.
The issues here are;
- The documentation is missing for the “input” part for “update” function.
- And update works with updating single key.
Because of the need to update multiple keys with this plugin, I suggest the following code change;
import komand
from .schema import UpdateInput, UpdateOutput
# Custom imports below
class Update(komand.Action):
def __init__(self):
super(self.__class__, self).__init__(
name="update",
description="Update value by key",
input=UpdateInput(),
output=UpdateOutput(),
)
def run(json, params=[]):
if isinstance(json, dict):
new_dict = dict(json)
for update in params:
update_key = update.get("key")
update_value = update.get("value")
if update_key in new_dict:
new_dict[update_key] = update_value
return {"json": [new_dict]}
if isinstance(json, list):
l = []
for d in json:
new_dict = dict(d)
for update in params:
update_key = update.get("key")
update_value = update.get("value")
if update_key in new_dict:
new_dict[update_key] = update_value
l.append(new_dict)
return {"json": l}
return {"json": [{}]}
Please note that I didn’t test the code fully, but the logic behind it is clear.