Add Dumas JSON array iterator node
This commit is contained in:
@@ -86,6 +86,18 @@ def _merge_json_objects(base_object, overlay_object):
|
||||
return merged
|
||||
|
||||
|
||||
def _resolve_iteration_index(index, mode, total_items):
|
||||
if total_items <= 0:
|
||||
return -1
|
||||
|
||||
if mode == "incr":
|
||||
index += 1
|
||||
elif mode == "decr":
|
||||
index -= 1
|
||||
|
||||
return max(0, min(index, total_items - 1))
|
||||
|
||||
|
||||
class DumasJSONStringToObjectNode:
|
||||
CATEGORY = "Dumas/JSON"
|
||||
RETURN_TYPES = ("JSON",)
|
||||
@@ -230,6 +242,34 @@ class DumasJSONArrayLengthNode:
|
||||
return (len(json_array),)
|
||||
|
||||
|
||||
class DumasJSONArrayIteratorNode:
|
||||
CATEGORY = "Dumas/JSON"
|
||||
RETURN_TYPES = ("JSON", "INT", "INT")
|
||||
RETURN_NAMES = ("item", "current_index", "total_items")
|
||||
FUNCTION = "iterate"
|
||||
|
||||
@classmethod
|
||||
def INPUT_TYPES(cls):
|
||||
return {
|
||||
"required": {
|
||||
"json_input": ("JSON",),
|
||||
"index": ("INT", {"default": 0, "min": 0, "step": 1}),
|
||||
"mode": (["fixed", "incr", "decr"], {"default": "fixed"}),
|
||||
}
|
||||
}
|
||||
|
||||
def iterate(self, json_input, index, mode):
|
||||
if not isinstance(json_input, list):
|
||||
return (None, -1, 0)
|
||||
|
||||
total_items = len(json_input)
|
||||
if total_items == 0:
|
||||
return (None, -1, 0)
|
||||
|
||||
current_index = _resolve_iteration_index(index, mode, total_items)
|
||||
return (json_input[current_index], current_index, total_items)
|
||||
|
||||
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"DumasJSONStringToObject": DumasJSONStringToObjectNode,
|
||||
"DumasJSONObjectToString": DumasJSONObjectToStringNode,
|
||||
@@ -238,6 +278,7 @@ NODE_CLASS_MAPPINGS = {
|
||||
"DumasJSONMergeObjects": DumasJSONMergeObjectsNode,
|
||||
"DumasJSONKeys": DumasJSONKeysNode,
|
||||
"DumasJSONArrayLength": DumasJSONArrayLengthNode,
|
||||
"DumasJSONArrayIterator": DumasJSONArrayIteratorNode,
|
||||
}
|
||||
|
||||
NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
@@ -248,4 +289,5 @@ NODE_DISPLAY_NAME_MAPPINGS = {
|
||||
"DumasJSONMergeObjects": "Dumas JSON Merge Objects",
|
||||
"DumasJSONKeys": "Dumas JSON Keys",
|
||||
"DumasJSONArrayLength": "Dumas JSON Array Length",
|
||||
"DumasJSONArrayIterator": "Dumas JSON Array Iterator",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user