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

Create alias for certain operations #28

Merged
merged 1 commit into from
Apr 22, 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
3 changes: 3 additions & 0 deletions generic_k8s_webhook/config_parser/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,15 @@ def _parse_v1beta1(self, raw_list_webhook_config: dict) -> list[Webhook]:
meta_op_parser=op_parser.MetaOperatorParser(
list_op_parser_classes=[
op_parser.AndParser,
op_parser.AllParser,
op_parser.OrParser,
op_parser.AnyParser,
op_parser.EqualParser,
op_parser.SumParser,
op_parser.NotParser,
op_parser.ListParser,
op_parser.ForEachParser,
op_parser.MapParser,
op_parser.ContainParser,
op_parser.ConstParser,
op_parser.GetValueParser,
Expand Down
42 changes: 42 additions & 0 deletions generic_k8s_webhook/config_parser/operator_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def get_operator_cls(cls) -> operators.BinaryOp:


class AndParser(BinaryOpParser):
"""
Deprecated. Use "all" instead
"""

@classmethod
def get_name(cls) -> str:
return "and"
Expand All @@ -109,7 +113,21 @@ def get_operator_cls(cls) -> operators.BinaryOp:
return operators.And


class AllParser(AndParser):
"""
Just an alias for "and". In the future, we'll deprecate "and" in favour of "all"
"""

@classmethod
def get_name(cls) -> str:
return "all"


class OrParser(BinaryOpParser):
"""
Deprecated. Use "any" instead
"""

@classmethod
def get_name(cls) -> str:
return "or"
Expand All @@ -119,6 +137,16 @@ def get_operator_cls(cls) -> operators.BinaryOp:
return operators.Or


class AnyParser(OrParser):
"""
Just an alias for "or". In the future, we'll deprecate "or" in favour of "all"
"""

@classmethod
def get_name(cls) -> str:
return "any"


class EqualParser(BinaryOpParser):
@classmethod
def get_name(cls) -> str:
Expand Down Expand Up @@ -180,6 +208,10 @@ def parse(self, op_inputs: dict | list, path_op: str) -> operators.List:


class ForEachParser(OperatorParser):
"""
Deprecated. Use "map" instead of "forEach"
"""

@classmethod
def get_name(cls) -> str:
return "forEach"
Expand All @@ -197,6 +229,16 @@ def parse(self, op_inputs: dict | list, path_op: str) -> operators.ForEach:
raise ParsingException(f"Error when parsing {path_op}") from e


class MapParser(ForEachParser):
"""
It's an alias of ForEachParser. In the future, we'll deprecate "forEach" in favour of "map"
"""

@classmethod
def get_name(cls) -> str:
return "map"


class ContainParser(OperatorParser):
@classmethod
def get_name(cls) -> str:
Expand Down
4 changes: 3 additions & 1 deletion generic_k8s_webhook/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def _op(self, lhs, rhs):
return lhs and rhs

def _zero_args_result(self):
# This follows the default behaviour in Python when executing `all([])`
return True


Expand All @@ -118,7 +119,8 @@ def _op(self, lhs, rhs):
return lhs or rhs

def _zero_args_result(self):
return True
# This follows the default behaviour in Python when executing `any([])`
return False


class ArithOp(BinaryOp):
Expand Down
37 changes: 37 additions & 0 deletions tests/conditions_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ test_suites:
- condition:
and: []
expected_result: true
# Just check we can parse "all", since it's the same as "and"
- name: ALL
tests:
- schemas: [v1beta1]
cases:
- condition:
all:
- const: true
- const: true
expected_result: true
- name: OR
tests:
- schemas: [v1alpha1]
Expand All @@ -49,6 +59,19 @@ test_suites:
or:
- const: true
expected_result: true
- condition:
or: []
expected_result: false
# Just check we can parse "any", since it's the same as "or"
- name: ANY
tests:
- schemas: [v1beta1]
cases:
- condition:
any:
- const: false
- const: false
expected_result: false
- name: NOT
tests:
- schemas: [v1alpha1]
Expand Down Expand Up @@ -143,6 +166,20 @@ test_suites:
- maxCPU: 1
- maxCPU: 2
expected_result: [2, 3]
# Just check we can parse "map", since it's the same as "forEach"
- name: MAP
tests:
- schemas: [v1beta1]
cases:
- condition:
map:
elements:
const: [1, 2]
op:
sum:
- const: 10
- getValue: "."
expected_result: [11, 12]
- name: CONTAIN
tests:
- schemas: [v1alpha1]
Expand Down
Loading