Skip to content

Commit

Permalink
feat: string interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
arjendev committed Dec 1, 2023
1 parent 9036c34 commit bd99c36
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ def literal_evaluation(self, value: list[Token, str, int, float, bool]) -> [str,
raise ExpressionEvaluationError("Literal evaluation only supports string, int, float, bool and None")
return value[0]

def literal_interpolation(self, value: list[Token, str, int, float, bool]) -> str:
result = ""
for item in value:
if type(item) not in [str, int, float, bool, None]:
raise ExpressionEvaluationError("Literal interpolation only supports string, int, float, bool and None")

result += str(item)

return result

def EXPRESSION_NULL(self, token: Token) -> Optional[None]: # noqa: N802
return None

Expand Down Expand Up @@ -242,6 +252,11 @@ def expression_evaluation(self, values: list[Token, str, int, float, bool, list]
eval_value = eval_value[array_index.value]
return eval_value

def expression_interpolation_evaluation(
self, values: list[Token, str, int, float, bool, list]
) -> [str, int, float, bool]:
return values[0]

def expression_array_indices(self, values: list[Token, str, int, float, bool]) -> Optional[list[Token]]:
if values[0] is None:
return Discard # if there are no array indices, discard the value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def __init__(self) -> None:
| LITERAL_NULL
| literal_array
literal_array: "[" literal_evaluation ("," literal_evaluation)* "]"
literal_interpolation: LITERAL_LETTER* "@{" expression_evaluation "}" LITERAL_LETTER*
// literal terminals:
LITERAL_LETTER: /[^@]+/
Expand Down Expand Up @@ -56,7 +58,7 @@ def __init__(self) -> None:
expression_linked_service_reference: "linkedService" "(" EXPRESSION_LINKED_SERVICE_NAME ")"
expression_item_reference: "item()"
expression_system_variable_reference: "pipeline" "()" "." EXPRESSION_SYSTEM_VARIABLE_NAME
// function call rules
expression_function_call: EXPRESSION_FUNCTION_NAME "(" [expression_function_parameters] ")"
expression_function_parameters: expression_parameter ("," expression_parameter )*
Expand All @@ -81,7 +83,7 @@ def __init__(self) -> None:
""" # noqa: E501

base_grammar = """
?start: ("@" expression_start) | (["@@"] literal_start)
?start: ("@" expression_start) | (["@@"] literal_start) | (literal_interpolation)
// shared custom basic data type rules:
ARRAY_INDEX: "[" /[0-9]+/ "]"
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/functions/test_expression_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,30 @@
),
id="function_call_with_nested_array_index",
),
p(
"/repos/@{pipeline().globalParameters.OpsPrincipalClientId}/",
Tree(
Token("RULE", "literal_interpolation"),
[
Token("LITERAL_LETTER", "/repos/"),
Tree(
Token("RULE", "expression_evaluation"),
[
Tree(
Token("RULE", "expression_pipeline_reference"),
[
Token("EXPRESSION_PIPELINE_PROPERTY", "globalParameters"),
Token("EXPRESSION_PARAMETER_NAME", "OpsPrincipalClientId"),
],
),
Tree(Token("RULE", "expression_array_indices"), [None]),
],
),
Token("LITERAL_LETTER", "/"),
],
),
id="string_interpolation",
),
],
)
def test_parse(expression: str, expected: Tree[Token]) -> None:
Expand Down Expand Up @@ -619,6 +643,18 @@ def test_parse(expression: str, expected: Tree[Token]) -> None:
None,
id="function_call_with_null_parameter",
),
p(
"@{pipeline().globalParameters.OpsPrincipalClientId}",
PipelineRunState(parameters=[RunParameter(RunParameterType.Global, "OpsPrincipalClientId", "dummyId")]),
"dummyId",
id="string_interpolation_with_no_surrounding_literals",
),
p(
"/Repos/@{pipeline().globalParameters.OpsPrincipalClientId}/",
PipelineRunState(parameters=[RunParameter(RunParameterType.Global, "OpsPrincipalClientId", "dummyId")]),
"/Repos/dummyId/",
id="string_interpolation_with_literals",
),
],
)
@freeze_time("2021-11-24 12:11:49.753132")
Expand Down

0 comments on commit bd99c36

Please sign in to comment.