Skip to content

Commit 63a2b49

Browse files
Ability to concatenate strings
The operand is "++". Example: ``` "foo" ++ "_" ++ "bar" ``` The result will be "foo_bar"
1 parent 5d21f87 commit 63a2b49

File tree

5 files changed

+45
-0
lines changed

5 files changed

+45
-0
lines changed

generic_k8s_webhook/config_parser/entrypoint.py

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def _parse_v1beta1(self, raw_list_webhook_config: dict) -> list[Webhook]:
105105
op_parser.AnyParser,
106106
op_parser.EqualParser,
107107
op_parser.SumParser,
108+
op_parser.StrConcatParser,
108109
op_parser.NotParser,
109110
op_parser.ListParser,
110111
op_parser.ForEachParser,

generic_k8s_webhook/config_parser/expr_parser.py

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
?sum: product
3838
| sum "+" product -> add
3939
| sum "-" product -> sub
40+
| sum "++" product -> strconcat
4041
4142
?product: atom
4243
| product "*" atom -> mul
@@ -97,6 +98,9 @@ def add(self, items):
9798
def sub(self, items):
9899
return op.Sub(op.List(items))
99100

101+
def strconcat(self, items):
102+
return op.StrConcat(op.List(items))
103+
100104
def mul(self, items):
101105
return op.Mul(op.List(items))
102106

generic_k8s_webhook/config_parser/operator_parser.py

+10
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ def get_operator_cls(cls) -> operators.BinaryOp:
167167
return operators.Sum
168168

169169

170+
class StrConcatParser(BinaryOpParser):
171+
@classmethod
172+
def get_name(cls) -> str:
173+
return "strconcat"
174+
175+
@classmethod
176+
def get_operator_cls(cls) -> operators.BinaryOp:
177+
return operators.StrConcat
178+
179+
170180
class UnaryOpParser(OperatorParser):
171181
def parse(self, op_inputs: dict | list, path_op: str) -> operators.UnaryOp:
172182
arg = self.meta_op_parser.parse(op_inputs, path_op)

generic_k8s_webhook/operators.py

+14
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ def _op(self, lhs, rhs):
154154
return lhs / rhs
155155

156156

157+
class StrConcat(BinaryOp):
158+
def input_type(self) -> type | None:
159+
return list[str]
160+
161+
def return_type(self) -> type | None:
162+
return str
163+
164+
def _zero_args_result(self) -> str:
165+
return ""
166+
167+
def _op(self, lhs, rhs):
168+
return lhs + rhs
169+
170+
157171
class Comp(BinaryOp):
158172
def get_value(self, contexts: list) -> Any:
159173
list_arg_values = self.args.get_value(contexts)

tests/conditions_test.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ test_suites:
116116
sum:
117117
- const: 2
118118
expected_result: 2
119+
- name: STRCONCAT
120+
tests:
121+
- schemas: [v1beta1]
122+
cases:
123+
- condition:
124+
strconcat:
125+
- const: "foo"
126+
- const: "_"
127+
- const: "bar"
128+
expected_result: foo_bar
119129
- name: GET_VALUE
120130
tests:
121131
- schemas: [v1alpha1]
@@ -274,6 +284,12 @@ test_suites:
274284
- maxCPU: 1
275285
- maxCPU: 2
276286
expected_result: true
287+
- condition: '"prefix" ++ "-" ++ "suffix"'
288+
expected_result: prefix-suffix
289+
- condition: '"prefix-" ++ .name'
290+
context:
291+
- name: my-name
292+
expected_result: prefix-my-name
277293
- name: LIST_FILTER_MAP_EXPR
278294
tests:
279295
- schemas: [v1beta1]

0 commit comments

Comments
 (0)