Skip to content

Commit

Permalink
Ability to concatenate strings
Browse files Browse the repository at this point in the history
The operand is "++". Example:

```
"foo" ++ "_" ++ "bar"
```

The result will be "foo_bar"
  • Loading branch information
jordipiqueselles committed May 4, 2024
1 parent 5d21f87 commit 63a2b49
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
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

0 comments on commit 63a2b49

Please sign in to comment.