Skip to content

Commit

Permalink
Add a timingPhaseDetail extension (#67)
Browse files Browse the repository at this point in the history
* Create timingPhaseDetail extension

* Use fixtures to test extension validation

* Reduce duplicated code, add 'step_down' function
and nested_extension property

* Add docstrings and edit formatting of required
function arguments
  • Loading branch information
pipliggins authored Aug 29, 2024
1 parent 2830531 commit 5142a6a
Show file tree
Hide file tree
Showing 23 changed files with 1,222 additions and 213 deletions.
344 changes: 281 additions & 63 deletions fhirflat/flat2fhir.py

Large diffs are not rendered by default.

29 changes: 24 additions & 5 deletions fhirflat/resources/condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@
from pydantic.v1 import Field, validator

from .base import FHIRFlatBase
from .extension_types import presenceAbsenceType, prespecifiedQueryType, timingPhaseType
from .extensions import presenceAbsence, prespecifiedQuery, timingPhase
from .extension_types import (
presenceAbsenceType,
prespecifiedQueryType,
timingPhaseDetailType,
# timingDetailType,
timingPhaseType,
)
from .extensions import (
presenceAbsence,
prespecifiedQuery,
timingPhase,
# timingDetail,
timingPhaseDetail,
)

JsonString: TypeAlias = str

Expand All @@ -22,6 +34,7 @@ class Condition(_Condition, FHIRFlatBase):
presenceAbsenceType,
prespecifiedQueryType,
timingPhaseType,
timingPhaseDetailType,
fhirtypes.ExtensionType,
]
] = Field(
Expand Down Expand Up @@ -61,11 +74,17 @@ def validate_extension_contents(cls, extensions):
present_count = sum(isinstance(item, presenceAbsence) for item in extensions)
query_count = sum(isinstance(item, prespecifiedQuery) for item in extensions)
timing_count = sum(isinstance(item, timingPhase) for item in extensions)
detail_count = sum(isinstance(item, timingPhaseDetail) for item in extensions)

if present_count > 1 or query_count > 1 or timing_count > 1:
if present_count > 1 or query_count > 1 or timing_count > 1 or detail_count > 1:
raise ValueError(
"presenceAbsence, prespecifiedQuery and timingPhase can only appear"
" once."
"presenceAbsence, prespecifiedQuery, timingPhase and timingPhaseDetail "
"can only appear once."
)

if timing_count > 0 and detail_count > 0:
raise ValueError(
"timingPhase and timingPhaseDetail cannot appear together."
)

return extensions
Expand Down
21 changes: 16 additions & 5 deletions fhirflat/resources/diagnosticreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
from fhirflat.flat2fhir import expand_concepts

from .base import FHIRFlatBase
from .extension_types import timingPhaseType
from .extensions import timingPhase
from .extension_types import timingPhaseDetailType, timingPhaseType
from .extensions import timingPhase, timingPhaseDetail


class DiagnosticReport(_DiagnosticReport, FHIRFlatBase):
extension: list[Union[timingPhaseType, fhirtypes.ExtensionType]] = Field(
extension: list[
Union[timingPhaseType, timingPhaseDetailType, fhirtypes.ExtensionType]
] = Field(
None,
alias="extension",
title="List of `Extension` items (represented as `dict` in JSON)",
Expand Down Expand Up @@ -51,11 +53,20 @@ class DiagnosticReport(_DiagnosticReport, FHIRFlatBase):

@validator("extension")
def validate_extension_contents(cls, extensions):
tim_phase_count = sum(isinstance(item, timingPhase) for item in extensions)
timing_count = sum(isinstance(item, timingPhase) for item in extensions)
detail_count = sum(isinstance(item, timingPhaseDetail) for item in extensions)

if tim_phase_count > 1:
if timing_count > 1:
raise ValueError("timingPhase can only appear once.")

if detail_count > 1:
raise ValueError("timingPhaseDetail can only appear once.")

if timing_count > 0 and detail_count > 0:
raise ValueError(
"timingPhase and timingPhaseDetail cannot appear together."
)

return extensions

@classmethod
Expand Down
27 changes: 20 additions & 7 deletions fhirflat/resources/encounter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@
from pydantic.v1 import Field, validator

from .base import FHIRFlatBase
from .extension_types import relativePeriodType, timingPhaseType
from .extensions import relativePeriod, timingPhase
from .extension_types import relativePeriodType, timingPhaseDetailType, timingPhaseType
from .extensions import relativePeriod, timingPhase, timingPhaseDetail

JsonString: TypeAlias = str


class Encounter(_Encounter, FHIRFlatBase):
extension: list[
Union[relativePeriodType, timingPhaseType, fhirtypes.ExtensionType]
Union[
relativePeriodType,
timingPhaseType,
timingPhaseDetailType,
fhirtypes.ExtensionType,
]
] = Field(
None,
alias="extension",
Expand Down Expand Up @@ -64,10 +69,18 @@ class Encounter(_Encounter, FHIRFlatBase):
@validator("extension")
def validate_extension_contents(cls, extensions):
rel_phase_count = sum(isinstance(item, relativePeriod) for item in extensions)
tim_phase_count = sum(isinstance(item, timingPhase) for item in extensions)

if rel_phase_count > 1 or tim_phase_count > 1:
raise ValueError("relativePeriod and timingPhase can only appear once.")
timing_count = sum(isinstance(item, timingPhase) for item in extensions)
detail_count = sum(isinstance(item, timingPhaseDetail) for item in extensions)

if rel_phase_count > 1 or timing_count > 1 or detail_count > 1:
raise ValueError(
"relativePeriod, timingPhase and timingPhaseDetail can only appear once." # noqa E501
)

if timing_count > 0 and detail_count > 0:
raise ValueError(
"timingPhase and timingPhaseDetail cannot appear together."
)

return extensions

Expand Down
8 changes: 8 additions & 0 deletions fhirflat/resources/extension_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ class timingPhaseType(AbstractType):
__resource_type__ = "timingPhase"


class timingDetailType(AbstractType):
__resource_type__ = "timingDetail"


class timingPhaseDetailType(AbstractType):
__resource_type__ = "timingPhaseDetail"


class relativeDayType(AbstractType):
__resource_type__ = "relativeDay"

Expand Down
10 changes: 10 additions & 0 deletions fhirflat/resources/extension_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Validators:
def __init__(self):
self.MODEL_CLASSES = {
"timingPhase": (None, ".extensions"),
"timingDetail": (None, ".extensions"),
"timingPhaseDetail": (None, ".extensions"),
"relativeDay": (None, ".extensions"),
"relativeStart": (None, ".extensions"),
"relativeEnd": (None, ".extensions"),
Expand Down Expand Up @@ -202,6 +204,14 @@ def timingphase_validator(v: Union[StrBytes, dict, Path, FHIRAbstractModel]):
return Validators().fhir_model_validator("timingPhase", v)


def timingdetail_validator(v: Union[StrBytes, dict, Path, FHIRAbstractModel]):
return Validators().fhir_model_validator("timingDetail", v)


def timingphasedetail_validator(v: Union[StrBytes, dict, Path, FHIRAbstractModel]):
return Validators().fhir_model_validator("timingPhaseDetail", v)


def relativeday_validator(v: Union[StrBytes, dict, Path, FHIRAbstractModel]):
return Validators().fhir_model_validator("relativeDay", v)

Expand Down
Loading

0 comments on commit 5142a6a

Please sign in to comment.