Skip to content

Commit

Permalink
Resolved - Generalize the function get_path_value_from_dict
Browse files Browse the repository at this point in the history
Refactor get_path_value_from_dict

Moved get_path_value_from_dict to common.py.
Refactored Kubernetes and JSON handler files to import from common.py.

Updated Common.py
  • Loading branch information
AkashS0510 committed Aug 7, 2024
1 parent dc658cf commit 13bd12d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/tirith/providers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ def create_result_dict(value=None, meta=None, err=None) -> Dict:
return dict(value=value, meta=meta, err=err)


def get_path_value_from_dict(key_path: str, input_dict: dict, get_path_value_from_dict_func):
splitted_attribute = key_path.split(".*.")
return get_path_value_from_dict_func(splitted_attribute, input_dict)


class ProviderError:
"""
A class to represent an error happening in a provider
Expand Down
10 changes: 2 additions & 8 deletions src/tirith/providers/json/handler.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import pydash

from typing import Callable, Dict, List
from ..common import create_result_dict, ProviderError
from ..common import create_result_dict, ProviderError, get_path_value_from_dict


class PydashPathNotFound:
pass


def get_path_value_from_dict(key_path, input_dict):
# TODO: Make this function more general and then move it to common.py
splitted_attribute = key_path.split(".*.")
return _get_path_value_from_dict(splitted_attribute, input_dict)


def _get_path_value_from_dict(splitted_paths, input_dict):
final_data = []
for i, expression in enumerate(splitted_paths):
Expand All @@ -38,7 +32,7 @@ def get_value(provider_args: Dict, input_data: Dict) -> List[dict]:
# Must be validated first whether the provider args are valid for this op type
key_path: str = provider_args["key_path"]

values = get_path_value_from_dict(key_path, input_data)
values = get_path_value_from_dict(key_path, input_data, _get_path_value_from_dict)

if len(values) == 0:
severity_value = 2
Expand Down
10 changes: 2 additions & 8 deletions src/tirith/providers/kubernetes/handler.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import pydash

from typing import Callable, Dict, List
from ..common import create_result_dict, ProviderError
from ..common import create_result_dict, ProviderError, get_path_value_from_dict


class PydashPathNotFound:
pass


def get_path_value_from_dict(key_path, input_dict):
# TODO: Make this function more general and then move it to common.py
splitted_attribute = key_path.split(".*.")
return _get_path_value_from_dict(splitted_attribute, input_dict)


def _get_path_value_from_dict(splitted_paths, input_dict):
final_data = []
expression = splitted_paths[0]
Expand Down Expand Up @@ -55,7 +49,7 @@ def get_value(provider_args: Dict, input_data: Dict, outputs: list) -> Dict:
if resource["kind"] != target_kind:
continue
is_kind_found = True
values = get_path_value_from_dict(attribute_path, resource)
values = get_path_value_from_dict(attribute_path, resource, _get_path_value_from_dict)
if ".*." not in attribute_path:
# If there's no * in the attribute path, the values always have 1 member
values = values[0]
Expand Down

0 comments on commit 13bd12d

Please sign in to comment.