Skip to content

Commit a836c12

Browse files
author
Matteo Di Pirro
committed
SCALA-400 make Command ADT more complex
1 parent 38b8449 commit a836c12

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

zio-2/src/main/scala/com/baeldung/scala/zio/json/BaeldungZIOJson.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import zio.json.*
55
sealed trait Command
66
case class Start(timeout: Long) extends Command
77
case object Stop extends Command
8+
case class Kill(reason: String, force: Boolean) extends Command
89

910
@jsonDiscriminator("type")
1011
sealed trait Command2
1112
case class Start2(timeout: Long) extends Command2
1213
case object Stop2 extends Command2
14+
case class Kill2(reason: String, force: Boolean) extends Command2
1315

1416
object Start {
1517
implicit val encoder: JsonEncoder[Start] = DeriveJsonEncoder.gen[Start]

zio-2/src/test/scala/com/baeldung/scala/zio/json/DecodingSpec.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class DecodingSpec extends AnyWordSpec with Matchers {
2525
Start(100)
2626
)
2727
"""{"Stop":{}}""".fromJson[Command] shouldBe Right(Stop)
28+
"""{"Kill":{"reason":"Random reason","force":false}}"""
29+
.fromJson[Command] shouldBe Right(
30+
Kill("Random reason", false)
31+
)
2832
}
2933

3034
"decode Stop to JSON" in {
@@ -35,13 +39,21 @@ class DecodingSpec extends AnyWordSpec with Matchers {
3539
Start(100)
3640
)
3741
""""Stop"""".fromJson[Stop.type] shouldBe Right(Stop)
42+
"""{"Kill":{"reason":"Random reason","force":false}}"""
43+
.fromJson[Command] shouldBe Right(
44+
Kill("Random reason", false)
45+
)
3846
}
3947

4048
"use a discriminator" in {
4149
"""{"type":"Start2","timeout":100}""".fromJson[Command2] shouldBe Right(
4250
Start2(100)
4351
)
4452
"""{"type":"Stop2"}""".fromJson[Command2] shouldBe Right(Stop2)
53+
"""{"type":"Kill2","reason":"Random reason","force":false}"""
54+
.fromJson[Command2] shouldBe Right(
55+
Kill2("Random reason", false)
56+
)
4557
}
4658

4759
"fail if there's no discriminator" in {

zio-2/src/test/scala/com/baeldung/scala/zio/json/EncodingSpec.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class EncodingSpec extends AnyWordSpec with Matchers {
1313
"encode an ADT to JSON" in {
1414
(Start(100): Command).toJson shouldBe """{"Start":{"timeout":100}}"""
1515
(Stop: Command).toJson shouldBe """{"Stop":{}}"""
16+
(Kill(
17+
reason = "Random reason",
18+
force = false
19+
): Command).toJson shouldBe """{"Kill":{"reason":"Random reason","force":false}}"""
1620
}
1721

1822
"encode Stop to JSON" in {
@@ -21,13 +25,21 @@ class EncodingSpec extends AnyWordSpec with Matchers {
2125

2226
(Start(100): Command).toJson shouldBe """{"Start":{"timeout":100}}"""
2327
Stop.toJson shouldBe """"Stop""""
28+
(Kill(
29+
reason = "Random reason",
30+
force = false
31+
): Command).toJson shouldBe """{"Kill":{"reason":"Random reason","force":false}}"""
2432
}
2533

2634
"use a discriminator" in {
2735
(Start2(
2836
100
2937
): Command2).toJson shouldBe """{"type":"Start2","timeout":100}"""
3038
(Stop2: Command2).toJson shouldBe """{"type":"Stop2"}"""
39+
(Kill2(
40+
reason = "Random reason",
41+
force = false
42+
): Command2).toJson shouldBe """{"type":"Kill2","reason":"Random reason","force":false}"""
3143
}
3244
}
3345
}

0 commit comments

Comments
 (0)