Skip to content

Commit

Permalink
Create alias for certain operations
Browse files Browse the repository at this point in the history
This commit improves the name of certain operations. This change has
effect on v1beta1. However, to be able to consume configs written with
v1alph1, we're keeping the old names. The operations that now have a new
alias are:

- "and" -> "all"
- "or" -> "any"
- "forEach" -> "map"

Appart from that, we're changing the behaviour of "or" (and "any) when
they have no operands. Before, the result was true, but now it's false.
This makes this operation consistent with `any([])` in Python. The
intention is to make it's output more intuitive for Python users.
  • Loading branch information
jordi authored and jordipiqueselles committed Apr 22, 2024
1 parent 55f29e7 commit bee57f0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
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

0 comments on commit bee57f0

Please sign in to comment.