From 98874f4445699cfb72b9f5eef8b40ca0abd39389 Mon Sep 17 00:00:00 2001 From: Andrea Acampora Date: Tue, 28 Mar 2023 22:29:29 +0200 Subject: [PATCH] style: create method to avoid code duplication --- .../event/serialization/EventSerialization.kt | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/application/presenter/event/serialization/EventSerialization.kt b/src/main/kotlin/application/presenter/event/serialization/EventSerialization.kt index df2047d5..61810288 100644 --- a/src/main/kotlin/application/presenter/event/serialization/EventSerialization.kt +++ b/src/main/kotlin/application/presenter/event/serialization/EventSerialization.kt @@ -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 @@ -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>(this) - + deserializeToEvent(this) + ProcessEventsKeys.MEDICAL_DEVICE_USAGE_EVENT -> + deserializeToEvent(this) + ProcessEventsKeys.PATIENT_ON_OB_EVENT -> + deserializeToEvent(this) + ProcessEventsKeys.PATIENT_TRACKED_EVENT -> + deserializeToEvent(this) + ProcessEventsKeys.PATIENT_BODY_TEMPERATURE_UPDATE_EVENT -> + deserializeToEvent>(this) + ProcessEventsKeys.PATIENT_DIASTOLIC_PRESSURE_UPDATE_EVENT -> + deserializeToEvent>(this) + ProcessEventsKeys.PATIENT_SYSTOLIC_PRESSURE_UPDATE_EVENT -> + deserializeToEvent>(this) + ProcessEventsKeys.PATIENT_RESPIRATORY_RATE_UPDATE_EVENT -> + deserializeToEvent>(this) + ProcessEventsKeys.PATIENT_SATURATION_UPDATE_EVENT -> + deserializeToEvent>(this) + ProcessEventsKeys.PATIENT_HEARTBEAT_UPDATE_EVENT -> + deserializeToEvent>(this) else -> throw IllegalArgumentException("Event not supported") } + + private inline fun deserializeToEvent(event: String) = + Json.decodeFromString>(event) }