Skip to content

Commit

Permalink
removing jsonb get method
Browse files Browse the repository at this point in the history
  • Loading branch information
TebaleloS committed Jul 9, 2024
1 parent f0878b0 commit 479ec33
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
1 change: 0 additions & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ object Dependencies {
val tapirOrg = "com.softwaremill.sttp.tapir"
val http4sOrg = "org.http4s"
val faDbOrg = "za.co.absa.fa-db"
val playOrg = "org.playframework"
val sbtOrg = "com.github.sbt"
val logbackOrg = "ch.qos.logback"
val awsSdkOrg = "software.amazon.awssdk"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,46 @@ package za.co.absa.atum.server.api.database

import cats.Show
import cats.data.NonEmptyList
import doobie.{Get, Put}
import doobie.postgres.implicits._
import doobie.{Get, Put}
import io.circe.{Json => CirceJson}
import org.postgresql.jdbc.PgArray
import org.postgresql.util.PGobject
import io.circe.parser._

import scala.util.{Failure, Success, Try}
import scala.util.Try

object DoobieImplicits {
package object DoobieImplicits {

private implicit val showPgArray: Show[PgArray] = Show.fromToString

implicit val getMapWithOptionStringValues: Get[Map[String, Option[String]]] = Get[Map[String, String]]
.tmap(map => map.map { case (k, v) => k -> Option(v) })
.tmap(map => map.map { case (k, v) => k -> Option(v) })

private def circeJsonListToPGJsonArrayString(jsonList: List[CirceJson]): String = {
val arrayElements = jsonList.map { x =>
// Convert to compact JSON string and escape inner quotes
val escapedJsonString = x.noSpaces.replace("\"", "\\\"")
// Wrap in double quotes for the array element
s"\"$escapedJsonString\""
s""""$escapedJsonString""""
}

arrayElements.mkString("{", ",", "}")
}

private def pgArrayToListOfCirceJson(pgArray: PgArray): Either[String, List[CirceJson]] = {
Try(pgArray.getArray.asInstanceOf[List[String]].map(CirceJson.fromString)) match {
case Failure(exception) => Left(exception.toString)
case Success(value) => Right(value)
Try {
Option(pgArray.getArray) match {
case Some(array: Array[_]) => array.collect {
case str: String => parse(str).toTry.get
case other => parse(other.toString).toTry.get
}.toList
case None => List.empty[CirceJson]
case _ => throw new IllegalArgumentException("Unexpected type encountered.")
}
}
.toEither
.left.map(_.getMessage)
}

object Sequence {
Expand Down Expand Up @@ -98,13 +107,6 @@ object DoobieImplicits {
}
}

implicit val jsonbArrayGet: Get[List[CirceJson]] = {
Get.Advanced
.other[PgArray](
NonEmptyList.of("jsonb[]")
)
.temap(pgArray => pgArrayToListOfCirceJson(pgArray) )
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import za.co.absa.atum.server.api.database.DoobieImplicits.Sequence.get

import doobie.postgres.implicits._
import doobie.postgres.circe.jsonb.implicits.jsonbPut
import doobie.postgres.circe.jsonb.implicits.jsonbGet
import doobie.postgres.circe.json.implicits.jsonGet
import io.circe.syntax.EncoderOps

class GetFlowCheckpoints(implicit schema: DBSchema, dbEngine: DoobieEngine[Task])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import io.circe.syntax._
import za.co.absa.atum.server.api.database.DoobieImplicits.Sequence.get
import doobie.postgres.implicits._
import doobie.postgres.circe.jsonb.implicits.jsonbPut
import doobie.postgres.circe.jsonb.implicits.jsonbGet
import doobie.postgres.circe.json.implicits.jsonGet

class GetPartitioningCheckpoints (implicit schema: DBSchema, dbEngine: DoobieEngine[Task])
extends DoobieMultipleResultFunction[CheckpointQueryDTO, CheckpointFromDB, Task] {
Expand Down

0 comments on commit 479ec33

Please sign in to comment.