Skip to content

Commit

Permalink
#188: moving the circe-related implicit conversions to a companion ob…
Browse files Browse the repository at this point in the history
…ject of a given DTO
  • Loading branch information
lsulak committed Jun 14, 2024
1 parent d6fe29d commit 8d777f6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package za.co.absa.atum.model.dto

import io.circe.{Decoder, Encoder}

case class MeasureResultDTO(
mainValue: MeasureResultDTO.TypedValue,
supportValues: Map[String, MeasureResultDTO.TypedValue] = Map.empty
Expand All @@ -36,4 +38,29 @@ object MeasureResultDTO {
case object Double extends ResultValueType
}


implicit val encodeResultValueType: Encoder[MeasureResultDTO.ResultValueType] = Encoder.encodeString.contramap {
case MeasureResultDTO.ResultValueType.String => "String"
case MeasureResultDTO.ResultValueType.Long => "Long"
case MeasureResultDTO.ResultValueType.BigDecimal => "BigDecimal"
case MeasureResultDTO.ResultValueType.Double => "Double"
}

implicit val decodeResultValueType: Decoder[MeasureResultDTO.ResultValueType] = Decoder.decodeString.emap {
case "String" => Right(MeasureResultDTO.ResultValueType.String)
case "Long" => Right(MeasureResultDTO.ResultValueType.Long)
case "BigDecimal" => Right(MeasureResultDTO.ResultValueType.BigDecimal)
case "Double" => Right(MeasureResultDTO.ResultValueType.Double)
case other => Left(s"Cannot decode $other as ResultValueType")
}

implicit val encodeTypedValue: Encoder[MeasureResultDTO.TypedValue] =
Encoder.forProduct2("value", "valueType")(tv => (tv.value, tv.valueType))

implicit val decodeTypedValue: Decoder[MeasureResultDTO.TypedValue] =
Decoder.forProduct2("value", "valueType")(MeasureResultDTO.TypedValue.apply)

implicit val decodeMeasureResultDTO: Decoder[MeasureResultDTO] =
Decoder.forProduct2("mainValue", "supportValues")(MeasureResultDTO.apply)

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ import cats.Show
import cats.data.NonEmptyList
import doobie.{Get, Put}
import doobie.postgres.implicits._
import io.circe.{Decoder, Encoder}
import org.postgresql.jdbc.PgArray
import org.postgresql.util.PGobject
import za.co.absa.atum.model.dto.MeasureResultDTO

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

Expand Down Expand Up @@ -157,27 +155,4 @@ object DoobieImplicits {
}
}

implicit val encodeResultValueType: Encoder[MeasureResultDTO.ResultValueType] = Encoder.encodeString.contramap {
case MeasureResultDTO.ResultValueType.String => "String"
case MeasureResultDTO.ResultValueType.Long => "Long"
case MeasureResultDTO.ResultValueType.BigDecimal => "BigDecimal"
case MeasureResultDTO.ResultValueType.Double => "Double"
}

implicit val decodeResultValueType: Decoder[MeasureResultDTO.ResultValueType] = Decoder.decodeString.emap {
case "String" => Right(MeasureResultDTO.ResultValueType.String)
case "Long" => Right(MeasureResultDTO.ResultValueType.Long)
case "BigDecimal" => Right(MeasureResultDTO.ResultValueType.BigDecimal)
case "Double" => Right(MeasureResultDTO.ResultValueType.Double)
case other => Left(s"Cannot decode $other as ResultValueType")
}

implicit val encodeTypedValue: Encoder[MeasureResultDTO.TypedValue] =
Encoder.forProduct2("value", "valueType")(tv => (tv.value, tv.valueType))

implicit val decodeTypedValue: Decoder[MeasureResultDTO.TypedValue] =
Decoder.forProduct2("value", "valueType")(MeasureResultDTO.TypedValue.apply)

implicit val decodeMeasureResultDTO: Decoder[MeasureResultDTO] =
Decoder.forProduct2("mainValue", "supportValues")(MeasureResultDTO.apply)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ package za.co.absa.atum.server.model

import za.co.absa.atum.model.dto.{CheckpointDTO, MeasureDTO, MeasureResultDTO, MeasurementDTO, PartitioningDTO}
import io.circe.{DecodingFailure, Json}
import io.circe.generic.auto._

import java.time.ZonedDateTime
import java.util.UUID

import za.co.absa.atum.server.api.database.DoobieImplicits.decodeResultValueType

case class CheckpointFromDB(
idCheckpoint: UUID,
checkpointName: String,
Expand Down

0 comments on commit 8d777f6

Please sign in to comment.