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

feat: Gracefully handle primitive extensions in a backwards-compatible way #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fhirclient
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,14 @@ and install the needed packages as shown:
[smart]: http://docs.smarthealthit.org
[fhir-parser]: https://github.com/smart-on-fhir/fhir-parser
[docs]: https://smart-on-fhir.github.io/client-py

### Generating Fresh Models

To generate fresh models after making changes to `fhir-parser` or to
`fhir-parser-resources`, do the following:

```
git submodule init --update --recursive
pip install -r fhir-parser/requirements.txt
./generate_models.sh
```
21 changes: 21 additions & 0 deletions fhir-parser-resources/fhirprimitiveextension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from . import backboneelement, extension


class FHIRPrimitiveExtension(backboneelement.BackboneElement):
"""A special kind of FHIR extension which can be used to extend primitives.
"""

def __init__(self, jsondict=None, strict=True):
self.extension = None
""" The extension value(s).
List of `Extension` items (represented as `list[dict]` in JSON). """

super(FHIRPrimitiveExtension, self).__init__(jsondict=jsondict,
strict=strict)

def elementProperties(self):
js = super(FHIRPrimitiveExtension, self).elementProperties()
js.extend([
("extension", "extension", extension.Extension, True, None, False),
])
return js
1 change: 1 addition & 0 deletions fhir-parser-resources/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
('../fhir-parser-resources/fhirdate.py', 'fhirdate', ['date']),
('../fhir-parser-resources/fhirdatetime.py', 'fhirdatetime', ['dateTime']),
('../fhir-parser-resources/fhirinstant.py', 'fhirinstant', ['instant']),
('../fhir-parser-resources/fhirprimitiveextension.py', 'fhirprimitiveextension', ['FHIRPrimitiveExtension']),
('../fhir-parser-resources/fhirtime.py', 'fhirtime', ['time']),
('../fhir-parser-resources/fhirsearch.py', 'fhirsearch', ['FHIRSearch']),
]
11 changes: 10 additions & 1 deletion fhir-parser-resources/template-resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ def __init__(self, jsondict=None, strict=True):
{% if prop.is_array %}List of{% else %}Type{% endif %} `{{ prop.class_name }}`{% if prop.is_array %} items{% endif %}
{%- if prop.reference_to_names|length > 0 %} referencing `{{ prop.reference_to_names|join(', ') }}`{% endif %}
{%- if prop.json_class != prop.class_name %} (represented as `{{ prop.json_class }}` in JSON){% endif %}. """
{#- HACK: _owner is a reserved property in fhirclient... #}
{%- if prop.name != "owner" %}
self._{{ prop.name }} = None
""" Primitive extension for {{ prop.name }}. {% if prop.is_array %}List of{% else %}Type{% endif %} `FHIRPrimitiveExtension` """
{%- endif %}
{%- endfor %}

super({{ klass.name }}, self).__init__(jsondict=jsondict, strict=strict)
Expand All @@ -69,14 +74,18 @@ def elementProperties(self):
{{- prop.is_array }},
{%- if prop.one_of_many %} "{{ prop.one_of_many }}"{% else %} None{% endif %}, {# #}
{{- prop.nonoptional }}),
{#- HACK: _owner is a reserved property in fhirclient... #}
{%- if prop.name != "owner" %}
("_{{ prop.name }}", "_{{ prop.name }}", fhirprimitiveextension.FHIRPrimitiveExtension, {{ prop.is_array }}, None, False),
{%- endif %}
{%- endfor %}
])
return js

{%- endif %}
{%- endfor %}

from . import fhirprimitiveextension
{% for imp in imports %}{% if imp.module not in imported %}
from . import {{ imp.module }}
{%- endif %}{% endfor %}

48 changes: 47 additions & 1 deletion fhirclient/models/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,32 @@ def __init__(self, jsondict=None, strict=True):
""" The party(s) that are responsible for covering the payment of this
account, and what order should they be applied to the account.
List of `AccountCoverage` items (represented as `dict` in JSON). """
self._coverage = None
""" Primitive extension for coverage. List of `FHIRPrimitiveExtension` """

self.description = None
""" Explanation of purpose/use.
Type `str`. """
self._description = None
""" Primitive extension for description. Type `FHIRPrimitiveExtension` """

self.guarantor = None
""" The parties ultimately responsible for balancing the Account.
List of `AccountGuarantor` items (represented as `dict` in JSON). """
self._guarantor = None
""" Primitive extension for guarantor. List of `FHIRPrimitiveExtension` """

self.identifier = None
""" Account number.
List of `Identifier` items (represented as `dict` in JSON). """
self._identifier = None
""" Primitive extension for identifier. List of `FHIRPrimitiveExtension` """

self.name = None
""" Human-readable label.
Type `str`. """
self._name = None
""" Primitive extension for name. Type `FHIRPrimitiveExtension` """

self.owner = None
""" Entity managing the Account.
Expand All @@ -50,39 +60,59 @@ def __init__(self, jsondict=None, strict=True):
self.partOf = None
""" Reference to a parent Account.
Type `FHIRReference` (represented as `dict` in JSON). """
self._partOf = None
""" Primitive extension for partOf. Type `FHIRPrimitiveExtension` """

self.servicePeriod = None
""" Transaction window.
Type `Period` (represented as `dict` in JSON). """
self._servicePeriod = None
""" Primitive extension for servicePeriod. Type `FHIRPrimitiveExtension` """

self.status = None
""" active | inactive | entered-in-error | on-hold | unknown.
Type `str`. """
self._status = None
""" Primitive extension for status. Type `FHIRPrimitiveExtension` """

self.subject = None
""" The entity that caused the expenses.
List of `FHIRReference` items (represented as `dict` in JSON). """
self._subject = None
""" Primitive extension for subject. List of `FHIRPrimitiveExtension` """

self.type = None
""" E.g. patient, expense, depreciation.
Type `CodeableConcept` (represented as `dict` in JSON). """
self._type = None
""" Primitive extension for type. Type `FHIRPrimitiveExtension` """

super(Account, self).__init__(jsondict=jsondict, strict=strict)

def elementProperties(self):
js = super(Account, self).elementProperties()
js.extend([
("coverage", "coverage", AccountCoverage, True, None, False),
("_coverage", "_coverage", fhirprimitiveextension.FHIRPrimitiveExtension, True, None, False),
("description", "description", str, False, None, False),
("_description", "_description", fhirprimitiveextension.FHIRPrimitiveExtension, False, None, False),
("guarantor", "guarantor", AccountGuarantor, True, None, False),
("_guarantor", "_guarantor", fhirprimitiveextension.FHIRPrimitiveExtension, True, None, False),
("identifier", "identifier", identifier.Identifier, True, None, False),
("_identifier", "_identifier", fhirprimitiveextension.FHIRPrimitiveExtension, True, None, False),
("name", "name", str, False, None, False),
("_name", "_name", fhirprimitiveextension.FHIRPrimitiveExtension, False, None, False),
("owner", "owner", fhirreference.FHIRReference, False, None, False),
("partOf", "partOf", fhirreference.FHIRReference, False, None, False),
("_partOf", "_partOf", fhirprimitiveextension.FHIRPrimitiveExtension, False, None, False),
("servicePeriod", "servicePeriod", period.Period, False, None, False),
("_servicePeriod", "_servicePeriod", fhirprimitiveextension.FHIRPrimitiveExtension, False, None, False),
("status", "status", str, False, None, True),
("_status", "_status", fhirprimitiveextension.FHIRPrimitiveExtension, False, None, False),
("subject", "subject", fhirreference.FHIRReference, True, None, False),
("_subject", "_subject", fhirprimitiveextension.FHIRPrimitiveExtension, True, None, False),
("type", "type", codeableconcept.CodeableConcept, False, None, False),
("_type", "_type", fhirprimitiveextension.FHIRPrimitiveExtension, False, None, False),
])
return js

Expand All @@ -108,18 +138,24 @@ def __init__(self, jsondict=None, strict=True):
""" The party(s), such as insurances, that may contribute to the
payment of this account.
Type `FHIRReference` (represented as `dict` in JSON). """
self._coverage = None
""" Primitive extension for coverage. Type `FHIRPrimitiveExtension` """

self.priority = None
""" The priority of the coverage in the context of this account.
Type `int`. """
self._priority = None
""" Primitive extension for priority. Type `FHIRPrimitiveExtension` """

super(AccountCoverage, self).__init__(jsondict=jsondict, strict=strict)

def elementProperties(self):
js = super(AccountCoverage, self).elementProperties()
js.extend([
("coverage", "coverage", fhirreference.FHIRReference, False, None, True),
("_coverage", "_coverage", fhirprimitiveextension.FHIRPrimitiveExtension, False, None, False),
("priority", "priority", int, False, None, False),
("_priority", "_priority", fhirprimitiveextension.FHIRPrimitiveExtension, False, None, False),
])
return js

Expand All @@ -144,28 +180,38 @@ def __init__(self, jsondict=None, strict=True):
self.onHold = None
""" Credit or other hold applied.
Type `bool`. """
self._onHold = None
""" Primitive extension for onHold. Type `FHIRPrimitiveExtension` """

self.party = None
""" Responsible entity.
Type `FHIRReference` (represented as `dict` in JSON). """
self._party = None
""" Primitive extension for party. Type `FHIRPrimitiveExtension` """

self.period = None
""" Guarantee account during.
Type `Period` (represented as `dict` in JSON). """
self._period = None
""" Primitive extension for period. Type `FHIRPrimitiveExtension` """

super(AccountGuarantor, self).__init__(jsondict=jsondict, strict=strict)

def elementProperties(self):
js = super(AccountGuarantor, self).elementProperties()
js.extend([
("onHold", "onHold", bool, False, None, False),
("_onHold", "_onHold", fhirprimitiveextension.FHIRPrimitiveExtension, False, None, False),
("party", "party", fhirreference.FHIRReference, False, None, True),
("_party", "_party", fhirprimitiveextension.FHIRPrimitiveExtension, False, None, False),
("period", "period", period.Period, False, None, False),
("_period", "_period", fhirprimitiveextension.FHIRPrimitiveExtension, False, None, False),
])
return js

from . import fhirprimitiveextension

from . import codeableconcept
from . import fhirreference
from . import identifier
from . import period
from . import period
Loading