diff --git a/src/it/scala/org/renci/cam/it/ExampleQueriesEndpointTest.scala b/src/it/scala/org/renci/cam/it/ExampleQueriesEndpointTest.scala index 98505a19..ae228768 100644 --- a/src/it/scala/org/renci/cam/it/ExampleQueriesEndpointTest.scala +++ b/src/it/scala/org/renci/cam/it/ExampleQueriesEndpointTest.scala @@ -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] } ) @@ -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}") @@ -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] } ) diff --git a/src/it/scala/org/renci/cam/it/ImplicitsTest.scala b/src/it/scala/org/renci/cam/it/ImplicitsTest.scala index c89482f1..fc7e337e 100644 --- a/src/it/scala/org/renci/cam/it/ImplicitsTest.scala +++ b/src/it/scala/org/renci/cam/it/ImplicitsTest.scala @@ -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")) } @@ -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)( diff --git a/src/it/scala/org/renci/cam/it/MetaKnowledgeGraphServiceTest.scala b/src/it/scala/org/renci/cam/it/MetaKnowledgeGraphServiceTest.scala index 17a6a2b3..df569f77 100644 --- a/src/it/scala/org/renci/cam/it/MetaKnowledgeGraphServiceTest.scala +++ b/src/it/scala/org/renci/cam/it/MetaKnowledgeGraphServiceTest.scala @@ -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] diff --git a/src/it/scala/org/renci/cam/it/ProdQueryServiceTest.scala b/src/it/scala/org/renci/cam/it/ProdQueryServiceTest.scala index 5d7b4450..b4ad1632 100644 --- a/src/it/scala/org/renci/cam/it/ProdQueryServiceTest.scala +++ b/src/it/scala/org/renci/cam/it/ProdQueryServiceTest.scala @@ -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) @@ -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]] } @@ -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]] } diff --git a/src/it/scala/org/renci/cam/it/QueryServiceTest.scala b/src/it/scala/org/renci/cam/it/QueryServiceTest.scala index af9ca340..034a5f8f 100644 --- a/src/it/scala/org/renci/cam/it/QueryServiceTest.scala +++ b/src/it/scala/org/renci/cam/it/QueryServiceTest.scala @@ -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) diff --git a/src/it/scala/org/renci/cam/it/SerializationTest.scala b/src/it/scala/org/renci/cam/it/SerializationTest.scala index b679c72f..4f1eb47c 100644 --- a/src/it/scala/org/renci/cam/it/SerializationTest.scala +++ b/src/it/scala/org/renci/cam/it/SerializationTest.scala @@ -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] = diff --git a/src/main/scala/org/renci/cam/Biolink.scala b/src/main/scala/org/renci/cam/Biolink.scala index 70fe71e3..eeb6720b 100644 --- a/src/main/scala/org/renci/cam/Biolink.scala +++ b/src/main/scala/org/renci/cam/Biolink.scala @@ -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._ @@ -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 diff --git a/src/main/scala/org/renci/cam/Server.scala b/src/main/scala/org/renci/cam/Server.scala index 742d4524..2e6184f6 100644 --- a/src/main/scala/org/renci/cam/Server.scala +++ b/src/main/scala/org/renci/cam/Server.scala @@ -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 @@ -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] } ) @@ -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) diff --git a/src/test/scala/org/renci/cam/test/TRAPITest.scala b/src/test/scala/org/renci/cam/test/TRAPITest.scala index 60812077..2217daa8 100644 --- a/src/test/scala/org/renci/cam/test/TRAPITest.scala +++ b/src/test/scala/org/renci/cam/test/TRAPITest.scala @@ -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] @@ -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] } )