Skip to content

Commit

Permalink
Update equality, equivalence
Browse files Browse the repository at this point in the history
  • Loading branch information
atuonufure committed Dec 22, 2023
1 parent e7ae9ab commit ed98d6f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
12 changes: 9 additions & 3 deletions fhirpathpy/engine/invocations/equality.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ def equality(ctx, x, y):
if type(x[0]) in DATETIME_NODES_LIST or type(y[0]) in DATETIME_NODES_LIST:
return datetime_equality(ctx, x, y)

return x == y
if len(x) != len(y):
return False

return util.parse_value(x[0]) == util.parse_value(y[0])


def normalize_string(s):
Expand Down Expand Up @@ -61,8 +64,11 @@ def equivalence(ctx, x, y):
if isinstance(a, Decimal) or isinstance(b, Decimal):
return is_equivalent(a, b)

if isinstance(a, nodes.FP_Quantity) or isinstance(b, nodes.FP_Quantity):
return a.deep_equal(b)
x_val = util.parse_value(x[0])
y_val = util.parse_value(y[0])

if isinstance(x_val, nodes.FP_Quantity) and isinstance(y_val, nodes.FP_Quantity):
return x_val.deep_equal(y_val)

return x == y

Expand Down
5 changes: 5 additions & 0 deletions fhirpathpy/engine/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ def __hash__(self):
def __eq__(self, other):
if isinstance(other, FP_Quantity):
if self.unit in self._years_and_months and other.unit in self._years_and_months:
if self.unit == "'a'" or other.unit == "'a'":
return False
return self._compare_years_and_months(other)
elif self.unit in self._weeks_days_and_time and other.unit in self._weeks_days_and_time:
self_value_in_seconds = self.value * self.datetime_multipliers[self.unit]
Expand Down Expand Up @@ -752,6 +754,9 @@ def __hash__(self):
def get_type_info(self):
namespace = TypeInfo.FHIR

if self.path is None:
return None

match = re.match(r"^System\.(.*)$", self.path)
if match:
return TypeInfo(namespace=TypeInfo.System, name=match.group(1))
Expand Down
15 changes: 14 additions & 1 deletion fhirpathpy/engine/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
from collections import OrderedDict
from functools import reduce
from fhirpathpy.engine.nodes import ResourceNode
from fhirpathpy.engine.nodes import ResourceNode, FP_Quantity


class set_paths:
Expand All @@ -24,6 +24,19 @@ def get_data(value):
return value


def parse_value(value):
def parse_complex_value(v):
num_value, unit = v.get("value"), v.get("code")
return FP_Quantity(num_value, f"'{unit}'") if num_value and unit else None

return (
parse_complex_value(value.data)
if getattr(value, "get_type_info", lambda: None)()
and value.get_type_info().name == "Quantity"
else value
)


def is_number(value):
return isinstance(value, (int, Decimal, complex)) and not isinstance(value, bool)

Expand Down

0 comments on commit ed98d6f

Please sign in to comment.