From e54ea8cbcbbbbe38000004d902f4dd123304b239 Mon Sep 17 00:00:00 2001 From: Michael Nedokushev Date: Mon, 27 Nov 2023 19:49:02 +0000 Subject: [PATCH 1/2] Remove @simpleEnum from code and tests --- .../test/scala/zio/dynamodb/ProjectionExpressionSpec.scala | 2 -- .../src/test/scala/zio/dynamodb/codec/ItemDecoderSpec.scala | 4 ++-- .../src/test/scala/zio/dynamodb/codec/ItemEncoderSpec.scala | 6 +++--- dynamodb/src/test/scala/zio/dynamodb/codec/models.scala | 3 +-- .../examples/TypeSafeRoundTripSerialisationExample.scala | 3 +-- .../main/scala/zio/dynamodb/examples/model/Student.scala | 2 -- 6 files changed, 7 insertions(+), 13 deletions(-) diff --git a/dynamodb/src/test/scala/zio/dynamodb/ProjectionExpressionSpec.scala b/dynamodb/src/test/scala/zio/dynamodb/ProjectionExpressionSpec.scala index b48f40aee..fb3bda84a 100644 --- a/dynamodb/src/test/scala/zio/dynamodb/ProjectionExpressionSpec.scala +++ b/dynamodb/src/test/scala/zio/dynamodb/ProjectionExpressionSpec.scala @@ -1,7 +1,6 @@ package zio.dynamodb import zio.dynamodb.ProjectionExpression.{ $, mapElement, MapElement, Root } -import zio.schema.annotation.simpleEnum import zio.schema.{ DeriveSchema, Schema } import zio.test.Assertion._ import zio.test.{ assert, assertTrue, ZIOSpecDefault } @@ -15,7 +14,6 @@ object ProjectionExpressionSpec extends ZIOSpecDefault { private val groups = "groups" private val payment = "payment" - @simpleEnum sealed trait Payment object Payment { case object CreditCard extends Payment diff --git a/dynamodb/src/test/scala/zio/dynamodb/codec/ItemDecoderSpec.scala b/dynamodb/src/test/scala/zio/dynamodb/codec/ItemDecoderSpec.scala index d4cd4ff8d..d885a655c 100644 --- a/dynamodb/src/test/scala/zio/dynamodb/codec/ItemDecoderSpec.scala +++ b/dynamodb/src/test/scala/zio/dynamodb/codec/ItemDecoderSpec.scala @@ -284,14 +284,14 @@ object ItemDecoderSpec extends ZIOSpecDefault with CodecTestFixtures { assert(actual)(isRight(equalTo(PreBilled(id = 1, s = "foobar")))) }, - test("decodes case object only enum with @simpleEnum annotation and without @caseName annotation") { + test("decodes case object only enum without @caseName annotation") { val item: Item = Item(Map("enum" -> AttributeValue.String("ONE"))) val actual = DynamoDBQuery.fromItem[WithCaseObjectOnlyEnum](item) assert(actual)(isRight(equalTo(WithCaseObjectOnlyEnum(WithCaseObjectOnlyEnum.ONE)))) }, - test("decodes case object only enum with @simpleEnum annotation and @caseName annotation of '2'") { + test("decodes case object only enum with @caseName annotation of '2'") { val item: Item = Item(Map("enum" -> AttributeValue.String("2"))) val actual = DynamoDBQuery.fromItem[WithCaseObjectOnlyEnum](item) diff --git a/dynamodb/src/test/scala/zio/dynamodb/codec/ItemEncoderSpec.scala b/dynamodb/src/test/scala/zio/dynamodb/codec/ItemEncoderSpec.scala index c5f2832ba..38c6cb23d 100644 --- a/dynamodb/src/test/scala/zio/dynamodb/codec/ItemEncoderSpec.scala +++ b/dynamodb/src/test/scala/zio/dynamodb/codec/ItemEncoderSpec.scala @@ -218,21 +218,21 @@ object ItemEncoderSpec extends ZIOSpecDefault with CodecTestFixtures { assert(item)(equalTo(expectedItem)) }, - test("encodes case object only enum with @simpleEnum annotation") { + test("encodes case object only enum") { val expectedItem: Item = Item(Map("enum" -> AttributeValue.String("ONE"))) val item = DynamoDBQuery.toItem(WithCaseObjectOnlyEnum(WithCaseObjectOnlyEnum.ONE)) assert(item)(equalTo(expectedItem)) }, - test("encodes case object only enum with @simpleEnum annotation and @caseName annotation of '2'") { + test("encodes case object only enum with @caseName annotation of '2'") { val expectedItem: Item = Item(Map("enum" -> AttributeValue.String("2"))) val item = DynamoDBQuery.toItem(WithCaseObjectOnlyEnum(WithCaseObjectOnlyEnum.TWO)) assert(item)(equalTo(expectedItem)) }, - test("encodes enum and honours @caseName annotation when there is no @simpleEnum annotation") { + test("encodes enum and honours @caseName annotation") { val expectedItem: Item = Item("enum" -> Item(Map("1" -> AttributeValue.Null))) val item = DynamoDBQuery.toItem(WithEnumWithoutDiscriminator(WithEnumWithoutDiscriminator.ONE)) diff --git a/dynamodb/src/test/scala/zio/dynamodb/codec/models.scala b/dynamodb/src/test/scala/zio/dynamodb/codec/models.scala index 4d7cc95bc..dbd349971 100644 --- a/dynamodb/src/test/scala/zio/dynamodb/codec/models.scala +++ b/dynamodb/src/test/scala/zio/dynamodb/codec/models.scala @@ -1,6 +1,6 @@ package zio.dynamodb.codec -import zio.schema.annotation.{ caseName, discriminatorName, fieldName, simpleEnum } +import zio.schema.annotation.{ caseName, discriminatorName, fieldName } import zio.schema.{ DeriveSchema, Schema } import java.time.Instant @@ -57,7 +57,6 @@ object WithDiscriminatedEnum { implicit val schema: Schema[WithDiscriminatedEnum] = DeriveSchema.gen[WithDiscriminatedEnum] } -@simpleEnum sealed trait CaseObjectOnlyEnum final case class WithCaseObjectOnlyEnum(`enum`: CaseObjectOnlyEnum) object WithCaseObjectOnlyEnum { diff --git a/examples/src/main/scala/zio/dynamodb/examples/TypeSafeRoundTripSerialisationExample.scala b/examples/src/main/scala/zio/dynamodb/examples/TypeSafeRoundTripSerialisationExample.scala index 32c6cc8ef..27d443e1f 100644 --- a/examples/src/main/scala/zio/dynamodb/examples/TypeSafeRoundTripSerialisationExample.scala +++ b/examples/src/main/scala/zio/dynamodb/examples/TypeSafeRoundTripSerialisationExample.scala @@ -11,7 +11,7 @@ import zio.dynamodb.examples.TypeSafeRoundTripSerialisationExample.Invoice.{ Product } import zio.dynamodb.{ DynamoDBExecutor, DynamoDBQuery, PrimaryKey } -import zio.schema.annotation.{ caseName, discriminatorName, simpleEnum } +import zio.schema.annotation.{ caseName, discriminatorName } import zio.schema.{ DeriveSchema, Schema } import java.time.Instant @@ -26,7 +26,6 @@ object TypeSafeRoundTripSerialisationExample extends ZIOAppDefault { def id: String } object Invoice { - @simpleEnum sealed trait PaymentType object PaymentType { @caseName("debit") diff --git a/examples/src/main/scala/zio/dynamodb/examples/model/Student.scala b/examples/src/main/scala/zio/dynamodb/examples/model/Student.scala index 829141017..f79f7dff8 100644 --- a/examples/src/main/scala/zio/dynamodb/examples/model/Student.scala +++ b/examples/src/main/scala/zio/dynamodb/examples/model/Student.scala @@ -6,9 +6,7 @@ import zio.schema.DeriveSchema import java.time.Instant import zio.schema.Schema import zio.dynamodb.KeyConditionExpr -import zio.schema.annotation.simpleEnum -@simpleEnum sealed trait Payment object Payment { From 1a9fdcda1ad92d75d89cecc2db8400a5f2a17cc4 Mon Sep 17 00:00:00 2001 From: Michael Nedokushev Date: Mon, 27 Nov 2023 20:00:35 +0000 Subject: [PATCH 2/2] Update doc --- docs/codec-customization.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/codec-customization.md b/docs/codec-customization.md index ce86a9a88..c9b1aca02 100644 --- a/docs/codec-customization.md +++ b/docs/codec-customization.md @@ -37,13 +37,13 @@ Here an intermediate map is used to identify the member of `TraficLight` ie `Map Note that the `Null` is used as in this case we do not care about the value. # Customising encodings via annotations -Encodings can be customised through the use of the following annotations `@discriminatorName`, `@simpleEnum` and `@fieldName`. +Encodings can be customised through the use of the following annotations `@discriminatorName` and `@fieldName`. These annotations are useful when working with a legacy DynamoDB database. The `@discriminatorName` encodings does not introduce another map for the purposes of identification but rather adds another discriminator field to the attribute Map. -Concrete examples of using the `@discriminatorName`, `@simpleEnum` and `@field` annotations can be seen below. +Concrete examples of using the `@discriminatorName` and `@field` annotations can be seen below. ## Sealed trait members that are case classes @@ -77,7 +77,6 @@ The encoding for case class field names can also be customised via `@fieldName` ## Sealed trait members that are all case objects ```scala -@simpleEnum sealed trait TrafficLight case object GREEN extends TrafficLight @caseName("red_traffic_light") @@ -86,7 +85,9 @@ final case class Box(trafficLightColour: TrafficLight) ``` We can get a more compact and intuitive encoding of trait members that are case objects by using the `@simpleEnum` -annotation which encodes to just a value that is the member name. Encoding for an instance of `Box(GREEN)` would be: +annotation which encodes to just a value that is the member name. This annotation doesn't need to be added explicitly +since this is done by the ZIO Schema macro automatically. +Encoding for an instance of `Box(GREEN)` would be: `Map(trafficLightColour -> String(GREEN))`