Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circe support - @noflat has no effect when KebsCirce.NoFlat is not used #178

Open
pk044 opened this issue Dec 1, 2021 · 0 comments
Open
Labels

Comments

@pk044
Copy link
Collaborator

pk044 commented Dec 1, 2021

  object KebsProtocol extends KebsCirce
  import KebsProtocol._

  @noflat case class F(i: Int)

  test("No-flat format - annotation") {

    val decoder = implicitly[Decoder[F]]
    val encoder = implicitly[Encoder[F]]
    decoder.apply(Json.fromFields(Seq("i" -> Json.fromInt(10))).hcursor) shouldBe Right(F(10))
    encoder.apply(F(10)) shouldBe Json.fromFields(Seq("i" -> Json.fromInt(10)))
  }

Test above results in failure. Currently the support for noflat works only when NoFlat trait is mixed in.

In order to fix this, the following code should be changed so @noflat is taken into account when only KebsCirce trait is extended:

trait KebsCirce extends AutoDerivation {
  implicit def flatDecoder[T, A](implicit rep: CaseClass1Rep[T, A], decoder: Decoder[A]): Decoder[T] =
    decoder.emap(obj => Try(rep.apply(obj)).toEither.left.map(_.getMessage))
  implicit def flatEncoder[T, A](implicit rep: CaseClass1Rep[T, A], encoder: Encoder[A]): Encoder[T] =
    encoder.contramap(rep.unapply)
}

KebsSpray is already handling this - it's worth taking a look: https://github.com/theiterators/kebs/blob/bb83e41ec2e3a217719c2da083e99e8ca4eee0ba/spray-json/src/main/scala/pl/iterators/kebs/json/KebsSpray.scala

@pk044 pk044 added the bug label Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant