Skip to content

Commit

Permalink
Add support for avg, min, max, sum, toBoolean, convertsTo
Browse files Browse the repository at this point in the history
  • Loading branch information
projkov committed Dec 13, 2023
1 parent ba71798 commit bab0dd7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
10 changes: 10 additions & 0 deletions fhirpathpy/engine/invocations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import fhirpathpy.engine.invocations.logic as logic
import fhirpathpy.engine.invocations.datetime as datetime
import fhirpathpy.engine.invocations.types as types
import fhirpathpy.engine.invocations.aggregate as aggregate

invocations = {
"empty": {"fn": existence.empty_fn},
Expand Down Expand Up @@ -44,6 +45,7 @@
"iif": {"fn": misc.iif_macro, "arity": {2: ["Expr", "Expr"], 3: ["Expr", "Expr", "Expr"]}},
"trace": {"fn": misc.trace_fn, "arity": {0: [], 1: ["String"]}},
"toInteger": {"fn": misc.to_integer},
"toBoolean": {"fn": misc.to_boolean},
"toDecimal": {"fn": misc.to_decimal},
"toString": {"fn": misc.to_string},
"toDate": {"fn": misc.to_date},
Expand Down Expand Up @@ -113,4 +115,12 @@
"and": {"fn": logic.and_op, "arity": {2: [["Boolean"], ["Boolean"]]}},
"xor": {"fn": logic.xor_op, "arity": {2: [["Boolean"], ["Boolean"]]}},
"implies": {"fn": logic.implies_op, "arity": {2: [["Boolean"], ["Boolean"]]}},
"avg": {"fn": aggregate.avg_fn},
"sum": {"fn": aggregate.sum_fn},
"min": {"fn": aggregate.min_fn},
"max": {"fn": aggregate.max_fn},
"convertsToBoolean": {"fn": misc.create_converts_to_fn(misc.to_boolean, 'bool')},
"convertsToInteger": {"fn": misc.create_converts_to_fn(misc.to_integer, 'int')},
"convertsToDecimal": {"fn": misc.create_converts_to_fn(misc.to_decimal, 'float')},
"convertsToString": {"fn": misc.create_converts_to_fn(misc.to_string, 'str')},
}
27 changes: 27 additions & 0 deletions fhirpathpy/engine/invocations/aggregate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from fhirpathpy.engine.invocations.existence import count_fn
from fhirpathpy.engine.invocations.math import div


def avg_fn(ctx, x):
if count_fn(ctx, x) == 0:
return []

return div(ctx, sum_fn(ctx, x), count_fn(ctx, x))


def sum_fn(ctx, x):
return sum(x)


def min_fn(ctx, x):
if count_fn(ctx, x) == 0:
return []

return min(x)


def max_fn(ctx, x):
if count_fn(ctx, x) == 0:
return []

return max(x)
44 changes: 44 additions & 0 deletions fhirpathpy/engine/invocations/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,47 @@ def to_date(ctx, coll):
rtn.append(dateObject)

return rtn


def create_converts_to_fn(to_function, _type):
if isinstance(_type, str):
def in_function(ctx, coll):
if len(coll) != 1:
return []
return type(to_function(ctx, coll)).__name__ == _type
return in_function

def in_function(ctx, coll):
if len(coll) != 1:
return []

return isinstance(to_function(ctx, coll), _type)

return in_function


def to_boolean(ctx, coll):
true_strings = ['true', 't', 'yes', 'y', '1', '1.0']
false_strings = ['false', 'f', 'no', 'n', '0', '0.0']

if len(coll) != 1:
return []

val = coll[0]
var_type = type(val).__name__

if var_type == "bool":
return val
elif var_type == "int" or var_type == "float":
if val == 1 or val == 1.0:
return True
elif val == 0 or val == 0.0:
return False
elif var_type == "str":
lower_case_var = val.lower()
if lower_case_var in true_strings:
return True
if lower_case_var in false_strings:
return False

return []

0 comments on commit bab0dd7

Please sign in to comment.