diff --git a/modules/core/src/main/scala-3/tethys/JsonConfiguration.scala b/modules/core/src/main/scala-3/tethys/JsonConfiguration.scala new file mode 100644 index 0000000..8adfac6 --- /dev/null +++ b/modules/core/src/main/scala-3/tethys/JsonConfiguration.scala @@ -0,0 +1,12 @@ +package tethys + +trait JsonConfiguration: + def fieldStyle(fieldStyle: FieldStyle): JsonConfiguration + + def strict: JsonConfiguration + + +object JsonConfiguration: + @scala.annotation.compileTimeOnly("JsonConfiguration should be declared as inline given") + def default: JsonConfiguration = throw IllegalAccessException() + diff --git a/modules/core/src/test/scala-3/tethys/derivation/DerivationSpec.scala b/modules/core/src/test/scala-3/tethys/derivation/DerivationSpec.scala index 981dac1..a9630f9 100644 --- a/modules/core/src/test/scala-3/tethys/derivation/DerivationSpec.scala +++ b/modules/core/src/test/scala-3/tethys/derivation/DerivationSpec.scala @@ -813,4 +813,62 @@ class DerivationSpec extends AnyFlatSpec with Matchers { ) shouldBe SubChild(3) } + it should "apply configuration for multiple case classes" in { + + inline given JsonConfiguration = JsonConfiguration.default + .fieldStyle(FieldStyle.LowerSnakeCase) + + case class First(firstField: String) derives JsonWriter, JsonReader + case class Second(secondField: String) derives JsonWriter, JsonReader + + val first = First("abc") + val second = Second("bcd") + val firstJson = obj("first_field" -> "abc") + val secondJson = obj("second_field" -> "bcd") + + first.asTokenList shouldBe firstJson + second.asTokenList shouldBe secondJson + + read[First](firstJson) shouldBe first + read[Second](secondJson) shouldBe second + } + + it should "apply configuration when derive product recursively" in { + inline given JsonConfiguration = JsonConfiguration.default + .fieldStyle(FieldStyle.LowerSnakeCase) + + case class Inner(innerField: String) + case class Outer(outerField: Inner) derives JsonWriter, JsonReader + + val model = Outer(Inner("foo")) + val json = obj("outer_field" -> obj("inner_field" -> "foo")) + + model.asTokenList shouldBe json + read[Outer](json) shouldBe model + } + + it should "apply configuration when derive sum recursively" in { + inline given JsonConfiguration = JsonConfiguration.default + .fieldStyle(FieldStyle.LowerSnakeCase) + + enum Choice(@selector val select: Int) derives JsonReader, JsonWriter: + case First(firstField: Int) extends Choice(0) + case Second(secondField: String) extends Choice(1) + + + val first = Choice.First(1) + val second = Choice.Second("foo") + val firstJson = obj("select" -> 0, "first_field" -> 1) + val secondJson = obj("select" -> 1, "second_field" -> "foo") + + first.asTokenList shouldBe firstJson + second.asTokenList shouldBe secondJson + + read[Choice](firstJson) shouldBe first + read[Choice](secondJson) shouldBe second + } + + + + }