Skip to content

Commit fa10e21

Browse files
committed
WIP: Add initial versions of compile_as_first and compile_as_array helpers with tests
Ref: #63
1 parent a0b7bb4 commit fa10e21

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

fhirpathpy/__init__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,26 @@ def compile(path, model=None, options=None):
124124
For example, you could pass in the result of require("fhirpath/fhir-context/r4")
125125
"""
126126
return set_paths(apply_parsed_path, parsedPath=parse(path), model=model, options=options)
127+
128+
129+
def compile_as_array(expression: str):
130+
path_fn = compile(expression)
131+
132+
def fn(resource, context=None):
133+
result = path_fn(resource, context)
134+
return result if isinstance(result, list) else ([] if result is None else [result])
135+
136+
return fn
137+
138+
139+
def compile_as_first(expression: str):
140+
path_fn = compile(expression)
141+
142+
def fn(resource, context = None):
143+
result = path_fn(resource, context)
144+
if isinstance(result, list):
145+
return result[0] if result else None
146+
147+
return result
148+
149+
return fn

tests/test_helpers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from fhirpathpy import compile_as_first, compile_as_array
2+
3+
4+
PATIENT_DATA = {
5+
"resourceType": "Patient",
6+
"name": [{"given": ["First", "Middle"], "family": "Last"}],
7+
}
8+
9+
10+
def compile_as_first_test():
11+
patient_given = compile_as_first("Patient.name.given")
12+
assert patient_given(PATIENT_DATA) == "First"
13+
14+
15+
def compile_as_array_test():
16+
patient_given = compile_as_array("Patient.name.given")
17+
assert patient_given(PATIENT_DATA) == ["First", "Middle"]

0 commit comments

Comments
 (0)