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

Cleanup simple enum #338

Merged
merged 2 commits into from
Nov 28, 2023
Merged
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
9 changes: 5 additions & 4 deletions docs/codec-customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand All @@ -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))`

Expand Down
Original file line number Diff line number Diff line change
@@ -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 }
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions dynamodb/src/test/scala/zio/dynamodb/codec/models.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,7 +26,6 @@ object TypeSafeRoundTripSerialisationExample extends ZIOAppDefault {
def id: String
}
object Invoice {
@simpleEnum
sealed trait PaymentType
object PaymentType {
@caseName("debit")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading