Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to concatenate strings #31

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generic_k8s_webhook/config_parser/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions generic_k8s_webhook/config_parser/expr_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
?sum: product
| sum "+" product -> add
| sum "-" product -> sub
| sum "++" product -> strconcat

?product: atom
| product "*" atom -> mul
Expand Down Expand Up @@ -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))

Expand Down
10 changes: 10 additions & 0 deletions generic_k8s_webhook/config_parser/operator_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions generic_k8s_webhook/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions tests/conditions_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
Loading