Skip to content

Commit

Permalink
feat: modify logic of global variables for apply int value
Browse files Browse the repository at this point in the history
Signed-off-by: seolmin <[email protected]>
  • Loading branch information
stat-kwon committed Nov 5, 2024
1 parent 37986c0 commit 0c9c1c5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
14 changes: 13 additions & 1 deletion src/spaceone/dashboard/manager/data_table_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,21 @@ def remove_jinja_braces(expression: str) -> Union[str, float, list]:
return re.sub(r"{{\s*(\w+)\s*}}", r"\1", expression)
elif re.match(r"{{\s*(\d+(\.\d+)?)\s*}}", expression):
result = re.sub(r"{{\s*(\d+(\.\d+)?)\s*}}", r"\1", expression)
return float(result)
try:
return float(result)
except ValueError:
return eval(result)
else:
expression = expression.replace("{{", "").replace("}}", "").strip()

pattern = r"\[\d+\]$"
if re.search(pattern, expression):
dict_expression, _ = re.subn(pattern, "", expression)
index_str = expression.replace(dict_expression, "").strip()
index_str = index_str.replace("[", "").replace("]", "")
index = int(index_str)
return ast.literal_eval(dict_expression)[index]

return ast.literal_eval(expression)

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,25 +324,26 @@ def evaluate_data_table(
for key in self.label_keys:
template_vars[key] = f"`{key}`"

try:
value_expression = value_expression.format(**template_vars)
except Exception as e:
raise ERROR_INVALID_PARAMETER(
key="options.EVAL.expressions.expression",
reason=f"Invalid expression: (template_var={e})",
)

if field_type not in ["DATA", "LABEL"]:
raise ERROR_INVALID_PARAMETER(
key="options.EVAL.expressions.field_type",
reason=f"Invalid field type: {field_type}",
)

if "@" in value_expression:
raise ERROR_INVALID_PARAMETER(
key="options.EVAL.expressions",
reason="It should not have '@' symbol.",
)
if isinstance(value_expression, str):
try:
value_expression = value_expression.format(**template_vars)
except Exception as e:
raise ERROR_INVALID_PARAMETER(
key="options.EVAL.expressions.expression",
reason=f"Invalid expression: (template_var={e})",
)

if "@" in value_expression:
raise ERROR_INVALID_PARAMETER(
key="options.EVAL.expressions",
reason="It should not have '@' symbol.",
)

try:
merged_expr = f"`{name}` = {value_expression}"
Expand Down

0 comments on commit 0c9c1c5

Please sign in to comment.