Skip to content

Commit

Permalink
read from maps
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhayhurst committed Nov 18, 2024
1 parent 6a544a0 commit 2d4be9a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
29 changes: 7 additions & 22 deletions app/models/entities/Drug.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
10 changes: 8 additions & 2 deletions app/models/entities/GwasIndex.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]()
Expand Down

0 comments on commit 2d4be9a

Please sign in to comment.