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

upgrade zio-schema to 1.2.1 #437

Merged
merged 5 commits into from
Jun 16, 2024
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
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ addCommandAlias("check", "all scalafmtSbtCheck scalafmtCheck test:scalafmtCheck"

val zioVersion = "2.1.1"
val zioAwsVersion = "7.21.15.12"
val zioSchemaVersion = "0.4.17"
val zioSchemaVersion = "1.2.1"
val zioPreludeVersion = "1.0.0-RC25"
val zioInteropCats3Version = "23.1.0.2"
val catsEffect3Version = "3.5.4"
Expand Down
10 changes: 9 additions & 1 deletion dynamodb/src/main/scala/zio/dynamodb/Codec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private[dynamodb] object Codec {
case StandardType.DoubleType => (a: A) => AttributeValue.Number(BigDecimal(a.toString))
case StandardType.BigDecimalType => (a: A) => AttributeValue.Number(BigDecimal(a.toString))
case StandardType.BigIntegerType => (a: A) => AttributeValue.Number(BigDecimal(a.toString))
case StandardType.UUIDType => (a: A) => AttributeValue.String(a.toString)
case StandardType.CurrencyType => (a: A) => AttributeValue.String(a.toString)
case StandardType.DayOfWeekType => (a: A) => AttributeValue.String(a.toString)
case StandardType.DurationType => (a: A) => AttributeValue.String(a.toString)
case StandardType.InstantType => (a: A) => AttributeValue.String(a.toString)
Expand All @@ -226,6 +226,7 @@ private[dynamodb] object Codec {
case StandardType.OffsetDateTimeType => (a: A) => AttributeValue.String(a.toString)
case StandardType.OffsetTimeType => (a: A) => AttributeValue.String(a.toString)
case StandardType.PeriodType => (a: A) => AttributeValue.String(a.toString)
case StandardType.UUIDType => (a: A) => AttributeValue.String(a.toString)
case StandardType.YearType => yearEncoder
case StandardType.YearMonthType => (a: A) => AttributeValue.String(a.toString)
case StandardType.ZonedDateTimeType => (a: A) => AttributeValue.String(a.toString)
Expand Down Expand Up @@ -611,6 +612,13 @@ private[dynamodb] object Codec {
FromAttributeValue.stringFromAttributeValue.fromAttributeValue(av).flatMap { s =>
Try(UUID.fromString(s)).toEither.left.map(iae => DecodingError(s"Invalid UUID: ${iae.getMessage}"))
}
case StandardType.CurrencyType =>
(av: AttributeValue) =>
FromAttributeValue.stringFromAttributeValue.fromAttributeValue(av).flatMap { s =>
Try(java.util.Currency.getInstance(s)).toEither.left.map(e =>
DecodingError(s"Invalid Currency: ${e.getMessage}")
)
}
case StandardType.DayOfWeekType =>
(av: AttributeValue) => javaTimeStringParser(av)(DayOfWeek.valueOf(_))
case StandardType.DurationType =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ trait CodecTestFixtures {
]("boolean")(_.asInstanceOf[Boolean])(_.asInstanceOf[Any])(_.isInstanceOf[Boolean])
)

lazy implicit val caseClassOfCurrencySchema: Schema[CaseClassOfCurrency] = DeriveSchema.gen[CaseClassOfCurrency]
lazy implicit val nestedCaseClass2Schema: Schema[NestedCaseClass2] = DeriveSchema.gen[NestedCaseClass2]
lazy implicit val simpleCaseClass3Schema: Schema[SimpleCaseClass3] = DeriveSchema.gen[SimpleCaseClass3]
lazy implicit val simpleCaseClass3SchemaOptional: Schema[SimpleCaseClass3Option] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import zio.test.{ Gen, Sized }
import zio.Chunk

import scala.collection.immutable.ListMap
import scala.jdk.CollectionConverters._

object DynamicValueGen {

Expand All @@ -24,6 +25,8 @@ object DynamicValueGen {
case typ: StandardType.FloatType.type => gen(typ, Gen.float)
case typ: StandardType.BigDecimalType.type => gen(typ, Gen.double.map(d => java.math.BigDecimal.valueOf(d)))
case typ: StandardType.BigIntegerType.type => gen(typ, Gen.long.map(n => java.math.BigInteger.valueOf(n)))
case typ: StandardType.CurrencyType.type =>
gen(typ, Gen.fromIterable(java.util.Currency.getAvailableCurrencies.asScala))
case typ: StandardType.DayOfWeekType.type => gen(typ, JavaTimeGen.anyDayOfWeek)
case typ: StandardType.DurationType.type => gen(typ, JavaTimeGen.anyDuration)
case typ: StandardType.InstantType.type => gen(typ, JavaTimeGen.anyInstant)
Expand Down Expand Up @@ -82,6 +85,7 @@ object DynamicValueGen {
case Schema.Either(left, right, _) => Gen.oneOf(anyDynamicLeftValueOfSchema(left), anyDynamicRightValueOfSchema(right))
case Schema.Transform(schema, _, _, _, _) => anyDynamicValueOfSchema(schema)
case Schema.Fail(message, _) => Gen.const(DynamicValue.Error(message))
case Schema.Fallback(left, right, _, _) => Gen.oneOf(anyDynamicLeftValueOfSchema(left), anyDynamicRightValueOfSchema(right)) // TODO: Avi - check
case l @ Schema.Lazy(_) => anyDynamicValueOfSchema(l.schema)
}
//scalafmt: { maxColumn = 120 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ object ItemDecoderSpec extends ZIOSpecDefault with CodecTestFixtures {

assertTrue(actual == Right("FOO"))
},
test("decoded currency") {
val currency: java.util.Currency = java.util.Currency.getInstance("GBP")
val expected = CaseClassOfCurrency(currency)

val actual = DynamoDBQuery.fromItem[CaseClassOfCurrency](Item("c" -> "GBP"))

assert(actual)(isRight(equalTo(expected)))
},
test("decoded list") {
val expected = CaseClassOfList(List(1, 2))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ object ItemEncoderSpec extends ZIOSpecDefault with CodecTestFixtures {
equalTo(AttributeValue.Map(Map(toAvString("string") -> toAvString("FOO"))))
)
},
test("encodes a Currency") {
val expectedItem: Item = Item("c" -> "GBP")
val currency: java.util.Currency = java.util.Currency.getInstance("GBP")
println(s"currency: $currency")

val item = DynamoDBQuery.toItem(CaseClassOfCurrency(currency))

assert(item)(equalTo(expectedItem))
},
test("encodes List of Int") {
val expectedItem: Item = Item("nums" -> List(1, 2, 3))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import java.math.{ BigDecimal => JBigDecimal, BigInteger => JBigInt }
import zio.test.{ Gen, Sized }
import zio.schema.StandardType

import scala.jdk.CollectionConverters._

object StandardTypeGen {

def anyStandardType[A]: Gen[Any, StandardType[A]] =
Expand All @@ -22,6 +24,7 @@ object StandardTypeGen {
(StandardType.BigDecimalType),
(StandardType.BigIntegerType),
(StandardType.CharType),
(StandardType.CurrencyType),
(StandardType.UUIDType),
(StandardType.DayOfWeekType),
(StandardType.DurationType),
Expand Down Expand Up @@ -56,6 +59,9 @@ object StandardTypeGen {

def anyStandardTypeAndGen[A]: Gen[Any, StandardTypeAndGen[A]] =
anyStandardType[A].map {
case typ: StandardType.CurrencyType.type =>
val allCurrencies: List[java.util.Currency] = java.util.Currency.getAvailableCurrencies.asScala.toList
(typ -> Gen.fromIterable(allCurrencies)).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.StringType.type => (typ -> Gen.string).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.BoolType.type => (typ -> Gen.boolean).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.ShortType.type => (typ -> Gen.short).asInstanceOf[StandardTypeAndGen[A]]
Expand All @@ -64,6 +70,7 @@ object StandardTypeGen {
case typ: StandardType.FloatType.type => (typ -> Gen.float).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.DoubleType.type => (typ -> Gen.double).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.BinaryType.type => (typ -> Gen.chunkOf(Gen.byte)).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.ByteType.type => (typ -> Gen.byte).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.CharType.type => (typ -> Gen.asciiChar).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.UUIDType.type => (typ -> Gen.uuid).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.BigDecimalType.type => (typ -> javaBigDecimal).asInstanceOf[StandardTypeAndGen[A]]
Expand All @@ -82,13 +89,14 @@ object StandardTypeGen {
case typ: StandardType.OffsetTimeType.type =>
(typ -> JavaTimeGen.anyOffsetTime).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.PeriodType.type => (typ -> JavaTimeGen.anyPeriod).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.UnitType.type =>
(typ -> Gen.unit).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.YearType.type => (typ -> JavaTimeGen.anyYear).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.YearMonthType.type => (typ -> JavaTimeGen.anyYearMonth).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.ZonedDateTimeType.type =>
(typ -> JavaTimeGen.anyZonedDateTime).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.ZoneIdType.type => (typ -> JavaTimeGen.anyZoneId).asInstanceOf[StandardTypeAndGen[A]]
case typ: StandardType.ZoneOffsetType.type =>
(typ -> JavaTimeGen.anyZoneOffset).asInstanceOf[StandardTypeAndGen[A]]
case _ => (StandardType.UnitType -> Gen.unit).asInstanceOf[StandardTypeAndGen[A]]
}
}
3 changes: 3 additions & 0 deletions dynamodb/src/test/scala/zio/dynamodb/codec/models.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ final case class Ok(response: List[String]) extends Status
final case class Failed(code: Int, reason: String, additionalExplanation: Option[String], remark: String = "oops")
extends Status
case object Pending extends Status

final case class CaseClassOfCurrency(c: java.util.Currency)

final case class NestedCaseClass2(id: Int, nested: SimpleCaseClass3)

final case class SimpleCaseClass3(id: Int, name: String, flag: Boolean)
Expand Down
Loading