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

Adding generic configuration #331

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions modules/core/src/main/scala-3/tethys/JsonConfiguration.scala
Original file line number Diff line number Diff line change
@@ -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()

Original file line number Diff line number Diff line change
Expand Up @@ -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
}




}
Loading