Skip to content

Commit

Permalink
Fix not() function
Browse files Browse the repository at this point in the history
  • Loading branch information
projkov committed Dec 22, 2023
1 parent ed98d6f commit 3441034
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
11 changes: 4 additions & 7 deletions fhirpathpy/engine/invocations/existence.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from decimal import Decimal
from fhirpathpy.engine.invocations import misc
from fhirpathpy.engine.invocations.misc import to_boolean
import fhirpathpy.engine.util as util
import fhirpathpy.engine.nodes as nodes
Expand All @@ -21,21 +22,17 @@ def count_fn(ctx, value):
return 0


# engine.notFn = function(coll) {
# let d = misc.singleton(coll, 'Boolean');
# return (typeof (d) === 'boolean') ? !d : [];
# };

def not_fn(ctx, x):
if len(x) != 1:
return []

data = util.get_data(x[0])
data = misc.singleton(x, "Boolean")

if type(data) == bool:
if isinstance(data, bool):
return not data

return not to_boolean(ctx, x)
return []


def exists_macro(ctx, coll, expr=None):
Expand Down
31 changes: 31 additions & 0 deletions fhirpathpy/engine/invocations/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,34 @@ def to_boolean(ctx, coll):
return False

return []


def boolean_singleton(coll):
d = util.get_data(coll[0])
if isinstance(d, bool):
return d
elif len(coll) == 1:
return True

def string_singleton(coll):
d = util.get_data(coll[0])
if isinstance(d, str):
return d

singleton_eval_by_type = {
"Boolean": boolean_singleton,
"String": string_singleton,
}

def singleton(coll, type):
if len(coll) > 1:
raise Exception("Unexpected collection {coll}; expected singleton of type {type}".format(coll=coll, type=type))
elif len(coll) == 0:
return []
to_singleton = singleton_eval_by_type[type]
if to_singleton:
val = to_singleton(coll)
if (val is not None):
return val
raise Exception("Expected {type}, but got: {coll}".format(type=type.lower(), coll=coll))
raise Exception("Not supported type {}".format(type))

0 comments on commit 3441034

Please sign in to comment.