Skip to content

Commit

Permalink
Update member invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
atuonufure committed Jan 8, 2024
1 parent 96e9799 commit 6bd1c6b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
58 changes: 25 additions & 33 deletions fhirpathpy/engine/evaluators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ def param_list(ctx, parentData, node):
def union_expression(ctx, parentData, node):
return engine.infix_invoke(ctx, "|", parentData, node["children"])


def index_invocation(ctx, parentData, node):
return util.arraify(ctx["$index"])


def this_invocation(ctx, parentData, node):
return util.arraify(ctx["$this"])

Expand Down Expand Up @@ -168,49 +170,39 @@ def time_literal(ctx, parentData, node):
def create_reduce_member_invocation(model, key):
def func(acc, res):
res = nodes.ResourceNode.create_node(res)

childPath = ""
if res.path is not None:
childPath = res.path + "." + key

if (
model is not None
and "pathsDefinedElsewhere" in model
and childPath in model["pathsDefinedElsewhere"]
):
childPath = model["pathsDefinedElsewhere"][childPath]
childPath = f"{res.path}.{key}" if res.path else key

actualTypes = None

if (
model is not None
and "choiceTypePaths" in model
and childPath in model["choiceTypePaths"]
):
actualTypes = model["choiceTypePaths"][childPath]

toAdd = None
toAdd_ = None

if isinstance(model, dict):
childPath = model["pathsDefinedElsewhere"].get(childPath, childPath)
actualTypes = model["choiceTypePaths"].get(childPath)

if isinstance(res.data, nodes.FP_Quantity):
toAdd = res.data.value

if isinstance(actualTypes, list):
if actualTypes and isinstance(res.data, dict):
# Use actualTypes to find the field's value
for actualType in actualTypes:
field = key + actualType
if isinstance(res.data, (dict, list)):
toAdd = res.data.get(field)
toAdd_ = res.data.get(f"_{field}")
if toAdd is not None or toAdd_ is not None:
childPath = actualType
break
else:
if isinstance(res.data, (dict, list)):
toAdd = res.data.get(key)
toAdd_ = res.data.get(f"_{key}")
if key == "extension":
childPath = "Extension"
field = f"{key}{actualType}"
toAdd = res.data.get(field)
toAdd_ = res.data.get(f"_{field}")
if toAdd is not None or toAdd_ is not None:
childPath += actualType
break
elif isinstance(res.data, dict):
toAdd = res.data.get(key)
toAdd_ = res.data.get(f"_{key}")
if key == "extension":
childPath = "Extension"

childPath = (
model["path2Type"].get(childPath, childPath)
if isinstance(model, dict) and "path2Type" in model
else childPath
)

if util.is_some(toAdd):
if isinstance(toAdd, list):
Expand Down
10 changes: 7 additions & 3 deletions fhirpathpy/engine/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,8 @@ def __init__(self, dateStr):
self._precision = self._calculatePrecision(self._dateTimeAsList)

def __str__(self):
if re.match(r'^\d{4}-\d{2}-\d{2}$', self.asStr):
return self.asStr
if self.asStr and len(self.asStr) <= 4:
return self.asStr
if self._getDateTimeObject():
Expand Down Expand Up @@ -745,9 +747,10 @@ class ResourceNode:
* @param path the node's path in the resource (e.g. Patient.name). If the
* data's type can be determined from data, that will take precedence over
* this parameter.
* @param _data additional data stored in a property named with "_" prepended.
"""

def __init__(self, data, path):
def __init__(self, data, path, _data=None):
"""
If data is a resource (maybe a contained resource) reset the path
information to the resource type.
Expand All @@ -757,6 +760,7 @@ def __init__(self, data, path):

self.path = path
self.data = data
self._data = _data

def __eq__(self, value):
if isinstance(value, ResourceNode):
Expand Down Expand Up @@ -789,10 +793,10 @@ def toJSON(self):
return json.dumps(self.data)

@staticmethod
def create_node(data, path=None):
def create_node(data, path=None, _data=None):
if isinstance(data, ResourceNode):
return data
return ResourceNode(data, path)
return ResourceNode(data, path, _data)


class TypeInfo:
Expand Down

0 comments on commit 6bd1c6b

Please sign in to comment.