Skip to content

Commit

Permalink
style: create method to avoid code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-acampora committed Mar 28, 2023
1 parent 5b48158 commit 98874f4
Showing 1 changed file with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ package application.presenter.event.serialization
import application.presenter.event.model.Event
import application.presenter.event.model.ProcessEvent
import application.presenter.event.model.ProcessEventsKeys
import application.presenter.event.model.payloads.ProcessEventsPayloads.BodyTemperature
import application.presenter.event.model.payloads.ProcessEventsPayloads.DiastolicPressure
import application.presenter.event.model.payloads.ProcessEventsPayloads.Heartbeat
import application.presenter.event.model.payloads.ProcessEventsPayloads.MedicalDeviceUsage
import application.presenter.event.model.payloads.ProcessEventsPayloads.MedicalTechnologyUsage
import application.presenter.event.model.payloads.ProcessEventsPayloads.PatientData
import application.presenter.event.model.payloads.ProcessEventsPayloads.PatientTracked
import application.presenter.event.model.payloads.ProcessEventsPayloads.ProcessEventPayload
import application.presenter.event.model.payloads.ProcessEventsPayloads.ProcessInfo
import application.presenter.event.model.payloads.ProcessEventsPayloads.RespiratoryRate
import application.presenter.event.model.payloads.ProcessEventsPayloads.Saturation
import application.presenter.event.model.payloads.ProcessEventsPayloads.SystolicPressure
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

Expand All @@ -21,8 +32,28 @@ object EventSerialization {
/** Extension method to deserialize a json to an [Event]. */
fun String.toEvent(eventKey: String): Event<*> = when (eventKey) {
ProcessEventsKeys.MEDICAL_TECHNOLOGY_USAGE_EVENT ->
Json.decodeFromString<ProcessEvent<MedicalTechnologyUsage>>(this)

deserializeToEvent<MedicalTechnologyUsage>(this)
ProcessEventsKeys.MEDICAL_DEVICE_USAGE_EVENT ->
deserializeToEvent<MedicalDeviceUsage>(this)
ProcessEventsKeys.PATIENT_ON_OB_EVENT ->
deserializeToEvent<ProcessInfo>(this)
ProcessEventsKeys.PATIENT_TRACKED_EVENT ->
deserializeToEvent<PatientTracked>(this)
ProcessEventsKeys.PATIENT_BODY_TEMPERATURE_UPDATE_EVENT ->
deserializeToEvent<PatientData<BodyTemperature>>(this)
ProcessEventsKeys.PATIENT_DIASTOLIC_PRESSURE_UPDATE_EVENT ->
deserializeToEvent<PatientData<DiastolicPressure>>(this)
ProcessEventsKeys.PATIENT_SYSTOLIC_PRESSURE_UPDATE_EVENT ->
deserializeToEvent<PatientData<SystolicPressure>>(this)
ProcessEventsKeys.PATIENT_RESPIRATORY_RATE_UPDATE_EVENT ->
deserializeToEvent<PatientData<RespiratoryRate>>(this)
ProcessEventsKeys.PATIENT_SATURATION_UPDATE_EVENT ->
deserializeToEvent<PatientData<Saturation>>(this)
ProcessEventsKeys.PATIENT_HEARTBEAT_UPDATE_EVENT ->
deserializeToEvent<PatientData<Heartbeat>>(this)
else -> throw IllegalArgumentException("Event not supported")
}

private inline fun <reified X : ProcessEventPayload> deserializeToEvent(event: String) =
Json.decodeFromString<ProcessEvent<X>>(event)
}

0 comments on commit 98874f4

Please sign in to comment.