diff --git a/lambda_function/port/entities.py b/lambda_function/port/entities.py index d4583ad..4cb80fc 100644 --- a/lambda_function/port/entities.py +++ b/lambda_function/port/entities.py @@ -32,11 +32,6 @@ def create_entities_json( def run_jq_query(jq_query): return jq.first(jq_query, resource_object) - def raise_missing_exception(missing_field, mapping): - raise Exception( - f"Missing required field value for entity, field: {missing_field}, mapping: {mapping.get(missing_field)}" - ) - def dedup_list(lst): return [dict(tup) for tup in {tuple(obj.items()) for obj in lst}] @@ -55,34 +50,54 @@ def dedup_list(lst): if selector_jq_query and not run_jq_query(selector_jq_query): return [] - return [ - { - k: v - for k, v in { - "identifier": run_jq_query(mapping.get("identifier", "null")) - or raise_missing_exception("identifier", mapping), - "title": run_jq_query(mapping.get("title", "null")) - if mapping.get("title") - else None, - "blueprint": mapping.get("blueprint", "").strip('"') - or raise_missing_exception("blueprint", mapping), - "icon": run_jq_query(mapping.get("icon", "null")) - if mapping.get("icon") - else None, - "team": run_jq_query(mapping.get("team", "null")) - if mapping.get("team") - else None, - "properties": { - prop_key: run_jq_query(prop_val) - for prop_key, prop_val in mapping.get("properties", {}).items() - }, - "relations": { - rel_key: run_jq_query(rel_val) - for rel_key, rel_val in mapping.get("relations", {}).items() - } - or None, - }.items() - if v is not None - } - for mapping in jq_mappings - ] + entities = [] + for mapping in jq_mappings: + items_to_parse = mapping.get('itemsToParse') + if items_to_parse: + items = run_jq_query(items_to_parse) + items = items if isinstance(items, list) else [] + for item in items: + entities.append(create_upsert_entity_json(mapping, resource_object | {'item': item})) + else: + entities.append(create_upsert_entity_json(mapping, resource_object)) + + return entities + + +def create_upsert_entity_json(mapping, resource_object): + def run_jq_query(jq_query): + return jq.first(jq_query, resource_object) + + return { + k: v + for k, v in { + "identifier": run_jq_query(mapping.get("identifier", "null")) + or raise_missing_exception("identifier", mapping), + "title": run_jq_query(mapping.get("title", "null")) + if mapping.get("title") + else None, + "blueprint": mapping.get("blueprint", "").strip('"') + or raise_missing_exception("blueprint", mapping), + "icon": run_jq_query(mapping.get("icon", "null")) + if mapping.get("icon") + else None, + "team": run_jq_query(mapping.get("team", "null")) + if mapping.get("team") + else None, + "properties": { + prop_key: run_jq_query(prop_val) + for prop_key, prop_val in mapping.get("properties", {}).items() + }, + "relations": { + rel_key: run_jq_query(rel_val) + for rel_key, rel_val in mapping.get("relations", {}).items() + } + or None, + }.items() + if v is not None + } + +def raise_missing_exception(missing_field, mapping): + raise Exception( + f"Missing required field value for entity, field: {missing_field}, mapping: {mapping.get(missing_field)}" + )