diff --git a/generic_k8s_webhook/config_parser/entrypoint.py b/generic_k8s_webhook/config_parser/entrypoint.py index 6344922..a2a417a 100644 --- a/generic_k8s_webhook/config_parser/entrypoint.py +++ b/generic_k8s_webhook/config_parser/entrypoint.py @@ -105,6 +105,7 @@ def _parse_v1beta1(self, raw_list_webhook_config: dict) -> list[Webhook]: op_parser.AnyParser, op_parser.EqualParser, op_parser.SumParser, + op_parser.StrConcatParser, op_parser.NotParser, op_parser.ListParser, op_parser.ForEachParser, diff --git a/generic_k8s_webhook/config_parser/expr_parser.py b/generic_k8s_webhook/config_parser/expr_parser.py index b0edce1..9645f66 100644 --- a/generic_k8s_webhook/config_parser/expr_parser.py +++ b/generic_k8s_webhook/config_parser/expr_parser.py @@ -37,6 +37,7 @@ ?sum: product | sum "+" product -> add | sum "-" product -> sub + | sum "++" product -> strconcat ?product: atom | product "*" atom -> mul @@ -97,6 +98,9 @@ def add(self, items): def sub(self, items): return op.Sub(op.List(items)) + def strconcat(self, items): + return op.StrConcat(op.List(items)) + def mul(self, items): return op.Mul(op.List(items)) diff --git a/generic_k8s_webhook/config_parser/operator_parser.py b/generic_k8s_webhook/config_parser/operator_parser.py index 5cabedc..c379c81 100644 --- a/generic_k8s_webhook/config_parser/operator_parser.py +++ b/generic_k8s_webhook/config_parser/operator_parser.py @@ -167,6 +167,16 @@ def get_operator_cls(cls) -> operators.BinaryOp: return operators.Sum +class StrConcatParser(BinaryOpParser): + @classmethod + def get_name(cls) -> str: + return "strconcat" + + @classmethod + def get_operator_cls(cls) -> operators.BinaryOp: + return operators.StrConcat + + class UnaryOpParser(OperatorParser): def parse(self, op_inputs: dict | list, path_op: str) -> operators.UnaryOp: arg = self.meta_op_parser.parse(op_inputs, path_op) diff --git a/generic_k8s_webhook/operators.py b/generic_k8s_webhook/operators.py index 4c03b3a..de4168c 100644 --- a/generic_k8s_webhook/operators.py +++ b/generic_k8s_webhook/operators.py @@ -154,6 +154,20 @@ def _op(self, lhs, rhs): return lhs / rhs +class StrConcat(BinaryOp): + def input_type(self) -> type | None: + return list[str] + + def return_type(self) -> type | None: + return str + + def _zero_args_result(self) -> str: + return "" + + def _op(self, lhs, rhs): + return lhs + rhs + + class Comp(BinaryOp): def get_value(self, contexts: list) -> Any: list_arg_values = self.args.get_value(contexts) diff --git a/tests/conditions_test.yaml b/tests/conditions_test.yaml index cce40a0..f34963d 100644 --- a/tests/conditions_test.yaml +++ b/tests/conditions_test.yaml @@ -116,6 +116,16 @@ test_suites: sum: - const: 2 expected_result: 2 + - name: STRCONCAT + tests: + - schemas: [v1beta1] + cases: + - condition: + strconcat: + - const: "foo" + - const: "_" + - const: "bar" + expected_result: foo_bar - name: GET_VALUE tests: - schemas: [v1alpha1] @@ -274,6 +284,12 @@ test_suites: - maxCPU: 1 - maxCPU: 2 expected_result: true + - condition: '"prefix" ++ "-" ++ "suffix"' + expected_result: prefix-suffix + - condition: '"prefix-" ++ .name' + context: + - name: my-name + expected_result: prefix-my-name - name: LIST_FILTER_MAP_EXPR tests: - schemas: [v1beta1]