From 186439aa64704f0c637c7b14153da67bf4962f03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Pra=C5=BCak?= Date: Wed, 28 Feb 2024 19:13:36 +0100 Subject: [PATCH] Fix #407 - recurrent type encoder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --------- Co-authored-by: Łukasz Biały --- .../main/scala/besom/internal/codecs.scala | 4 ++-- .../scala/besom/internal/EncoderTest.scala | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/besom/internal/codecs.scala b/core/src/main/scala/besom/internal/codecs.scala index ba2c9843..a85f7a5a 100644 --- a/core/src/main/scala/besom/internal/codecs.scala +++ b/core/src/main/scala/besom/internal/codecs.scala @@ -689,13 +689,13 @@ object Encoder: import Constants.* import besom.json.* - def encoderSum[A](mirror: Mirror.SumOf[A], nameEncoderPairs: List[(String, Encoder[?])]): Encoder[A] = + def encoderSum[A](mirror: Mirror.SumOf[A], nameEncoderPairs: => List[(String, Encoder[?])]): Encoder[A] = new Encoder[A]: // TODO We only serialize dumb enums!! // private val encoderMap = nameEncoderPairs.toMap override def encode(a: A): Result[(Set[Resource], Value)] = Result.pure(Set.empty -> a.toString.asValue) - def encoderProduct[A](nameEncoderPairs: List[(String, Encoder[?])]): Encoder[A] = + def encoderProduct[A](nameEncoderPairs: => List[(String, Encoder[?])]): Encoder[A] = new Encoder[A]: override def encode(a: A): Result[(Set[Resource], Value)] = Result diff --git a/core/src/test/scala/besom/internal/EncoderTest.scala b/core/src/test/scala/besom/internal/EncoderTest.scala index 3a9cddc9..68b4d581 100644 --- a/core/src/test/scala/besom/internal/EncoderTest.scala +++ b/core/src/test/scala/besom/internal/EncoderTest.scala @@ -454,3 +454,22 @@ object Regression383Test: besom.internal.Encoder.derived[SecretArgs] given argsEncoder(using besom.types.Context): besom.types.ArgsEncoder[SecretArgs] = besom.internal.ArgsEncoder.derived[SecretArgs] +end Regression383Test + +class RecurrentArgsTest extends munit.FunSuite with ValueAssertions: + case class Recurrent(value: Option[Recurrent]) + object Recurrent: + given encoder(using besom.types.Context): besom.types.Encoder[Recurrent] = + besom.internal.Encoder.derived[Recurrent] + + test("encode recurrent type") { + given Context = DummyContext().unsafeRunSync() + val e = summon[Encoder[Recurrent]] + + val (_, encoded) = e.encode(Recurrent(Some(Recurrent(Some(Recurrent(Some(Recurrent(None)))))))).unsafeRunSync() + + val expected = + Map("value" -> Map("value" -> Map("value" -> Map().asValue).asValue).asValue).asValue + assertEqualsValue(encoded, expected) + } +end RecurrentArgsTest