Skip to content

Commit

Permalink
Allow types to be converted to themselves
Browse files Browse the repository at this point in the history
This provides the feature requested in discussion #76.
  • Loading branch information
zeroSteiner committed Dec 3, 2023
1 parent 12132a5 commit 12fa716
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions lib/rule_engine/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,15 @@ def float_ceiling(self, value):
def float_floor(self, value):
return _float_op(value, math.floor)

@attribute('to_str', ast.DataType.FLOAT, result_type=ast.DataType.STRING)
def float_to_str(self, value):
# keep the string representations consistent for nan, inf, -inf
if value.is_nan():
return 'nan'
elif value.is_infinite():
if value.is_signed():
return '-inf'
else:
return 'inf'
return str(value)
@attribute('to_flt', ast.DataType.FLOAT, result_type=ast.DataType.FLOAT)
def float_to_flt(self, value):
return value

@attribute('to_int', ast.DataType.FLOAT, result_type=ast.DataType.FLOAT)
def float_to_int(self, value):
if not ast.is_integer_number(value):
raise errors.EvaluationError('data type mismatch (not an integer number)')
return value

@attribute('keys', ast.DataType.MAPPING, result_type=ast.DataType.ARRAY)
def mapping_keys(self, value):
Expand All @@ -256,10 +254,6 @@ def mapping_keys(self, value):
def mapping_values(self, value):
return tuple(value.values())

@attribute('to_ary', ast.DataType.SET, result_type=ast.DataType.ARRAY)
def set_to_ary(self, value):
return tuple(value)

@attribute('as_lower', ast.DataType.STRING, result_type=ast.DataType.STRING)
def string_as_lower(self, value):
return value.lower()
Expand Down Expand Up @@ -297,7 +291,25 @@ def value_is_empty(self, value):
def value_length(self, value):
return len(value)

@attribute('to_set', ast.DataType.ARRAY, ast.DataType.STRING, result_type=ast.DataType.SET)
@attribute('to_ary', ast.DataType.ARRAY, ast.DataType.SET, result_type=ast.DataType.ARRAY)
def value_to_ary(self, value):
return tuple(value)

@attribute('to_str', ast.DataType.FLOAT, ast.DataType.STRING, result_type=ast.DataType.STRING)
def value_to_str(self, value):
if isinstance(value, str):
return value
# keep the string representations consistent for nan, inf, -inf
if value.is_nan():
return 'nan'
elif value.is_infinite():
if value.is_signed():
return '-inf'
else:
return 'inf'
return str(value)

@attribute('to_set', ast.DataType.ARRAY, ast.DataType.SET, ast.DataType.STRING, result_type=ast.DataType.SET)
def value_to_set(self, value):
return set(value)

Expand Down

0 comments on commit 12fa716

Please sign in to comment.