Skip to content

Commit

Permalink
update xrefs parser
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhayhurst committed Oct 21, 2024
1 parent d9573a1 commit 3e9bba2
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions app/models/entities/Drug.scala
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,18 @@ object Drug {

private val drugTransformerXRef: Reads[JsObject] = __.json.update(
/*
The incoming Json has an cross reference object with an array for each source. We don't know in advance which drug
has which references, so we need to flatten the object into an array of objects for conversion into case classes.
See: https://www.playframework.com/documentation/2.6.x/ScalaJsonTransformers
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 cr: Seq[(String, JsValue)] = o.value("crossReferences").as[JsObject].fields.to(Seq)
val newJsonObjects: Seq[JsObject] =
cr.map(xref => JsObject(Seq("source" -> JsString(xref._1), "reference" -> xref._2)))
(o - "crossReferences") ++ Json.obj("crossReferences" -> newJsonObjects)
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
}
Expand Down

0 comments on commit 3e9bba2

Please sign in to comment.