Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for resolve() #42

Open
vadi2 opened this issue Nov 21, 2024 · 4 comments
Open

Support for resolve() #42

vadi2 opened this issue Nov 21, 2024 · 4 comments

Comments

@vadi2
Copy link
Contributor

vadi2 commented Nov 21, 2024

It would be nice if resolve() was supported for references that are within the Bundle you are operating on.

@vadi2
Copy link
Contributor Author

vadi2 commented Nov 21, 2024

I haven't got the bandwidth to add resolve natively, but in case anyone is looking for a naive implementation to use externally, this works well:

def evaluate_with_resolve(bundle, fhirpath):
    if '.resolve()' not in fhirpath:
        return evaluate(bundle, fhirpath)
        
    path_segments = fhirpath.split('.resolve()')
    current_result = evaluate(bundle, path_segments[0])
    
    for segment in path_segments[1:]:
        if not current_result:
            return None
        reference_id = current_result[0]
        if not reference_id:
            return None
        # Find referenced resource in bundle
        referenced_resource = next(
          (entry['resource'] for entry in bundle['entry'] 
          if entry.get('fullUrl') == reference_id),
          None)

        if not referenced_resource:
            return None
        current_result = evaluate(referenced_resource, segment.lstrip('.'))
    return current_result

@ruscoder
Copy link
Member

Hi @vadi2, thanks for posting your solution, it's useful feature.

The only addition from my side to this feature - it should follow the specification of resolving references, in that case reference 'Patient/123' should be also resolved into the entry with fullUrl http://example.com/fhir/Patient/123.

@ruscoder
Copy link
Member

The fhirpath specification says nothing about how exactly url's should be resolved.

For me, it looks logical to have a Bundle as context, and use it like:
evaluate(bundle, 'entry.resource.ofType(Patient).managingOrganization.resolve()')
should return all patients' organizations.

@axelv
Copy link
Contributor

axelv commented Dec 10, 2024

FYI: we do this by patching the invocation table:

from fhirpathpy.engine.invocations import invocation_registry

# add custom resolve fn to the invocations_register
invocation_registry["resolve"] = {"fn": resolve, "arity": {0: []}}
evaluate({...},  "Patient.subject.resolve()")

But not sure if it's ok to use invocation_registry directly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants