Skip to content

Commit

Permalink
Fix health encoder/decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Dec 25, 2018
1 parent 138e23b commit 86a9f12
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions modules/circe/src/main/scala/sup/modules/circe.scala
Expand Up @@ -6,8 +6,9 @@ import sup.data.{Report, Tagged}
import sup.{Health, HealthResult}

object circe {
implicit val healthCirceEncoder: Encoder[Health] = deriveEncoder
implicit val healthCirceDecoder: Decoder[Health] = deriveDecoder
implicit val healthCirceEncoder: Encoder[Health] = Encoder[String].contramap(_.toString)
implicit val healthCirceDecoder: Decoder[Health] =
Decoder[String].emap(s => Health.fromString(s).toRight(s"$s is not a valid ${Health.getClass.getName}"))

implicit def taggedCirceEncoder[Tag: Encoder, H: Encoder]: Encoder[Tagged[Tag, H]] = deriveEncoder
implicit def taggedCirceDecoder[Tag: Decoder, H: Decoder]: Decoder[Tagged[Tag, H]] = deriveDecoder
Expand Down
8 changes: 8 additions & 0 deletions modules/core/src/main/scala/sup/Health.scala
@@ -1,6 +1,8 @@
package sup

import cats.kernel.CommutativeMonoid
import cats.{Eq, Show}
import cats.implicits._

/**
* The component's health status. It can only be Healthy or Sick - there's no middle ground (no Unknown state).
Expand All @@ -25,6 +27,12 @@ object Health {
case false => Sick
}

val fromString: String => Option[Health] = {
case "Healthy" => Healthy.some
case "Sick" => Sick.some
case _ => None
}

implicit val eq: Eq[Health] = Eq.fromUniversalEquals

/**
Expand Down

0 comments on commit 86a9f12

Please sign in to comment.