From 2d4be9a7de6ddd5ed5ae494c27bd96442e5d7371 Mon Sep 17 00:00:00 2001 From: James Hayhurst Date: Fri, 8 Nov 2024 16:14:13 +0000 Subject: [PATCH] read from maps --- app/models/entities/Drug.scala | 29 +++++++---------------------- app/models/entities/GwasIndex.scala | 10 ++++++++-- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/app/models/entities/Drug.scala b/app/models/entities/Drug.scala index 61d24177..95f4233b 100644 --- a/app/models/entities/Drug.scala +++ b/app/models/entities/Drug.scala @@ -136,27 +136,12 @@ object Drug { MechanismsOfAction(rows, uat, utt) } - implicit val DrugXRefImpF: OFormat[DrugReferences] = Json.format[models.entities.DrugReferences] - - private val drugTransformerXRef: Reads[JsObject] = __.json.update( - /* - The incoming Json has an array of cross reference objects with a struct of "key" for the source and "value" for the reference. - We don't know in advance which drug has which references. - */ - __.read[JsObject] - .map { o => - if (o.keys.contains("crossReferences")) { - val replaceKeys = o.value("crossReferences").as[JsArray].value.map { x => - val source = x.as[JsObject].value("key").as[String] - val reference = x.as[JsObject].value("value") - JsObject(Seq("source" -> JsString(source), "reference" -> reference)) - } - (o - "crossReferences") ++ Json.obj("crossReferences" -> replaceKeys) - } else { - o - } - } - ) - implicit val drugImplicitR: Reads[Drug] = drugTransformerXRef.andThen(Json.reads[Drug]) + implicit val DrugXRefImpW: OWrites[DrugReferences] = Json.writes[DrugReferences] + implicit val DrugXRefImpR: Reads[DrugReferences] = ( + (JsPath \ "key").read[String] and + (JsPath \ "value").read[Seq[String]] + )(DrugReferences.apply _) + + implicit val drugImplicitR: Reads[Drug] = Json.reads[Drug] implicit val drugImplicitW: OWrites[Drug] = Json.writes[Drug] } diff --git a/app/models/entities/GwasIndex.scala b/app/models/entities/GwasIndex.scala index dee1363e..f7df5617 100644 --- a/app/models/entities/GwasIndex.scala +++ b/app/models/entities/GwasIndex.scala @@ -25,6 +25,8 @@ import sangria.schema.{ } import models.gql.StudyTypeEnum import models.gql.Arguments.{pageArg, StudyType} +import play.api.libs.json._ +import play.api.libs.functional.syntax.toFunctionalBuilderOps case class StudyQueryArgs( id: Seq[String] = Seq.empty, @@ -39,12 +41,16 @@ object GwasIndex extends Logging { case class LdPopulationStructure(ldPopulation: Option[String], relativeSampleSize: Option[Double]) - case class SumStatQC(QCCheckName: Option[String], QCCheckValue: Option[Double]) + case class SumStatQC(QCCheckName: String, QCCheckValue: Double) implicit val sampleF: OFormat[Sample] = Json.format[Sample] implicit val ldPopulationStructureF: OFormat[LdPopulationStructure] = Json.format[LdPopulationStructure] - implicit val sumStatQCF: OFormat[SumStatQC] = Json.format[SumStatQC] + implicit val sumStatQCW: OWrites[SumStatQC] = Json.writes[SumStatQC] + implicit val sumStatQCR: Reads[SumStatQC] = ( + (JsPath \ "key").read[String] and + (JsPath \ "value").read[Double] + )(SumStatQC.apply _) implicit val ldPopulationStructureImp: ObjectType[Backend, LdPopulationStructure] = deriveObjectType[Backend, LdPopulationStructure]()