Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Implicit encoders/decoders in one place. #602

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions src/it/scala/org/renci/cam/it/ExampleQueriesEndpointTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,7 @@ object ExampleQueriesEndpointTest extends DefaultRunnableSpec {
exampleJson <- ZIO.fromEither(io.circe.parser.parse(exampleText))
example <- ZIO.fromEither(
{
implicit val decoderIRI: Decoder[IRI] = Implicits.iriDecoder(biolinkData.prefixes)
implicit val keyDecoderIRI: KeyDecoder[IRI] = Implicits.iriKeyDecoder(biolinkData.prefixes)
implicit val decoderBiolinkClass: Decoder[BiolinkClass] = Implicits.biolinkClassDecoder(biolinkData.classes)
implicit val decoderBiolinkPredicate: Decoder[BiolinkPredicate] =
Implicits.biolinkPredicateDecoder(biolinkData.predicates)
implicit lazy val decoderTRAPIAttribute: Decoder[TRAPIAttribute] = deriveDecoder[TRAPIAttribute]

import biolinkData.implicits._
exampleJson.as[ExampleJsonFile]
}
)
Expand All @@ -93,12 +87,7 @@ object ExampleQueriesEndpointTest extends DefaultRunnableSpec {

// Prepare request for the CAM-KP-API endpoint.
messageText = {
implicit val iriEncoder: Encoder[IRI] = Implicits.iriEncoder(biolinkData.prefixes)
implicit val iriKeyEncoder: KeyEncoder[IRI] = Implicits.iriKeyEncoder(biolinkData.prefixes)
implicit val biolinkClassEncoder: Encoder[BiolinkClass] = Implicits.biolinkClassEncoder
implicit val biolinkPredicateEncoder: Encoder[BiolinkPredicate] =
Implicits.biolinkPredicateEncoder(biolinkData.prefixes)

import biolinkData.implicits._
TRAPIQuery(message = example.message, log_level = None).asJson.deepDropNullValues.noSpaces
}
// _ = println(s"messageText = ${messageText}")
Expand All @@ -115,13 +104,7 @@ object ExampleQueriesEndpointTest extends DefaultRunnableSpec {
// Translate the response into a TRAPIResponse for testing.
trapiResponse <- ZIO.fromEither(
{
implicit val decoderIRI: Decoder[IRI] = Implicits.iriDecoder(biolinkData.prefixes)
implicit val keyDecoderIRI: KeyDecoder[IRI] = Implicits.iriKeyDecoder(biolinkData.prefixes)
implicit val decoderBiolinkClass: Decoder[BiolinkClass] = Implicits.biolinkClassDecoder(biolinkData.classes)
implicit val decoderBiolinkPredicate: Decoder[BiolinkPredicate] =
Implicits.biolinkPredicateDecoder(biolinkData.predicates)
implicit lazy val decoderTRAPIAttribute: Decoder[TRAPIAttribute] = deriveDecoder[TRAPIAttribute]

import biolinkData.implicits._
response.as[TRAPIResponse]
}
)
Expand Down
5 changes: 2 additions & 3 deletions src/it/scala/org/renci/cam/it/ImplicitsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object ImplicitsTest extends DefaultRunnableSpec with LazyLogging {
for {
bl <- biolinkData
} yield {
implicit val iriEncoder: Encoder[IRI] = Implicits.iriEncoder(bl.prefixes)
import bl.implicits._
val json = iri.asJson.deepDropNullValues.noSpaces.replace("\"", "")
assert(json)(equalTo("WB:WBGene00013878"))
}
Expand All @@ -42,8 +42,7 @@ object ImplicitsTest extends DefaultRunnableSpec with LazyLogging {
} yield {
val dataAsList = """["biolink:participates_in","biolink:related_to"]"""
val data = """"biolink:related_to""""
implicit val biolinkPredicateDecoder: Decoder[List[BiolinkPredicate]] =
Implicits.predicateOrPredicateListDecoder(biolinkData.predicates)
import biolinkData.implicits._
val ret = decode[List[BiolinkPredicate]](data)
val retWithListData = decode[List[BiolinkPredicate]](dataAsList)
assert(ret.toOption.get)(contains(BiolinkPredicate("related_to"))) && assert(retWithListData.toOption.get)(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,7 @@ object MetaKnowledgeGraphServiceTest extends DefaultRunnableSpec with LazyLoggin
.withHeaders(Accept(MediaType.application.json))
response <- httpClient.expect[String](request)
} yield {
implicit val iriKeyEncoder: KeyEncoder[BiolinkClass] = Implicits.biolinkClassKeyEncoder
implicit val iriKeyDecoder: KeyDecoder[BiolinkClass] = Implicits.biolinkClassKeyDecoder(biolinkData.classes)

// implicit val iriDecoder: Decoder[IRI] = Implicits.iriDecoder(biolinkData.prefixes)
// implicit val iriEncoder: Encoder[IRI] = Implicits.iriEncoder(biolinkData.prefixes)

// logger.info("biolinkData.classes: {}", biolinkData.classes)
implicit val blClassDecoder: Decoder[BiolinkClass] = Implicits.biolinkClassDecoder(biolinkData.classes)
// implicit val blClassEncoder: Encoder[BiolinkClass] = Implicits.biolinkClassEncoder

// logger.info("biolinkData.predicates: {}", biolinkData.predicates)
implicit val biolinkPredicateDecoder: Decoder[BiolinkPredicate] = Implicits.biolinkPredicateDecoder(biolinkData.predicates)
// implicit val biolinkPredicateEncoder: Encoder[BiolinkPredicate] = Implicits.biolinkPredicateEncoder(biolinkData.prefixes)

import biolinkData.implicits._
val parsed = parser.parse(response).toOption.get
// logger.info("parsed: {}", parsed)
val mkg = parsed.as[MetaKnowledgeGraph]
Expand Down
15 changes: 3 additions & 12 deletions src/it/scala/org/renci/cam/it/ProdQueryServiceTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ object ProdQueryServiceTest extends DefaultRunnableSpec {
httpClient <- HttpClient.client
biolinkData <- Biolink.biolinkData
encoded = {
implicit val iriEncoder: Encoder[IRI] = Implicits.iriEncoder(biolinkData.prefixes)
implicit val iriKeyEncoder: KeyEncoder[IRI] = Implicits.iriKeyEncoder(biolinkData.prefixes)
implicit val biolinkClassEncoder: Encoder[BiolinkClass] = Implicits.biolinkClassEncoder
implicit val biolinkPredicateEncoder: Encoder[BiolinkPredicate] = Implicits.biolinkPredicateEncoder(biolinkData.prefixes)
import biolinkData.implicits._
trapiQuery.asJson.deepDropNullValues.noSpaces
}
_ = println("encoded: " + encoded)
Expand All @@ -57,10 +54,7 @@ object ProdQueryServiceTest extends DefaultRunnableSpec {
.orElseFail(new Exception("failed to traverse down to context"))

nodesMapResult = {
implicit val decoderIRI: Decoder[IRI] = Implicits.iriDecoder(biolinkData.prefixes)
implicit val keyDecoderIRI: KeyDecoder[IRI] = Implicits.iriKeyDecoder(biolinkData.prefixes)
implicit val decoderTRAPINode: Decoder[BiolinkClass] = Implicits.biolinkClassDecoder(biolinkData.classes)
implicit lazy val decoderTRAPIAttribute: Decoder[TRAPIAttribute] = deriveDecoder[TRAPIAttribute]
import biolinkData.implicits._
knowledgeGraphNodesJson.as[Map[IRI, TRAPINode]]
}

Expand All @@ -72,10 +66,7 @@ object ProdQueryServiceTest extends DefaultRunnableSpec {
.orElseFail(new Exception("failed to traverse down to context"))

edgesMapResult = {
implicit val decoderIRI: Decoder[IRI] = Implicits.iriDecoder(biolinkData.prefixes)
implicit val keyDecoderIRI: KeyDecoder[IRI] = Implicits.iriKeyDecoder(biolinkData.prefixes)
implicit val decoderBiolinkClass: Decoder[BiolinkClass] = Implicits.biolinkClassDecoder(biolinkData.classes)
implicit val decoderBiolinkPredicate: Decoder[BiolinkPredicate] = Implicits.biolinkPredicateDecoder(biolinkData.predicates)
import biolinkData.implicits._
implicit lazy val decoderTRAPIAttribute: Decoder[TRAPIAttribute] = deriveDecoder[TRAPIAttribute]
knowledgeGraphEdgesJson.as[Map[String, TRAPIEdge]]
}
Expand Down
5 changes: 1 addition & 4 deletions src/it/scala/org/renci/cam/it/QueryServiceTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ object QueryServiceTest extends DefaultRunnableSpec {
httpClient <- HttpClient.client
biolinkData <- Biolink.biolinkData
encoded = {
implicit val iriEncoder: Encoder[IRI] = Implicits.iriEncoder(biolinkData.prefixes)
implicit val iriKeyEncoder: KeyEncoder[IRI] = Implicits.iriKeyEncoder(biolinkData.prefixes)
implicit val biolinkClassEncoder: Encoder[BiolinkClass] = Implicits.biolinkClassEncoder
implicit val biolinkPredicateEncoder: Encoder[BiolinkPredicate] = Implicits.biolinkPredicateEncoder(biolinkData.prefixes)
import biolinkData.implicits._
trapiQuery.asJson.deepDropNullValues.noSpaces
}
_ = println("encoded: " + encoded)
Expand Down
9 changes: 1 addition & 8 deletions src/it/scala/org/renci/cam/it/SerializationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,7 @@ object SerializationTest extends DefaultRunnableSpec with LazyLogging {
for {
biolinkData <- Biolink.biolinkData
} yield {

implicit val iriDecoder: Decoder[IRI] = Implicits.iriDecoder(biolinkData.prefixes)
implicit val iriEncoder: Encoder[IRI] = Implicits.iriEncoder(biolinkData.prefixes)
implicit val biolinkClassEncoder: Encoder[BiolinkClass] = Implicits.biolinkClassEncoder
implicit val biolinkPredicateEncoder: Encoder[BiolinkPredicate] = Implicits.biolinkPredicateEncoder(biolinkData.prefixes)

implicit val iriKeyDecoder: KeyDecoder[IRI] = Implicits.iriKeyDecoder(biolinkData.prefixes)
implicit val iriKeyEncoder: KeyEncoder[IRI] = Implicits.iriKeyEncoder(biolinkData.prefixes)
import biolinkData.implicits._
// implicit val blClassKeyEncoder: KeyEncoder[BiolinkClass] = (blClass: BiolinkClass) => s"biolink:${blClass.shorthand}"
// implicit val blClassKeyDecoder: Decoder[TRAPIQueryNode] = new Decoder[TRAPIQueryNode] {
// final def apply(c: HCursor): Decoder.Result[TRAPIQueryNode] =
Expand Down
19 changes: 17 additions & 2 deletions src/main/scala/org/renci/cam/Biolink.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.apache.commons.lang3.StringUtils
import org.http4s.headers.Accept
import org.http4s.implicits._
import org.http4s.{MediaType, Method, Request}
import org.renci.cam.domain.{BiolinkClass, BiolinkPredicate}
import org.renci.cam.domain.{BiolinkClass, BiolinkPredicate, IRI}
import zio._
import zio.blocking.{effectBlockingIO, Blocking}
import zio.interop.catz._
Expand All @@ -21,7 +21,22 @@ object Biolink extends LazyLogging {
final case class BiolinkData(version: String,
prefixes: Map[String, String],
classes: List[BiolinkClass],
predicates: List[BiolinkPredicate])
predicates: List[BiolinkPredicate]) {

object implicits {
implicit val bcKeyDecoder: KeyDecoder[BiolinkClass] = Implicits.biolinkClassKeyDecoder(classes)
implicit val bcKeyEncoder: KeyEncoder[BiolinkClass] = Implicits.biolinkClassKeyEncoder
implicit val iriDecoder: Decoder[IRI] = Implicits.iriDecoder(prefixes)
implicit val iriEncoder: Encoder[IRI] = Implicits.iriEncoder(prefixes)
implicit val iriKeyDecoder: KeyDecoder[IRI] = Implicits.iriKeyDecoder(prefixes)
implicit val iriKeyEncoder: KeyEncoder[IRI] = Implicits.iriKeyEncoder(prefixes)
implicit val blClassDecoder: Decoder[BiolinkClass] = Implicits.biolinkClassDecoder(classes)
implicit val blClassEncoder: Encoder[BiolinkClass] = Implicits.biolinkClassEncoder
implicit val blPredicateDecoder: Decoder[BiolinkPredicate] = Implicits.biolinkPredicateDecoder(predicates)
implicit val blPredicateEncoder: Encoder[BiolinkPredicate] = Implicits.biolinkPredicateEncoder(prefixes)
}

}

def makeUtilitiesLayer: ZLayer[Any, Throwable, Has[BiolinkData]] = getBiolinkData.toLayer

Expand Down
29 changes: 3 additions & 26 deletions src/main/scala/org/renci/cam/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cats.implicits._
import com.typesafe.scalalogging.LazyLogging
import io.circe._
import io.circe.generic.auto._
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.yaml.syntax._
import org.http4s._
import org.http4s.blaze.server.BlazeServerBuilder
Expand Down Expand Up @@ -53,19 +54,7 @@ object Server extends App with LazyLogging {
.errorOut(stringBody)
.out(
{

implicit val bcKeyDecoder: KeyDecoder[BiolinkClass] = Implicits.biolinkClassKeyDecoder(biolinkData.classes)
implicit val bcKeyEncoder: KeyEncoder[BiolinkClass] = Implicits.biolinkClassKeyEncoder

implicit val iriDecoder: Decoder[IRI] = Implicits.iriDecoder(biolinkData.prefixes)
implicit val iriEncoder: Encoder[IRI] = Implicits.iriEncoder(biolinkData.prefixes)

implicit val blClassDecoder: Decoder[BiolinkClass] = Implicits.biolinkClassDecoder(biolinkData.classes)
implicit val blClassEncoder: Encoder[BiolinkClass] = Implicits.biolinkClassEncoder

implicit val blPredicateDecoder: Decoder[BiolinkPredicate] = Implicits.biolinkPredicateDecoder(biolinkData.predicates)
implicit val blPredicateEncoder: Encoder[BiolinkPredicate] = Implicits.biolinkPredicateEncoder(biolinkData.prefixes)

import biolinkData.implicits._
jsonBody[MetaKnowledgeGraph]
}
)
Expand All @@ -86,19 +75,7 @@ object Server extends App with LazyLogging {
for {
biolinkData <- biolinkData
} yield {
implicit val iriDecoder: Decoder[IRI] = Implicits.iriDecoder(biolinkData.prefixes)
implicit val iriEncoder: Encoder[IRI] = Implicits.iriEncoder(biolinkData.prefixes)

implicit val iriKeyEncoder: KeyEncoder[IRI] = Implicits.iriKeyEncoder(biolinkData.prefixes)
implicit val iriKeyDecoder: KeyDecoder[IRI] = Implicits.iriKeyDecoder(biolinkData.prefixes)

implicit val biolinkClassEncoder: Encoder[BiolinkClass] = Implicits.biolinkClassEncoder
implicit val biolinkClassDecoder: Decoder[BiolinkClass] = Implicits.biolinkClassDecoder(biolinkData.classes)

implicit val biolinkPredicateEncoder: Encoder[BiolinkPredicate] = Implicits.biolinkPredicateEncoder(biolinkData.prefixes)
implicit val biolinkPredicateDecoder: Decoder[List[BiolinkPredicate]] =
Implicits.predicateOrPredicateListDecoder(biolinkData.predicates)

import biolinkData.implicits._
val example = {
// This example asks what biological process or activities positively regulate GO:0004707
// (MAP kinase activity, see http://purl.obolibrary.org/obo/GO_0004707)
Expand Down
13 changes: 2 additions & 11 deletions src/test/scala/org/renci/cam/test/TRAPITest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ object TRAPITest extends DefaultRunnableSpec with LazyLogging {

trapiResponse <- ZIO.fromEither(
{
implicit val decoderIRI: Decoder[IRI] = Implicits.iriDecoder(biolinkData.prefixes)
implicit val keyDecoderIRI: KeyDecoder[IRI] = Implicits.iriKeyDecoder(biolinkData.prefixes)
implicit val decoderBiolinkClass: Decoder[BiolinkClass] = Implicits.biolinkClassDecoder(biolinkData.classes)
implicit val decoderBiolinkPredicate: Decoder[BiolinkPredicate] =
Implicits.biolinkPredicateDecoder(biolinkData.predicates)
import biolinkData.implicits._
implicit lazy val decoderTRAPIAttribute: Decoder[TRAPIAttribute] = deriveDecoder[TRAPIAttribute]

trapiResponseJson.as[TRAPIResponse]
Expand Down Expand Up @@ -152,13 +148,8 @@ object TRAPITest extends DefaultRunnableSpec with LazyLogging {

trapiResponse <- ZIO.fromEither(
{
implicit val decoderIRI: Decoder[IRI] = Implicits.iriDecoder(biolinkData.prefixes)
implicit val keyDecoderIRI: KeyDecoder[IRI] = Implicits.iriKeyDecoder(biolinkData.prefixes)
implicit val decoderBiolinkClass: Decoder[BiolinkClass] = Implicits.biolinkClassDecoder(biolinkData.classes)
implicit val decoderBiolinkPredicate: Decoder[BiolinkPredicate] =
Implicits.biolinkPredicateDecoder(biolinkData.predicates)
import biolinkData.implicits._
implicit lazy val decoderTRAPIAttribute: Decoder[TRAPIAttribute] = deriveDecoder[TRAPIAttribute]

trapiResponseJson.as[TRAPIResponse]
}
)
Expand Down