From a594be63b09b901596cf1b6ff66c429a114860dc Mon Sep 17 00:00:00 2001 From: Ivan Grebenik <1790371+igrebenik@users.noreply.github.com> Date: Thu, 6 Jan 2022 12:42:44 +0000 Subject: [PATCH] Add schema for DynamicValue (#170) * Add Schema for DynamicValue * Rename some of StandardType values * Fix type casting * Fix linting issues Run `prepare` on the whole project --- .../scala/zio/schema/DefaultValueSpec.scala | 28 +- .../scala/zio/schema/DynamicValueGen.scala | 28 +- .../src/test/scala/zio/schema/SchemaGen.scala | 28 +- .../scala/zio/schema/StandardTypeGen.scala | 54 +- .../scala/zio/schema/codec/JsonCodec.scala | 58 +-- .../zio/schema/codec/JsonCodecSpec.scala | 8 +- .../zio/schema/codec/ProtobufCodec.scala | 114 ++--- .../zio/schema/codec/ProtobufCodecSpec.scala | 27 +- .../src/main/scala/zio/schema/DeriveGen.scala | 58 +-- .../src/test/scala/zio/schema/TestData.scala | 30 +- .../src/main/scala/zio/schema/Diff.scala | 68 +-- .../main/scala/zio/schema/DynamicValue.scala | 479 +++++++++++++++++- .../src/main/scala/zio/schema/Schema.scala | 2 + .../main/scala/zio/schema/StandardType.scala | 72 +-- .../scala/zio/schema/SchemaAssertions.scala | 28 +- 15 files changed, 790 insertions(+), 292 deletions(-) diff --git a/tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala b/tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala index 92d68dec8..454267f9d 100644 --- a/tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala +++ b/tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala @@ -77,29 +77,29 @@ object DefaultValueSpec extends DefaultRunnableSpec { ) }, test("Month default value") { - assert(Primitive(StandardType.Month).defaultValue)(isRight(equalTo(java.time.Month.JANUARY))) + assert(Primitive(StandardType.MonthType).defaultValue)(isRight(equalTo(java.time.Month.JANUARY))) }, test("MonthDay default value") { - assert(Primitive(StandardType.MonthDay).defaultValue)( + assert(Primitive(StandardType.MonthDayType).defaultValue)( isRight(equalTo(java.time.MonthDay.of(java.time.Month.JANUARY, 1))) ) }, test("Period default value") { - assert(Primitive(StandardType.Period).defaultValue)(isRight(equalTo(java.time.Period.ZERO))) + assert(Primitive(StandardType.PeriodType).defaultValue)(isRight(equalTo(java.time.Period.ZERO))) }, test("Year default value") { - assert(Primitive(StandardType.Year).defaultValue)( + assert(Primitive(StandardType.YearType).defaultValue)( isRight(equalTo(java.time.Year.now)) ) }, test("YearMonth default value") { - assert(Primitive(StandardType.YearMonth).defaultValue)(isRight(equalTo(java.time.YearMonth.now))) + assert(Primitive(StandardType.YearMonthType).defaultValue)(isRight(equalTo(java.time.YearMonth.now))) }, test("ZoneId default value") { - assert(Primitive(StandardType.ZoneId).defaultValue)(isRight(equalTo(java.time.ZoneId.systemDefault))) + assert(Primitive(StandardType.ZoneIdType).defaultValue)(isRight(equalTo(java.time.ZoneId.systemDefault))) }, test("ZoneOffset default value") { - assert(Primitive(StandardType.ZoneOffset).defaultValue)(isRight(equalTo(java.time.ZoneOffset.UTC))) + assert(Primitive(StandardType.ZoneOffsetType).defaultValue)(isRight(equalTo(java.time.ZoneOffset.UTC))) }, test("Duration default value") { assert(Primitive(StandardType.Duration(ChronoUnit.SECONDS)).defaultValue)( @@ -107,37 +107,37 @@ object DefaultValueSpec extends DefaultRunnableSpec { ) }, test("Instant default value") { - assert(Primitive(StandardType.Instant(DateTimeFormatter.ISO_INSTANT)).defaultValue)( + assert(Primitive(StandardType.InstantType(DateTimeFormatter.ISO_INSTANT)).defaultValue)( isRight(equalTo(java.time.Instant.EPOCH)) ) }, test("LocalDate default value") { - assert(Primitive(StandardType.LocalDate(DateTimeFormatter.ISO_LOCAL_DATE)).defaultValue)( + assert(Primitive(StandardType.LocalDateType(DateTimeFormatter.ISO_LOCAL_DATE)).defaultValue)( isRight(isSubtype[java.time.LocalDate](anything)) ) }, test("LocalTime default value") { - assert(Primitive(StandardType.LocalTime(DateTimeFormatter.ISO_LOCAL_TIME)).defaultValue)( + assert(Primitive(StandardType.LocalTimeType(DateTimeFormatter.ISO_LOCAL_TIME)).defaultValue)( isRight(equalTo(java.time.LocalTime.MIDNIGHT)) ) }, test("LocalDateTime default value") { - assert(Primitive(StandardType.LocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME)).defaultValue)( + assert(Primitive(StandardType.LocalDateTimeType(DateTimeFormatter.ISO_LOCAL_DATE_TIME)).defaultValue)( isRight(isSubtype[java.time.LocalDateTime](anything)) ) }, test("OffsetTime default value") { - assert(Primitive(StandardType.OffsetTime(DateTimeFormatter.ISO_OFFSET_TIME)).defaultValue)( + assert(Primitive(StandardType.OffsetTimeType(DateTimeFormatter.ISO_OFFSET_TIME)).defaultValue)( isRight(isSubtype[java.time.OffsetTime](anything)) ) }, test("OffsetDateTime default value") { - assert(Primitive(StandardType.OffsetDateTime(DateTimeFormatter.ISO_OFFSET_DATE_TIME)).defaultValue)( + assert(Primitive(StandardType.OffsetDateTimeType(DateTimeFormatter.ISO_OFFSET_DATE_TIME)).defaultValue)( isRight(isSubtype[java.time.OffsetDateTime](anything)) ) }, test("ZonedDateTime default value") { - assert(Primitive(StandardType.ZonedDateTime(DateTimeFormatter.ISO_ZONED_DATE_TIME)).defaultValue)( + assert(Primitive(StandardType.ZonedDateTimeType(DateTimeFormatter.ISO_ZONED_DATE_TIME)).defaultValue)( isRight(isSubtype[java.time.ZonedDateTime](anything)) ) } diff --git a/tests/shared/src/test/scala/zio/schema/DynamicValueGen.scala b/tests/shared/src/test/scala/zio/schema/DynamicValueGen.scala index 6086e4f1b..32036f7aa 100644 --- a/tests/shared/src/test/scala/zio/schema/DynamicValueGen.scala +++ b/tests/shared/src/test/scala/zio/schema/DynamicValueGen.scala @@ -25,20 +25,20 @@ object DynamicValueGen { case typ: StandardType.BigIntegerType.type => gen(typ, Gen.anyLong.map(n => java.math.BigInteger.valueOf(n))) case typ: StandardType.DayOfWeekType.type => gen(typ, JavaTimeGen.anyDayOfWeek) case typ: StandardType.Duration => gen(typ, JavaTimeGen.anyDuration) - case typ: StandardType.Instant => gen(typ, JavaTimeGen.anyInstant) - case typ: StandardType.LocalDate => gen(typ, JavaTimeGen.anyLocalDate) - case typ: StandardType.LocalDateTime => gen(typ, JavaTimeGen.anyLocalDateTime) - case typ: StandardType.LocalTime => gen(typ, JavaTimeGen.anyLocalTime) - case typ: StandardType.Month.type => gen(typ, JavaTimeGen.anyMonth) - case typ: StandardType.MonthDay.type => gen(typ, JavaTimeGen.anyMonthDay) - case typ: StandardType.OffsetDateTime => gen(typ, JavaTimeGen.anyOffsetDateTime) - case typ: StandardType.OffsetTime => gen(typ, JavaTimeGen.anyOffsetTime) - case typ: StandardType.Period.type => gen(typ, JavaTimeGen.anyPeriod) - case typ: StandardType.Year.type => gen(typ, JavaTimeGen.anyYear) - case typ: StandardType.YearMonth.type => gen(typ, JavaTimeGen.anyYearMonth) - case typ: StandardType.ZonedDateTime => gen(typ, JavaTimeGen.anyZonedDateTime) - case typ: StandardType.ZoneId.type => gen(typ, JavaTimeGen.anyZoneId) - case typ: StandardType.ZoneOffset.type => gen(typ, JavaTimeGen.anyZoneOffset) + case typ: StandardType.InstantType => gen(typ, JavaTimeGen.anyInstant) + case typ: StandardType.LocalDateType => gen(typ, JavaTimeGen.anyLocalDate) + case typ: StandardType.LocalDateTimeType => gen(typ, JavaTimeGen.anyLocalDateTime) + case typ: StandardType.LocalTimeType => gen(typ, JavaTimeGen.anyLocalTime) + case typ: StandardType.MonthType.type => gen(typ, JavaTimeGen.anyMonth) + case typ: StandardType.MonthDayType.type => gen(typ, JavaTimeGen.anyMonthDay) + case typ: StandardType.OffsetDateTimeType => gen(typ, JavaTimeGen.anyOffsetDateTime) + case typ: StandardType.OffsetTimeType => gen(typ, JavaTimeGen.anyOffsetTime) + case typ: StandardType.PeriodType.type => gen(typ, JavaTimeGen.anyPeriod) + case typ: StandardType.YearType.type => gen(typ, JavaTimeGen.anyYear) + case typ: StandardType.YearMonthType.type => gen(typ, JavaTimeGen.anyYearMonth) + case typ: StandardType.ZonedDateTimeType => gen(typ, JavaTimeGen.anyZonedDateTime) + case typ: StandardType.ZoneIdType.type => gen(typ, JavaTimeGen.anyZoneId) + case typ: StandardType.ZoneOffsetType.type => gen(typ, JavaTimeGen.anyZoneOffset) case typ: StandardType.UnitType.type => Gen.const(DynamicValue.Primitive((), typ)) case typ: StandardType.UUIDType.type => gen(typ, Gen.anyUUID) } diff --git a/tests/shared/src/test/scala/zio/schema/SchemaGen.scala b/tests/shared/src/test/scala/zio/schema/SchemaGen.scala index dfd2afbe8..79370adeb 100644 --- a/tests/shared/src/test/scala/zio/schema/SchemaGen.scala +++ b/tests/shared/src/test/scala/zio/schema/SchemaGen.scala @@ -573,40 +573,40 @@ object SchemaGen { ), SchemaTest("DayOfWeek", StandardType.DayOfWeekType, JavaTimeGen.anyDayOfWeek), SchemaTest("Duration", StandardType.Duration(ChronoUnit.SECONDS), JavaTimeGen.anyDuration), - SchemaTest("Instant", StandardType.Instant(DateTimeFormatter.ISO_DATE_TIME), JavaTimeGen.anyInstant), - SchemaTest("LocalDate", StandardType.LocalDate(DateTimeFormatter.ISO_DATE), JavaTimeGen.anyLocalDate), + SchemaTest("Instant", StandardType.InstantType(DateTimeFormatter.ISO_DATE_TIME), JavaTimeGen.anyInstant), + SchemaTest("LocalDate", StandardType.LocalDateType(DateTimeFormatter.ISO_DATE), JavaTimeGen.anyLocalDate), SchemaTest( "LocalDateTime", - StandardType.LocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME), + StandardType.LocalDateTimeType(DateTimeFormatter.ISO_LOCAL_DATE_TIME), JavaTimeGen.anyLocalDateTime ), SchemaTest( "LocalTime", - StandardType.LocalTime(DateTimeFormatter.ISO_LOCAL_TIME), + StandardType.LocalTimeType(DateTimeFormatter.ISO_LOCAL_TIME), JavaTimeGen.anyLocalTime ), - SchemaTest("Month", StandardType.Month, JavaTimeGen.anyMonth), - SchemaTest("MonthDay", StandardType.MonthDay, JavaTimeGen.anyMonthDay), + SchemaTest("Month", StandardType.MonthType, JavaTimeGen.anyMonth), + SchemaTest("MonthDay", StandardType.MonthDayType, JavaTimeGen.anyMonthDay), SchemaTest( "OffsetDateTime", - StandardType.OffsetDateTime(DateTimeFormatter.ISO_OFFSET_DATE_TIME), + StandardType.OffsetDateTimeType(DateTimeFormatter.ISO_OFFSET_DATE_TIME), JavaTimeGen.anyOffsetDateTime ), SchemaTest( "OffsetTime", - StandardType.OffsetTime(DateTimeFormatter.ISO_OFFSET_TIME), + StandardType.OffsetTimeType(DateTimeFormatter.ISO_OFFSET_TIME), JavaTimeGen.anyOffsetTime ), - SchemaTest("Period", StandardType.Period, JavaTimeGen.anyPeriod), - SchemaTest("Year", StandardType.Year, JavaTimeGen.anyYear), - SchemaTest("YearMonth", StandardType.YearMonth, JavaTimeGen.anyYearMonth), + SchemaTest("Period", StandardType.PeriodType, JavaTimeGen.anyPeriod), + SchemaTest("Year", StandardType.YearType, JavaTimeGen.anyYear), + SchemaTest("YearMonth", StandardType.YearMonthType, JavaTimeGen.anyYearMonth), SchemaTest( "ZonedDateTime", - StandardType.ZonedDateTime(DateTimeFormatter.ISO_ZONED_DATE_TIME), + StandardType.ZonedDateTimeType(DateTimeFormatter.ISO_ZONED_DATE_TIME), JavaTimeGen.anyZonedDateTime ), - SchemaTest("ZoneId", StandardType.ZoneId, JavaTimeGen.anyZoneId), - SchemaTest("ZoneOffset", StandardType.ZoneOffset, JavaTimeGen.anyZoneOffset), + SchemaTest("ZoneId", StandardType.ZoneIdType, JavaTimeGen.anyZoneId), + SchemaTest("ZoneOffset", StandardType.ZoneOffsetType, JavaTimeGen.anyZoneOffset), SchemaTest("UnitType", StandardType.UnitType, Gen.unit) ) diff --git a/tests/shared/src/test/scala/zio/schema/StandardTypeGen.scala b/tests/shared/src/test/scala/zio/schema/StandardTypeGen.scala index 736cfe845..cff74e433 100644 --- a/tests/shared/src/test/scala/zio/schema/StandardTypeGen.scala +++ b/tests/shared/src/test/scala/zio/schema/StandardTypeGen.scala @@ -25,19 +25,19 @@ object StandardTypeGen { (StandardType.UUIDType), (StandardType.DayOfWeekType), (StandardType.Duration(ChronoUnit.SECONDS)), - (StandardType.Instant(DateTimeFormatter.ISO_DATE_TIME)), - (StandardType.LocalDate(DateTimeFormatter.ISO_DATE)), - (StandardType.LocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME)), - (StandardType.LocalTime(DateTimeFormatter.ISO_LOCAL_TIME)), - (StandardType.Month), - (StandardType.MonthDay), - (StandardType.OffsetDateTime(DateTimeFormatter.ISO_OFFSET_DATE_TIME)), - (StandardType.OffsetTime(DateTimeFormatter.ISO_OFFSET_TIME)), - (StandardType.Period), - (StandardType.Year), - (StandardType.YearMonth), - (StandardType.ZonedDateTime(DateTimeFormatter.ISO_ZONED_DATE_TIME)), - (StandardType.ZoneId) + (StandardType.InstantType(DateTimeFormatter.ISO_DATE_TIME)), + (StandardType.LocalDateType(DateTimeFormatter.ISO_DATE)), + (StandardType.LocalDateTimeType(DateTimeFormatter.ISO_LOCAL_DATE_TIME)), + (StandardType.LocalTimeType(DateTimeFormatter.ISO_LOCAL_TIME)), + (StandardType.MonthType), + (StandardType.MonthDayType), + (StandardType.OffsetDateTimeType(DateTimeFormatter.ISO_OFFSET_DATE_TIME)), + (StandardType.OffsetTimeType(DateTimeFormatter.ISO_OFFSET_TIME)), + (StandardType.PeriodType), + (StandardType.YearType), + (StandardType.YearMonthType), + (StandardType.ZonedDateTimeType(DateTimeFormatter.ISO_ZONED_DATE_TIME)), + (StandardType.ZoneIdType) ) //FIXME For some reason adding this causes other unrelated tests to break. // Gen.const(StandardType.ZoneOffset) @@ -69,20 +69,20 @@ object StandardTypeGen { case typ: StandardType.BigIntegerType.type => typ -> javaBigInt case typ: StandardType.DayOfWeekType.type => typ -> JavaTimeGen.anyDayOfWeek case typ: StandardType.Duration => typ -> JavaTimeGen.anyDuration - case typ: StandardType.Instant => typ -> JavaTimeGen.anyInstant - case typ: StandardType.LocalDate => typ -> JavaTimeGen.anyLocalDate - case typ: StandardType.LocalDateTime => typ -> JavaTimeGen.anyLocalDateTime - case typ: StandardType.LocalTime => typ -> JavaTimeGen.anyLocalTime - case typ: StandardType.Month.type => typ -> JavaTimeGen.anyMonth - case typ: StandardType.MonthDay.type => typ -> JavaTimeGen.anyMonthDay - case typ: StandardType.OffsetDateTime => typ -> JavaTimeGen.anyOffsetDateTime - case typ: StandardType.OffsetTime => typ -> JavaTimeGen.anyOffsetTime - case typ: StandardType.Period.type => typ -> JavaTimeGen.anyPeriod - case typ: StandardType.Year.type => typ -> JavaTimeGen.anyYear - case typ: StandardType.YearMonth.type => typ -> JavaTimeGen.anyYearMonth - case typ: StandardType.ZonedDateTime => typ -> JavaTimeGen.anyZonedDateTime - case typ: StandardType.ZoneId.type => typ -> JavaTimeGen.anyZoneId - case typ: StandardType.ZoneOffset.type => typ -> JavaTimeGen.anyZoneOffset + case typ: StandardType.InstantType => typ -> JavaTimeGen.anyInstant + case typ: StandardType.LocalDateType => typ -> JavaTimeGen.anyLocalDate + case typ: StandardType.LocalDateTimeType => typ -> JavaTimeGen.anyLocalDateTime + case typ: StandardType.LocalTimeType => typ -> JavaTimeGen.anyLocalTime + case typ: StandardType.MonthType.type => typ -> JavaTimeGen.anyMonth + case typ: StandardType.MonthDayType.type => typ -> JavaTimeGen.anyMonthDay + case typ: StandardType.OffsetDateTimeType => typ -> JavaTimeGen.anyOffsetDateTime + case typ: StandardType.OffsetTimeType => typ -> JavaTimeGen.anyOffsetTime + case typ: StandardType.PeriodType.type => typ -> JavaTimeGen.anyPeriod + case typ: StandardType.YearType.type => typ -> JavaTimeGen.anyYear + case typ: StandardType.YearMonthType.type => typ -> JavaTimeGen.anyYearMonth + case typ: StandardType.ZonedDateTimeType => typ -> JavaTimeGen.anyZonedDateTime + case typ: StandardType.ZoneIdType.type => typ -> JavaTimeGen.anyZoneId + case typ: StandardType.ZoneOffsetType.type => typ -> JavaTimeGen.anyZoneOffset case _ => StandardType.UnitType -> Gen.unit: StandardTypeAndGen[_] } } diff --git a/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala b/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala index 7c9b17d99..75093fc58 100644 --- a/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala +++ b/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala @@ -53,35 +53,35 @@ object JsonCodec extends Codec { private[codec] def primitiveCodec[A](standardType: StandardType[A]): ZJsonCodec[A] = standardType match { - case StandardType.UnitType => unitCodec - case StandardType.StringType => ZJsonCodec.string - case StandardType.BoolType => ZJsonCodec.boolean - case StandardType.ShortType => ZJsonCodec.short - case StandardType.IntType => ZJsonCodec.int - case StandardType.LongType => ZJsonCodec.long - case StandardType.FloatType => ZJsonCodec.float - case StandardType.DoubleType => ZJsonCodec.double - case StandardType.BinaryType => ZJsonCodec.chunk(ZJsonCodec.byte) - case StandardType.CharType => ZJsonCodec.char - case StandardType.BigIntegerType => ZJsonCodec.bigInteger - case StandardType.BigDecimalType => ZJsonCodec.bigDecimal - case StandardType.UUIDType => ZJsonCodec.uuid - case StandardType.DayOfWeekType => ZJsonCodec.dayOfWeek // ZJsonCodec[java.time.DayOfWeek] - case StandardType.Duration(_) => ZJsonCodec.duration //ZJsonCodec[java.time.Duration] - case StandardType.Instant(_) => ZJsonCodec.instant //ZJsonCodec[java.time.Instant] - case StandardType.LocalDate(_) => ZJsonCodec.localDate //ZJsonCodec[java.time.LocalDate] - case StandardType.LocalDateTime(_) => ZJsonCodec.localDateTime //ZJsonCodec[java.time.LocalDateTime] - case StandardType.LocalTime(_) => ZJsonCodec.localTime //ZJsonCodec[java.time.LocalTime] - case StandardType.Month => ZJsonCodec.month //ZJsonCodec[java.time.Month] - case StandardType.MonthDay => ZJsonCodec.monthDay //ZJsonCodec[java.time.MonthDay] - case StandardType.OffsetDateTime(_) => ZJsonCodec.offsetDateTime //ZJsonCodec[java.time.OffsetDateTime] - case StandardType.OffsetTime(_) => ZJsonCodec.offsetTime //ZJsonCodec[java.time.OffsetTime] - case StandardType.Period => ZJsonCodec.period //ZJsonCodec[java.time.Period] - case StandardType.Year => ZJsonCodec.year //ZJsonCodec[java.time.Year] - case StandardType.YearMonth => ZJsonCodec.yearMonth //ZJsonCodec[java.time.YearMonth] - case StandardType.ZonedDateTime(_) => ZJsonCodec.zonedDateTime //ZJsonCodec[java.time.ZonedDateTime] - case StandardType.ZoneId => ZJsonCodec.zoneId //ZJsonCodec[java.time.ZoneId] - case StandardType.ZoneOffset => ZJsonCodec.zoneOffset //ZJsonCodec[java.time.ZoneOffset] + case StandardType.UnitType => unitCodec + case StandardType.StringType => ZJsonCodec.string + case StandardType.BoolType => ZJsonCodec.boolean + case StandardType.ShortType => ZJsonCodec.short + case StandardType.IntType => ZJsonCodec.int + case StandardType.LongType => ZJsonCodec.long + case StandardType.FloatType => ZJsonCodec.float + case StandardType.DoubleType => ZJsonCodec.double + case StandardType.BinaryType => ZJsonCodec.chunk(ZJsonCodec.byte) + case StandardType.CharType => ZJsonCodec.char + case StandardType.BigIntegerType => ZJsonCodec.bigInteger + case StandardType.BigDecimalType => ZJsonCodec.bigDecimal + case StandardType.UUIDType => ZJsonCodec.uuid + case StandardType.DayOfWeekType => ZJsonCodec.dayOfWeek // ZJsonCodec[java.time.DayOfWeek] + case StandardType.Duration(_) => ZJsonCodec.duration //ZJsonCodec[java.time.Duration] + case StandardType.InstantType(_) => ZJsonCodec.instant //ZJsonCodec[java.time.Instant] + case StandardType.LocalDateType(_) => ZJsonCodec.localDate //ZJsonCodec[java.time.LocalDate] + case StandardType.LocalDateTimeType(_) => ZJsonCodec.localDateTime //ZJsonCodec[java.time.LocalDateTime] + case StandardType.LocalTimeType(_) => ZJsonCodec.localTime //ZJsonCodec[java.time.LocalTime] + case StandardType.MonthType => ZJsonCodec.month //ZJsonCodec[java.time.Month] + case StandardType.MonthDayType => ZJsonCodec.monthDay //ZJsonCodec[java.time.MonthDay] + case StandardType.OffsetDateTimeType(_) => ZJsonCodec.offsetDateTime //ZJsonCodec[java.time.OffsetDateTime] + case StandardType.OffsetTimeType(_) => ZJsonCodec.offsetTime //ZJsonCodec[java.time.OffsetTime] + case StandardType.PeriodType => ZJsonCodec.period //ZJsonCodec[java.time.Period] + case StandardType.YearType => ZJsonCodec.year //ZJsonCodec[java.time.Year] + case StandardType.YearMonthType => ZJsonCodec.yearMonth //ZJsonCodec[java.time.YearMonth] + case StandardType.ZonedDateTimeType(_) => ZJsonCodec.zonedDateTime //ZJsonCodec[java.time.ZonedDateTime] + case StandardType.ZoneIdType => ZJsonCodec.zoneId //ZJsonCodec[java.time.ZoneId] + case StandardType.ZoneOffsetType => ZJsonCodec.zoneOffset //ZJsonCodec[java.time.ZoneOffset] } } diff --git a/zio-schema-json/shared/src/test/scala/zio/schema/codec/JsonCodecSpec.scala b/zio-schema-json/shared/src/test/scala/zio/schema/codec/JsonCodecSpec.scala index f54e52b5f..cf685b2e5 100644 --- a/zio-schema-json/shared/src/test/scala/zio/schema/codec/JsonCodecSpec.scala +++ b/zio-schema-json/shared/src/test/scala/zio/schema/codec/JsonCodecSpec.scala @@ -39,10 +39,10 @@ object JsonCodecSpec extends DefaultRunnableSpec { checkM(Gen.anyString)(s => assertEncodes(Schema[String], s, stringify(s))) ), testM("ZoneOffset") { - assertEncodesJson(Schema.Primitive(StandardType.ZoneOffset), ZoneOffset.UTC) + assertEncodesJson(Schema.Primitive(StandardType.ZoneOffsetType), ZoneOffset.UTC) }, testM("ZoneId") { - assertEncodesJson(Schema.Primitive(StandardType.ZoneId), ZoneId.systemDefault()) + assertEncodesJson(Schema.Primitive(StandardType.ZoneIdType), ZoneId.systemDefault()) } ), suite("optional")( @@ -314,7 +314,7 @@ object JsonCodecSpec extends DefaultRunnableSpec { //FIXME test independently because including ZoneOffset in StandardTypeGen.anyStandardType wreaks havoc. checkM(Gen.chunkOf(JavaTimeGen.anyZoneOffset)) { chunk => assertEncodesThenDecodes( - Schema.chunk(Schema.Primitive(StandardType.ZoneOffset)), + Schema.chunk(Schema.Primitive(StandardType.ZoneOffsetType)), chunk ) } @@ -359,7 +359,7 @@ object JsonCodecSpec extends DefaultRunnableSpec { testM("of ZoneOffsets") { checkM(JavaTimeGen.anyZoneOffset) { zoneOffset => assertEncodesThenDecodes( - Schema.record(Schema.Field("zoneOffset", Schema.Primitive(StandardType.ZoneOffset))), + Schema.record(Schema.Field("zoneOffset", Schema.Primitive(StandardType.ZoneOffsetType))), ListMap[String, Any]("zoneOffset" -> zoneOffset) ) } diff --git a/zio-schema-protobuf/shared/src/main/scala/zio/schema/codec/ProtobufCodec.scala b/zio-schema-protobuf/shared/src/main/scala/zio/schema/codec/ProtobufCodec.scala index 7fccf54dc..e3c45d38d 100644 --- a/zio-schema-protobuf/shared/src/main/scala/zio/schema/codec/ProtobufCodec.scala +++ b/zio-schema-protobuf/shared/src/main/scala/zio/schema/codec/ProtobufCodec.scala @@ -97,35 +97,35 @@ object ProtobufCodec extends Codec { } private def canBePacked(standardType: StandardType[_]): Boolean = standardType match { - case StandardType.UnitType => false - case StandardType.StringType => false - case StandardType.BoolType => true - case StandardType.ShortType => true - case StandardType.IntType => true - case StandardType.LongType => true - case StandardType.FloatType => true - case StandardType.DoubleType => true - case StandardType.BinaryType => false - case StandardType.CharType => true - case StandardType.BigIntegerType => false - case StandardType.BigDecimalType => false - case StandardType.UUIDType => false - case StandardType.DayOfWeekType => true - case StandardType.Month => true - case StandardType.MonthDay => false - case StandardType.Period => false - case StandardType.Year => true - case StandardType.YearMonth => false - case StandardType.ZoneId => false - case StandardType.ZoneOffset => true - case StandardType.Duration(_) => true - case StandardType.Instant(_) => false - case StandardType.LocalDate(_) => false - case StandardType.LocalTime(_) => false - case StandardType.LocalDateTime(_) => false - case StandardType.OffsetTime(_) => false - case StandardType.OffsetDateTime(_) => false - case StandardType.ZonedDateTime(_) => false + case StandardType.UnitType => false + case StandardType.StringType => false + case StandardType.BoolType => true + case StandardType.ShortType => true + case StandardType.IntType => true + case StandardType.LongType => true + case StandardType.FloatType => true + case StandardType.DoubleType => true + case StandardType.BinaryType => false + case StandardType.CharType => true + case StandardType.BigIntegerType => false + case StandardType.BigDecimalType => false + case StandardType.UUIDType => false + case StandardType.DayOfWeekType => true + case StandardType.MonthType => true + case StandardType.MonthDayType => false + case StandardType.PeriodType => false + case StandardType.YearType => true + case StandardType.YearMonthType => false + case StandardType.ZoneIdType => false + case StandardType.ZoneOffsetType => true + case StandardType.Duration(_) => true + case StandardType.InstantType(_) => false + case StandardType.LocalDateType(_) => false + case StandardType.LocalTimeType(_) => false + case StandardType.LocalDateTimeType(_) => false + case StandardType.OffsetTimeType(_) => false + case StandardType.OffsetDateTimeType(_) => false + case StandardType.ZonedDateTimeType(_) => false } } @@ -320,39 +320,39 @@ object ProtobufCodec extends Codec { encodePrimitive(fieldNumber, StandardType.StringType, u.toString) case (StandardType.DayOfWeekType, v: DayOfWeek) => encodePrimitive(fieldNumber, StandardType.IntType, v.getValue) - case (StandardType.Month, v: Month) => + case (StandardType.MonthType, v: Month) => encodePrimitive(fieldNumber, StandardType.IntType, v.getValue) - case (StandardType.MonthDay, v: MonthDay) => + case (StandardType.MonthDayType, v: MonthDay) => encodeRecord(fieldNumber, monthDayStructure, ListMap("month" -> v.getMonthValue, "day" -> v.getDayOfMonth)) - case (StandardType.Period, v: Period) => + case (StandardType.PeriodType, v: Period) => encodeRecord( fieldNumber, periodStructure, ListMap("years" -> v.getYears, "months" -> v.getMonths, "days" -> v.getDays) ) - case (StandardType.Year, v: Year) => + case (StandardType.YearType, v: Year) => encodePrimitive(fieldNumber, StandardType.IntType, v.getValue) - case (StandardType.YearMonth, v: YearMonth) => + case (StandardType.YearMonthType, v: YearMonth) => encodeRecord(fieldNumber, yearMonthStructure, ListMap("year" -> v.getYear, "month" -> v.getMonthValue)) - case (StandardType.ZoneId, v: ZoneId) => + case (StandardType.ZoneIdType, v: ZoneId) => encodePrimitive(fieldNumber, StandardType.StringType, v.getId) - case (StandardType.ZoneOffset, v: ZoneOffset) => + case (StandardType.ZoneOffsetType, v: ZoneOffset) => encodePrimitive(fieldNumber, StandardType.IntType, v.getTotalSeconds) case (StandardType.Duration(_), v: Duration) => encodeRecord(fieldNumber, durationStructure, ListMap("seconds" -> v.getSeconds, "nanos" -> v.getNano)) - case (StandardType.Instant(formatter), v: Instant) => + case (StandardType.InstantType(formatter), v: Instant) => encodePrimitive(fieldNumber, StandardType.StringType, formatter.format(v)) - case (StandardType.LocalDate(formatter), v: LocalDate) => + case (StandardType.LocalDateType(formatter), v: LocalDate) => encodePrimitive(fieldNumber, StandardType.StringType, v.format(formatter)) - case (StandardType.LocalTime(formatter), v: LocalTime) => + case (StandardType.LocalTimeType(formatter), v: LocalTime) => encodePrimitive(fieldNumber, StandardType.StringType, v.format(formatter)) - case (StandardType.LocalDateTime(formatter), v: LocalDateTime) => + case (StandardType.LocalDateTimeType(formatter), v: LocalDateTime) => encodePrimitive(fieldNumber, StandardType.StringType, v.format(formatter)) - case (StandardType.OffsetTime(formatter), v: OffsetTime) => + case (StandardType.OffsetTimeType(formatter), v: OffsetTime) => encodePrimitive(fieldNumber, StandardType.StringType, v.format(formatter)) - case (StandardType.OffsetDateTime(formatter), v: OffsetDateTime) => + case (StandardType.OffsetDateTimeType(formatter), v: OffsetDateTime) => encodePrimitive(fieldNumber, StandardType.StringType, v.format(formatter)) - case (StandardType.ZonedDateTime(formatter), v: ZonedDateTime) => + case (StandardType.ZonedDateTimeType(formatter), v: ZonedDateTime) => encodePrimitive(fieldNumber, StandardType.StringType, v.format(formatter)) case (_, _) => Chunk.empty @@ -734,15 +734,15 @@ object ProtobufCodec extends Codec { } case StandardType.DayOfWeekType => varIntDecoder.map(_.intValue).map(DayOfWeek.of) - case StandardType.Month => + case StandardType.MonthType => varIntDecoder.map(_.intValue).map(Month.of) - case StandardType.MonthDay => + case StandardType.MonthDayType => recordDecoder(monthDayStructure) .map( data => MonthDay.of(data.getOrElse("month", 0).asInstanceOf[Int], data.getOrElse("day", 0).asInstanceOf[Int]) ) - case StandardType.Period => + case StandardType.PeriodType => recordDecoder(periodStructure) .map( data => @@ -752,16 +752,16 @@ object ProtobufCodec extends Codec { data.getOrElse("days", 0).asInstanceOf[Int] ) ) - case StandardType.Year => + case StandardType.YearType => varIntDecoder.map(_.intValue).map(Year.of) - case StandardType.YearMonth => + case StandardType.YearMonthType => recordDecoder(yearMonthStructure) .map( data => YearMonth.of(data.getOrElse("year", 0).asInstanceOf[Int], data.getOrElse("month", 0).asInstanceOf[Int]) ) - case StandardType.ZoneId => stringDecoder.map(ZoneId.of) - case StandardType.ZoneOffset => + case StandardType.ZoneIdType => stringDecoder.map(ZoneId.of) + case StandardType.ZoneOffsetType => varIntDecoder .map(_.intValue) .map(ZoneOffset.ofTotalSeconds) @@ -774,19 +774,19 @@ object ProtobufCodec extends Codec { data.getOrElse("nanos", 0).asInstanceOf[Int].toLong ) ) - case StandardType.Instant(formatter) => + case StandardType.InstantType(formatter) => stringDecoder.map(v => Instant.from(formatter.parse(v))) - case StandardType.LocalDate(formatter) => + case StandardType.LocalDateType(formatter) => stringDecoder.map(LocalDate.parse(_, formatter)) - case StandardType.LocalTime(formatter) => + case StandardType.LocalTimeType(formatter) => stringDecoder.map(LocalTime.parse(_, formatter)) - case StandardType.LocalDateTime(formatter) => + case StandardType.LocalDateTimeType(formatter) => stringDecoder.map(LocalDateTime.parse(_, formatter)) - case StandardType.OffsetTime(formatter) => + case StandardType.OffsetTimeType(formatter) => stringDecoder.map(OffsetTime.parse(_, formatter)) - case StandardType.OffsetDateTime(formatter) => + case StandardType.OffsetDateTimeType(formatter) => stringDecoder.map(OffsetDateTime.parse(_, formatter)) - case StandardType.ZonedDateTime(formatter) => + case StandardType.ZonedDateTimeType(formatter) => stringDecoder.map(ZonedDateTime.parse(_, formatter)) case st => fail(s"Unsupported primitive type $st") } diff --git a/zio-schema-protobuf/shared/src/test/scala/zio/schema/codec/ProtobufCodecSpec.scala b/zio-schema-protobuf/shared/src/test/scala/zio/schema/codec/ProtobufCodecSpec.scala index 6aa6d516f..4542c3663 100644 --- a/zio-schema-protobuf/shared/src/test/scala/zio/schema/codec/ProtobufCodecSpec.scala +++ b/zio-schema-protobuf/shared/src/test/scala/zio/schema/codec/ProtobufCodecSpec.scala @@ -280,48 +280,51 @@ object ProtobufCodecSpec extends DefaultRunnableSpec { testM("instants") { val value = Instant.now() for { - ed <- encodeAndDecode(Primitive(StandardType.Instant(DateTimeFormatter.ISO_INSTANT)), value) - ed2 <- encodeAndDecodeNS(Primitive(StandardType.Instant(DateTimeFormatter.ISO_INSTANT)), value) + ed <- encodeAndDecode(Primitive(StandardType.InstantType(DateTimeFormatter.ISO_INSTANT)), value) + ed2 <- encodeAndDecodeNS(Primitive(StandardType.InstantType(DateTimeFormatter.ISO_INSTANT)), value) } yield assert(ed)(equalTo(Chunk(value))) && assert(ed2)(equalTo(value)) }, testM("local dates") { val value = LocalDate.now() for { - ed <- encodeAndDecode(Primitive(StandardType.LocalDate(DateTimeFormatter.ISO_LOCAL_DATE)), value) - ed2 <- encodeAndDecodeNS(Primitive(StandardType.LocalDate(DateTimeFormatter.ISO_LOCAL_DATE)), value) + ed <- encodeAndDecode(Primitive(StandardType.LocalDateType(DateTimeFormatter.ISO_LOCAL_DATE)), value) + ed2 <- encodeAndDecodeNS(Primitive(StandardType.LocalDateType(DateTimeFormatter.ISO_LOCAL_DATE)), value) } yield assert(ed)(equalTo(Chunk(value))) && assert(ed2)(equalTo(value)) }, testM("local times") { val value = LocalTime.now() for { - ed <- encodeAndDecode(Primitive(StandardType.LocalTime(DateTimeFormatter.ISO_LOCAL_TIME)), value) - ed2 <- encodeAndDecodeNS(Primitive(StandardType.LocalTime(DateTimeFormatter.ISO_LOCAL_TIME)), value) + ed <- encodeAndDecode(Primitive(StandardType.LocalTimeType(DateTimeFormatter.ISO_LOCAL_TIME)), value) + ed2 <- encodeAndDecodeNS(Primitive(StandardType.LocalTimeType(DateTimeFormatter.ISO_LOCAL_TIME)), value) } yield assert(ed)(equalTo(Chunk(value))) && assert(ed2)(equalTo(value)) }, testM("local date times") { val value = LocalDateTime.now() for { - ed <- encodeAndDecode(Primitive(StandardType.LocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME)), value) - ed2 <- encodeAndDecodeNS(Primitive(StandardType.LocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME)), value) + ed <- encodeAndDecode(Primitive(StandardType.LocalDateTimeType(DateTimeFormatter.ISO_LOCAL_DATE_TIME)), value) + ed2 <- encodeAndDecodeNS( + Primitive(StandardType.LocalDateTimeType(DateTimeFormatter.ISO_LOCAL_DATE_TIME)), + value + ) } yield assert(ed)(equalTo(Chunk(value))) && assert(ed2)(equalTo(value)) }, testM("offset times") { val value = OffsetTime.now() for { - ed <- encodeAndDecode(Primitive(StandardType.OffsetTime(DateTimeFormatter.ISO_OFFSET_TIME)), value) - ed2 <- encodeAndDecodeNS(Primitive(StandardType.OffsetTime(DateTimeFormatter.ISO_OFFSET_TIME)), value) + ed <- encodeAndDecode(Primitive(StandardType.OffsetTimeType(DateTimeFormatter.ISO_OFFSET_TIME)), value) + ed2 <- encodeAndDecodeNS(Primitive(StandardType.OffsetTimeType(DateTimeFormatter.ISO_OFFSET_TIME)), value) } yield assert(ed)(equalTo(Chunk(value))) && assert(ed2)(equalTo(value)) }, testM("offset date times") { val value = OffsetDateTime.now() - val offsetDateSchema = Primitive(StandardType.OffsetDateTime(DateTimeFormatter.ISO_OFFSET_DATE_TIME)) + val offsetDateSchema = Primitive(StandardType.OffsetDateTimeType(DateTimeFormatter.ISO_OFFSET_DATE_TIME)) for { ed <- encodeAndDecode(offsetDateSchema, value) ed2 <- encodeAndDecodeNS(offsetDateSchema, value) } yield assert(ed)(equalTo(Chunk(value))) && assert(ed2)(equalTo(value)) }, testM("zoned date times") { - val zoneSchema = Primitive(StandardType.ZonedDateTime(DateTimeFormatter.ISO_ZONED_DATE_TIME)) + val zoneSchema = Primitive(StandardType.ZonedDateTimeType(DateTimeFormatter.ISO_ZONED_DATE_TIME)) val now = ZonedDateTime.now() for { ed <- encodeAndDecode(zoneSchema, now) diff --git a/zio-schema-zio-test/shared/src/main/scala/zio/schema/DeriveGen.scala b/zio-schema-zio-test/shared/src/main/scala/zio/schema/DeriveGen.scala index 0acc9d5fc..5fe826112 100644 --- a/zio-schema-zio-test/shared/src/main/scala/zio/schema/DeriveGen.scala +++ b/zio-schema-zio-test/shared/src/main/scala/zio/schema/DeriveGen.scala @@ -473,35 +473,35 @@ object DeriveGen { def genPrimitive[A](standardType: StandardType[A]): Gen[Random with Sized, A] = { val gen = standardType match { - case StandardType.UnitType => Gen.unit - case StandardType.StringType => Gen.anyString - case StandardType.BoolType => Gen.boolean - case StandardType.ShortType => Gen.anyShort - case StandardType.IntType => Gen.anyInt - case StandardType.LongType => Gen.anyLong - case StandardType.FloatType => Gen.anyFloat - case StandardType.DoubleType => Gen.anyDouble - case StandardType.BinaryType => Gen.chunkOf1(Gen.anyByte).map(_.toChunk) - case StandardType.CharType => Gen.anyChar - case StandardType.UUIDType => Gen.anyUUID - case StandardType.BigDecimalType => Gen.anyDouble.map(new java.math.BigDecimal(_)) - case StandardType.BigIntegerType => Gen.anyLong.map(java.math.BigInteger.valueOf(_)) - case StandardType.DayOfWeekType => Gen.anyDayOfWeek - case StandardType.Month => Gen.anyMonth - case StandardType.MonthDay => Gen.anyMonthDay - case StandardType.Period => Gen.anyPeriod - case StandardType.Year => Gen.anyYear - case StandardType.YearMonth => Gen.anyYearMonth - case StandardType.ZoneId => Gen.anyZoneId - case StandardType.ZoneOffset => Gen.anyZoneOffset - case StandardType.Duration(_) => Gen.anyFiniteDuration - case StandardType.Instant(_) => Gen.anyInstant - case StandardType.LocalDate(_) => Gen.anyLocalDate - case StandardType.LocalTime(_) => Gen.anyLocalTime - case StandardType.LocalDateTime(_) => Gen.anyLocalDateTime - case StandardType.OffsetTime(_) => Gen.anyOffsetTime - case StandardType.OffsetDateTime(_) => Gen.anyOffsetDateTime - case StandardType.ZonedDateTime(_) => Gen.anyZonedDateTime + case StandardType.UnitType => Gen.unit + case StandardType.StringType => Gen.anyString + case StandardType.BoolType => Gen.boolean + case StandardType.ShortType => Gen.anyShort + case StandardType.IntType => Gen.anyInt + case StandardType.LongType => Gen.anyLong + case StandardType.FloatType => Gen.anyFloat + case StandardType.DoubleType => Gen.anyDouble + case StandardType.BinaryType => Gen.chunkOf1(Gen.anyByte).map(_.toChunk) + case StandardType.CharType => Gen.anyChar + case StandardType.UUIDType => Gen.anyUUID + case StandardType.BigDecimalType => Gen.anyDouble.map(new java.math.BigDecimal(_)) + case StandardType.BigIntegerType => Gen.anyLong.map(java.math.BigInteger.valueOf(_)) + case StandardType.DayOfWeekType => Gen.anyDayOfWeek + case StandardType.MonthType => Gen.anyMonth + case StandardType.MonthDayType => Gen.anyMonthDay + case StandardType.PeriodType => Gen.anyPeriod + case StandardType.YearType => Gen.anyYear + case StandardType.YearMonthType => Gen.anyYearMonth + case StandardType.ZoneIdType => Gen.anyZoneId + case StandardType.ZoneOffsetType => Gen.anyZoneOffset + case StandardType.Duration(_) => Gen.anyFiniteDuration + case StandardType.InstantType(_) => Gen.anyInstant + case StandardType.LocalDateType(_) => Gen.anyLocalDate + case StandardType.LocalTimeType(_) => Gen.anyLocalTime + case StandardType.LocalDateTimeType(_) => Gen.anyLocalDateTime + case StandardType.OffsetTimeType(_) => Gen.anyOffsetTime + case StandardType.OffsetDateTimeType(_) => Gen.anyOffsetDateTime + case StandardType.ZonedDateTimeType(_) => Gen.anyZonedDateTime } gen.map(_.asInstanceOf[A]) diff --git a/zio-schema-zio-test/shared/src/test/scala/zio/schema/TestData.scala b/zio-schema-zio-test/shared/src/test/scala/zio/schema/TestData.scala index b478c0838..2d5ea8f5f 100644 --- a/zio-schema-zio-test/shared/src/test/scala/zio/schema/TestData.scala +++ b/zio-schema-zio-test/shared/src/test/scala/zio/schema/TestData.scala @@ -18,33 +18,35 @@ object TestData { val charSchema: Schema[Char] = Schema.Primitive(StandardType.CharType) val bigDecimalSchema: Schema[java.math.BigDecimal] = Schema.Primitive(StandardType.BigDecimalType) val bigIntegerSchema: Schema[java.math.BigInteger] = Schema.Primitive(StandardType.BigIntegerType) - val monthSchema: Schema[java.time.Month] = Schema.Primitive(StandardType.Month) - val monthDaySchema: Schema[java.time.MonthDay] = Schema.Primitive(StandardType.MonthDay) - val periodSchema: Schema[java.time.Period] = Schema.Primitive(StandardType.Period) + val monthSchema: Schema[java.time.Month] = Schema.Primitive(StandardType.MonthType) + val monthDaySchema: Schema[java.time.MonthDay] = Schema.Primitive(StandardType.MonthDayType) + val periodSchema: Schema[java.time.Period] = Schema.Primitive(StandardType.PeriodType) val dayOfWeekSchema: Schema[java.time.DayOfWeek] = Schema.Primitive(StandardType.DayOfWeekType) - val yearSchema: Schema[java.time.Year] = Schema.Primitive(StandardType.Year) - val yearMonthSchema: Schema[java.time.YearMonth] = Schema.Primitive(StandardType.YearMonth) - val zoneIdSchema: Schema[java.time.ZoneId] = Schema.Primitive(StandardType.ZoneId) - val zoneOffsetSchema: Schema[java.time.ZoneOffset] = Schema.Primitive(StandardType.ZoneOffset) - val instantSchema: Schema[java.time.Instant] = Schema.Primitive(StandardType.Instant(DateTimeFormatter.ISO_INSTANT)) + val yearSchema: Schema[java.time.Year] = Schema.Primitive(StandardType.YearType) + val yearMonthSchema: Schema[java.time.YearMonth] = Schema.Primitive(StandardType.YearMonthType) + val zoneIdSchema: Schema[java.time.ZoneId] = Schema.Primitive(StandardType.ZoneIdType) + val zoneOffsetSchema: Schema[java.time.ZoneOffset] = Schema.Primitive(StandardType.ZoneOffsetType) + + val instantSchema: Schema[java.time.Instant] = + Schema.Primitive(StandardType.InstantType(DateTimeFormatter.ISO_INSTANT)) val localDateSchema: Schema[java.time.LocalDate] = - Schema.Primitive(StandardType.LocalDate(DateTimeFormatter.ISO_LOCAL_DATE)) + Schema.Primitive(StandardType.LocalDateType(DateTimeFormatter.ISO_LOCAL_DATE)) val localTimeSchema: Schema[java.time.LocalTime] = - Schema.Primitive(StandardType.LocalTime(DateTimeFormatter.ISO_LOCAL_TIME)) + Schema.Primitive(StandardType.LocalTimeType(DateTimeFormatter.ISO_LOCAL_TIME)) val localDateTimeSchema: Schema[java.time.LocalDateTime] = - Schema.Primitive(StandardType.LocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME)) + Schema.Primitive(StandardType.LocalDateTimeType(DateTimeFormatter.ISO_LOCAL_DATE_TIME)) val offsetTimeSchema: Schema[java.time.OffsetTime] = - Schema.Primitive(StandardType.OffsetTime(DateTimeFormatter.ISO_OFFSET_TIME)) + Schema.Primitive(StandardType.OffsetTimeType(DateTimeFormatter.ISO_OFFSET_TIME)) val offsetDateTimeSchema: Schema[java.time.OffsetDateTime] = - Schema.Primitive(StandardType.OffsetDateTime(DateTimeFormatter.ISO_OFFSET_DATE_TIME)) + Schema.Primitive(StandardType.OffsetDateTimeType(DateTimeFormatter.ISO_OFFSET_DATE_TIME)) val zonedDateTimeSchema: Schema[java.time.ZonedDateTime] = - Schema.Primitive(StandardType.ZonedDateTime(DateTimeFormatter.ISO_ZONED_DATE_TIME)) + Schema.Primitive(StandardType.ZonedDateTimeType(DateTimeFormatter.ISO_ZONED_DATE_TIME)) val uuidSchema: Schema[java.util.UUID] = Schema.Primitive(StandardType.UUIDType) diff --git a/zio-schema/shared/src/main/scala/zio/schema/Diff.scala b/zio-schema/shared/src/main/scala/zio/schema/Diff.scala index 21a1567b1..93fbabbda 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/Diff.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/Diff.scala @@ -89,22 +89,22 @@ object Differ { case Schema.Primitive(StandardType.BigIntegerType, _) => bigInt case Schema.Primitive(StandardType.StringType, _) => string case Schema.Primitive(StandardType.UUIDType, _) => string.transform(_.toString) - case Schema.Primitive(StandardType.ZoneId, _) => string.transform[ZoneId](_.getId) + case Schema.Primitive(StandardType.ZoneIdType, _) => string.transform[ZoneId](_.getId) case Schema.Primitive(StandardType.DayOfWeekType, _) => dayOfWeek - case Schema.Primitive(StandardType.Period, _) => period - case Schema.Primitive(StandardType.Month, _) => month - case Schema.Primitive(StandardType.MonthDay, _) => monthDay - case Schema.Primitive(StandardType.Year, _) => year - case Schema.Primitive(StandardType.YearMonth, _) => yearMonth - case Schema.Primitive(StandardType.LocalDate(_), _) => localDate - case Schema.Primitive(StandardType.Instant(_), _) => instant + case Schema.Primitive(StandardType.PeriodType, _) => period + case Schema.Primitive(StandardType.MonthType, _) => month + case Schema.Primitive(StandardType.MonthDayType, _) => monthDay + case Schema.Primitive(StandardType.YearType, _) => year + case Schema.Primitive(StandardType.YearMonthType, _) => yearMonth + case Schema.Primitive(StandardType.LocalDateType(_), _) => localDate + case Schema.Primitive(StandardType.InstantType(_), _) => instant case Schema.Primitive(StandardType.Duration(_), _) => duration - case Schema.Primitive(StandardType.LocalTime(_), _) => localTime - case Schema.Primitive(StandardType.LocalDateTime(_), _) => localDateTime - case Schema.Primitive(StandardType.OffsetTime(_), _) => offsetTime - case Schema.Primitive(StandardType.OffsetDateTime(_), _) => offsetDateTime - case Schema.Primitive(StandardType.ZonedDateTime(_), _) => zonedDateTime - case Schema.Primitive(StandardType.ZoneOffset, _) => zoneOffset + case Schema.Primitive(StandardType.LocalTimeType(_), _) => localTime + case Schema.Primitive(StandardType.LocalDateTimeType(_), _) => localDateTime + case Schema.Primitive(StandardType.OffsetTimeType(_), _) => offsetTime + case Schema.Primitive(StandardType.OffsetDateTimeType(_), _) => offsetDateTime + case Schema.Primitive(StandardType.ZonedDateTimeType(_), _) => zonedDateTime + case Schema.Primitive(StandardType.ZoneOffsetType, _) => zoneOffset case Schema.Tuple(leftSchema, rightSchema, _) => fromSchema(leftSchema) <*> fromSchema(rightSchema) case Schema.Optional(schema, _) => fromSchema(schema).optional case Schema.Sequence(schema, _, f, _) => fromSchema(schema).foreach(f) @@ -461,7 +461,7 @@ sealed trait Diff { self => case (_, Identical) => Right(a) case (Schema.Primitive(StandardType.StringType, _), Myers(edits)) => patchStringFromMyersEdits(a, edits) case (Schema.Primitive(StandardType.UUIDType, _), Myers(edits)) => patchStringFromMyersEdits(a.toString, edits).map(UUID.fromString) - case (Schema.Primitive(StandardType.ZoneId, _), Myers(edits)) => patchStringFromMyersEdits(a.getId(), edits).map(ZoneId.of) + case (Schema.Primitive(StandardType.ZoneIdType, _), Myers(edits)) => patchStringFromMyersEdits(a.getId(), edits).map(ZoneId.of) case (Primitive(StandardType.IntType, _), Number(distance: Int)) => patchNumeric[Int](a, distance) case (Primitive(StandardType.ShortType, _), Number(distance: Short)) => patchNumeric[Short](a, distance) case (Primitive(StandardType.DoubleType, _), Number(distance: Double)) => patchNumeric[Double](a, distance) @@ -489,26 +489,26 @@ sealed trait Diff { self => case (Chunk.empty, values) => Right(values) case (errors, _) => Left(s"Running patch produced the following error(s): ${errors.toList}.") } - case (Primitive(StandardType.Year, _), Temporal(distance :: Nil)) => Right(Year.of(a.getValue - distance.toInt)) - case (Primitive(StandardType.YearMonth, _), Temporal(distance :: Nil)) => Right(YearMonth.now().`with`(ChronoField.PROLEPTIC_MONTH, a.getLong(ChronoField.PROLEPTIC_MONTH) - distance)) - case (Primitive(StandardType.LocalDate(_), _), Temporal(distance :: Nil)) => Right(LocalDate.ofEpochDay(a.toEpochDay - distance)) - case (Primitive(StandardType.Instant(_), _), Temporal(dist1 :: dist2 :: Nil)) => Right(Instant.ofEpochSecond(a.getEpochSecond - dist1, a.getNano() - dist2)) - case (Primitive(StandardType.LocalTime(_), _), Temporal(distance :: Nil)) => Right(LocalTime.ofNanoOfDay(a.toNanoOfDay - distance)) - case (Primitive(StandardType.LocalDateTime(_), _), Temporal(dist1 :: dist2 :: Nil)) => + case (Primitive(StandardType.YearType, _), Temporal(distance :: Nil)) => Right(Year.of(a.getValue - distance.toInt)) + case (Primitive(StandardType.YearMonthType, _), Temporal(distance :: Nil)) => Right(YearMonth.now().`with`(ChronoField.PROLEPTIC_MONTH, a.getLong(ChronoField.PROLEPTIC_MONTH) - distance)) + case (Primitive(StandardType.LocalDateType(_), _), Temporal(distance :: Nil)) => Right(LocalDate.ofEpochDay(a.toEpochDay - distance)) + case (Primitive(StandardType.InstantType(_), _), Temporal(dist1 :: dist2 :: Nil)) => Right(Instant.ofEpochSecond(a.getEpochSecond - dist1, a.getNano() - dist2)) + case (Primitive(StandardType.LocalTimeType(_), _), Temporal(distance :: Nil)) => Right(LocalTime.ofNanoOfDay(a.toNanoOfDay - distance)) + case (Primitive(StandardType.LocalDateTimeType(_), _), Temporal(dist1 :: dist2 :: Nil)) => Right { LocalDateTime.of( LocalDate.ofEpochDay(a.toLocalDate.toEpochDay - dist1), LocalTime.ofNanoOfDay(a.toLocalTime.toNanoOfDay - dist2) ) } - case (Primitive(StandardType.OffsetTime(_), _), Temporal(dist1 :: dist2 :: Nil)) => + case (Primitive(StandardType.OffsetTimeType(_), _), Temporal(dist1 :: dist2 :: Nil)) => Right { OffsetTime.of( LocalTime.ofNanoOfDay(a.toLocalTime.toNanoOfDay - dist1), ZoneOffset.ofTotalSeconds(a.getOffset.getTotalSeconds - dist2.toInt) ) } - case (Primitive(StandardType.OffsetDateTime(_), _), Temporal(dist1 :: dist2 :: dist3 :: Nil)) => + case (Primitive(StandardType.OffsetDateTimeType(_), _), Temporal(dist1 :: dist2 :: dist3 :: Nil)) => Right { OffsetDateTime.of( LocalDate.ofEpochDay(a.toLocalDate.toEpochDay - dist1), @@ -516,7 +516,7 @@ sealed trait Diff { self => ZoneOffset.ofTotalSeconds(a.getOffset.getTotalSeconds - dist3.toInt) ) } - case (Primitive(StandardType.Period, _), d @ Temporal(dayAdjustment :: monthAdjustment :: yearAdjustment :: Nil)) => + case (Primitive(StandardType.PeriodType, _), d @ Temporal(dayAdjustment :: monthAdjustment :: yearAdjustment :: Nil)) => try { Right( Period.of( @@ -526,33 +526,33 @@ sealed trait Diff { self => ) ) } catch { case _: Throwable => Left(s"Invalid java.time.Period diff $d") } - case (Primitive(StandardType.ZonedDateTime(_), _), ZonedDateTime(Identical, Identical)) => Right(a) - case (Primitive(StandardType.ZonedDateTime(_), _), ZonedDateTime(Identical, Myers(edits))) => + case (Primitive(StandardType.ZonedDateTimeType(_), _), ZonedDateTime(Identical, Identical)) => Right(a) + case (Primitive(StandardType.ZonedDateTimeType(_), _), ZonedDateTime(Identical, Myers(edits))) => patchStringFromMyersEdits(a.getZone.getId, edits).map { zoneIdString => JZonedDateTime.of(a.toLocalDateTime, ZoneId.of(zoneIdString)) } - case (Primitive(StandardType.ZonedDateTime(_), _), ZonedDateTime(Temporal(dist1 :: dist2 :: Nil), Identical)) => { + case (Primitive(StandardType.ZonedDateTimeType(_), _), ZonedDateTime(Temporal(dist1 :: dist2 :: Nil), Identical)) => { val localDateTime = LocalDateTime.of(LocalDate.ofEpochDay(a.toLocalDate.toEpochDay - dist1), LocalTime.ofNanoOfDay(a.toLocalTime.toNanoOfDay - dist2)) Right(JZonedDateTime.of(localDateTime, a.getZone)) } - case (Primitive(StandardType.ZonedDateTime(_), _), ZonedDateTime(Temporal(dist1 :: dist2 :: Nil), Myers(edits))) => { + case (Primitive(StandardType.ZonedDateTimeType(_), _), ZonedDateTime(Temporal(dist1 :: dist2 :: Nil), Myers(edits))) => { val localDateTime = LocalDateTime.of(LocalDate.ofEpochDay(a.toLocalDate.toEpochDay - dist1), LocalTime.ofNanoOfDay(a.toLocalTime.toNanoOfDay - dist2)) patchStringFromMyersEdits(a.getZone.getId, edits).map { zoneIdString => JZonedDateTime.of(localDateTime, ZoneId.of(zoneIdString)) } } - case (Primitive(StandardType.ZoneOffset, _), Temporal(distance :: Nil)) => + case (Primitive(StandardType.ZoneOffsetType, _), Temporal(distance :: Nil)) => try { Right(ZoneOffset.ofTotalSeconds(a.getTotalSeconds + distance.toInt)) } catch { case t: Throwable => Left(s"Patched offset is invalid: ${t.getMessage}") } case (Primitive(StandardType.DayOfWeekType, _), Temporal(distance :: Nil)) => Right(a.plus(distance)) - case (Primitive(StandardType.Month, _), Temporal(distance :: Nil)) => Right(a.plus(distance)) + case (Primitive(StandardType.MonthType, _), Temporal(distance :: Nil)) => Right(a.plus(distance)) case (Primitive(StandardType.Duration(_), _), Temporal(dist1 :: dist2 :: Nil)) => Right(JDuration.ofSeconds(a.getSeconds - dist1, a.getNano() - dist2)) // TODO need to deal with leap year differences - case (Primitive(StandardType.MonthDay, _), Temporal(regDiff :: _ :: Nil)) => Right(MonthDay.from(ChronoUnit.DAYS.addTo(a.atYear(2001), regDiff.toLong))) - case (s @ Schema.Lazy(_), diff) => diff.patch(a)(s.schema) - case (_: Optional[t], Total(_, Diff.Tag.Left)) => Right(None) - case (_: Optional[t], Total(right, Diff.Tag.Right)) => Right(Some(right.asInstanceOf[t])) + case (Primitive(StandardType.MonthDayType, _), Temporal(regDiff :: _ :: Nil)) => Right(MonthDay.from(ChronoUnit.DAYS.addTo(a.atYear(2001), regDiff.toLong))) + case (s @ Schema.Lazy(_), diff) => diff.patch(a)(s.schema) + case (_: Optional[t], Total(_, Diff.Tag.Left)) => Right(None) + case (_: Optional[t], Total(right, Diff.Tag.Right)) => Right(Some(right.asInstanceOf[t])) case (schema: Optional[t], diff) => a.asInstanceOf[Option[t]] match { case None => Right(None) diff --git a/zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala b/zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala index 8cfa64bfc..d7c02dcfc 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala @@ -1,5 +1,10 @@ package zio.schema +import java.math.{ BigDecimal, BigInteger } +import java.time._ +import java.time.format.DateTimeFormatter +import java.util.UUID + import scala.collection.immutable.ListMap import zio.Chunk @@ -1845,7 +1850,7 @@ object DynamicValue { final case class Sequence(values: Chunk[DynamicValue]) extends DynamicValue - final case class Dictionary[K, V](entries: Chunk[(DynamicValue, DynamicValue)]) extends DynamicValue + final case class Dictionary(entries: Chunk[(DynamicValue, DynamicValue)]) extends DynamicValue sealed case class Primitive[A](value: A, standardType: StandardType[A]) extends DynamicValue @@ -1866,4 +1871,476 @@ object DynamicValue { final case class DynamicAst(ast: SchemaAst) extends DynamicValue final case class Error(message: String) extends DynamicValue + +} + +private[schema] object DynamicValueSchema { self => + + def apply(): Schema[DynamicValue] = + Schema.EnumN( + CaseSet + .Cons(errorCase, CaseSet.Empty[DynamicValue]()) + .:+:(noneValueCase) + .:+:(rightValueCase) + .:+:(leftValueCase) + .:+:(tupleCase) + .:+:(transformCase) + .:+:(someValueCase) + .:+:(dictionaryCase) + .:+:(sequenceCase) + .:+:(enumerationCase) + .:+:(recordCase) + .:+:(dynamicAstCase) + .:+:(primitiveUnitCase) + .:+:(primitiveStringCase) + .:+:(primitiveBooleanCase) + .:+:(primitiveShortCase) + .:+:(primitiveIntCase) + .:+:(primitiveLongCase) + .:+:(primitiveFloatCase) + .:+:(primitiveDoubleCase) + .:+:(primitiveBinaryCase) + .:+:(primitiveCharCase) + .:+:(primitiveBigDecimalCase) + .:+:(primitiveBigIntegerCase) + .:+:(primitiveDayOfWeekCase) + .:+:(primitiveMonthCase) + .:+:(primitiveMonthDayCase) + .:+:(primitivePeriodCase) + .:+:(primitiveYearCase) + .:+:(primitiveYearMonthCase) + .:+:(primitiveZoneIdCase) + .:+:(primitiveZoneOffsetCase) + .:+:(primitiveInstantCase) + .:+:(primitiveLocalDateCase) + .:+:(primitiveLocalTimeCase) + .:+:(primitiveLocalDateTimeCase) + .:+:(primitiveOffsetTimeCase) + .:+:(primitiveOffsetDateTimeCase) + .:+:(primitiveZonedDateTimeCase) + .:+:(primitiveUUIDCase) + .:+:(singletonCase) + ) + + implicit val instantStandardType: StandardType[Instant] = + StandardType.InstantType(DateTimeFormatter.ISO_INSTANT) + + implicit val localDateStandardType: StandardType[LocalDate] = + StandardType.LocalDateType(DateTimeFormatter.ISO_LOCAL_DATE) + + implicit val localTimeStandardType: StandardType[LocalTime] = + StandardType.LocalTimeType(DateTimeFormatter.ISO_LOCAL_TIME) + + implicit val localDateTimeStandardType: StandardType[LocalDateTime] = + StandardType.LocalDateTimeType(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + + implicit val offsetTimeStandardType: StandardType[OffsetTime] = + StandardType.OffsetTimeType(DateTimeFormatter.ISO_OFFSET_TIME) + + implicit val offsetDateTimeStandardType: StandardType[OffsetDateTime] = + StandardType.OffsetDateTimeType(DateTimeFormatter.ISO_OFFSET_DATE_TIME) + + implicit val zonedDateTimeStandardType: StandardType[ZonedDateTime] = + StandardType.ZonedDateTimeType(DateTimeFormatter.ISO_ZONED_DATE_TIME) + + private val errorCase: Schema.Case[DynamicValue.Error, DynamicValue] = + Schema.Case( + "Error", + Schema.CaseClass1[String, DynamicValue.Error]( + Schema.Field("message", Schema.primitive[String]), + message => DynamicValue.Error(message), + error => error.message + ), + _.asInstanceOf[DynamicValue.Error] + ) + + private val noneValueCase: Schema.Case[DynamicValue.NoneValue.type, DynamicValue] = + Schema.Case( + "NoneValue", + Schema.none.transform(_ => DynamicValue.NoneValue, _ => None), + _.asInstanceOf[DynamicValue.NoneValue.type], + Chunk("case") + ) + + private val rightValueCase: Schema.Case[DynamicValue.RightValue, DynamicValue] = + Schema.Case( + "RightValue", + Schema.CaseClass1[DynamicValue, DynamicValue.RightValue]( + Schema.Field("value", Schema.defer(DynamicValueSchema())), + dynamicValue => DynamicValue.RightValue(dynamicValue), + rightValue => rightValue.value + ), + _.asInstanceOf[DynamicValue.RightValue] + ) + + private val leftValueCase: Schema.Case[DynamicValue.LeftValue, DynamicValue] = + Schema.Case( + "LeftValue", + Schema.CaseClass1[DynamicValue, DynamicValue.LeftValue]( + Schema.Field("value", Schema.defer(DynamicValueSchema())), + dynamicValue => DynamicValue.LeftValue(dynamicValue), + leftValue => leftValue.value + ), + _.asInstanceOf[DynamicValue.LeftValue] + ) + + private val tupleCase: Schema.Case[DynamicValue.Tuple, DynamicValue] = + Schema.Case( + "Tuple", + Schema.CaseClass2[DynamicValue, DynamicValue, DynamicValue.Tuple]( + Schema.Field("left", Schema.defer(DynamicValueSchema())), + Schema.Field("right", Schema.defer(DynamicValueSchema())), + (left, right) => DynamicValue.Tuple(left, right), + tuple => tuple.left, + tuple => tuple.right + ), + _.asInstanceOf[DynamicValue.Tuple] + ) + + private val transformCase: Schema.Case[DynamicValue.Transform, DynamicValue] = + Schema.Case( + "Transform", + Schema.CaseClass1[DynamicValue, DynamicValue.Transform]( + Schema.Field("value", Schema.defer(DynamicValueSchema())), + dv => DynamicValue.Transform(dv), + transform => transform.value + ), + _.asInstanceOf[DynamicValue.Transform] + ) + + private val someValueCase: Schema.Case[DynamicValue.SomeValue, DynamicValue] = + Schema.Case( + "SomeValue", + Schema.CaseClass1[DynamicValue, DynamicValue.SomeValue]( + Schema.Field("value", Schema.defer(DynamicValueSchema())), + dv => DynamicValue.SomeValue(dv), + someValue => someValue.value + ), + _.asInstanceOf[DynamicValue.SomeValue] + ) + + private val dictionaryCase: Schema.Case[DynamicValue.Dictionary, DynamicValue] = + Schema.Case( + "Dictionary", + Schema.CaseClass1[Chunk[(DynamicValue, DynamicValue)], DynamicValue.Dictionary]( + Schema.Field( + "entries", + Schema.defer(Schema.chunk(Schema.tuple2(DynamicValueSchema(), DynamicValueSchema()))) + ), + chunk => DynamicValue.Dictionary(chunk), + dictionary => dictionary.entries + ), + _.asInstanceOf[DynamicValue.Dictionary] + ) + + private val sequenceCase: Schema.Case[DynamicValue.Sequence, DynamicValue] = + Schema.Case( + "Sequence", + Schema.CaseClass1[Chunk[DynamicValue], DynamicValue.Sequence]( + Schema.Field("values", Schema.defer(Schema.chunk(DynamicValueSchema()))), + chunk => DynamicValue.Sequence(chunk), + seq => seq.values + ), + _.asInstanceOf[DynamicValue.Sequence] + ) + + private val enumerationCase: Schema.Case[DynamicValue.Enumeration, DynamicValue] = + Schema.Case( + "Enumeration", + Schema.CaseClass1[(String, DynamicValue), DynamicValue.Enumeration]( + Schema.Field("value", Schema.defer(Schema.tuple2(Schema.primitive[String], DynamicValueSchema()))), + value => DynamicValue.Enumeration(value), + enumeration => enumeration.value + ), + _.asInstanceOf[DynamicValue.Enumeration] + ) + + private val recordCase: Schema.Case[DynamicValue.Record, DynamicValue] = + Schema.Case( + "Record", + Schema.CaseClass1[Map[String, DynamicValue], DynamicValue.Record]( + Schema.Field("values", Schema.defer(Schema.map(Schema.primitive[String], DynamicValueSchema()))), + map => DynamicValue.Record(map.asInstanceOf[ListMap[String, DynamicValue]]), + record => record.values + ), + _.asInstanceOf[DynamicValue.Record] + ) + + private val dynamicAstCase: Schema.Case[DynamicValue.DynamicAst, DynamicValue] = + Schema.Case( + "DynamicAst", + Schema.CaseClass1[SchemaAst, DynamicValue.DynamicAst]( + Schema.Field("ast", SchemaAst.schema), + schemaAst => DynamicValue.DynamicAst(schemaAst), + dynamicAst => dynamicAst.ast + ), + _.asInstanceOf[DynamicValue.DynamicAst] + ) + + private val singletonCase: Schema.Case[DynamicValue.Singleton[Any], DynamicValue] = + Schema.Case( + "Singleton", + Schema[Unit].transform(_ => DynamicValue.Singleton(()), _ => ()), + _.asInstanceOf[DynamicValue.Singleton[Any]] + ) + + private val primitiveUnitCase: Schema.Case[DynamicValue.Primitive[Unit], DynamicValue] = + Schema.Case( + "Unit", + Schema.primitive[Unit].transform(unit => DynamicValue.Primitive(unit, StandardType[Unit]), _.value), { + case dv @ DynamicValue.Primitive((), _) => dv.asInstanceOf[DynamicValue.Primitive[Unit]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveStringCase: Schema.Case[DynamicValue.Primitive[String], DynamicValue] = + Schema.Case( + "String", + Schema.primitive[String].transform(s => DynamicValue.Primitive(s, StandardType[String]), _.value), { + case dv @ DynamicValue.Primitive(_: String, _) => dv.asInstanceOf[DynamicValue.Primitive[String]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveBooleanCase: Schema.Case[DynamicValue.Primitive[Boolean], DynamicValue] = + Schema.Case( + "Boolean", + Schema.primitive[Boolean].transform(b => DynamicValue.Primitive(b, StandardType[Boolean]), _.value), { + case dv @ DynamicValue.Primitive(_: Boolean, _) => dv.asInstanceOf[DynamicValue.Primitive[Boolean]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveShortCase: Schema.Case[DynamicValue.Primitive[Short], DynamicValue] = + Schema.Case( + "Short", + Schema.primitive[Short].transform(sh => DynamicValue.Primitive(sh, StandardType[Short]), _.value), { + case dv @ DynamicValue.Primitive(_: Short, _) => dv.asInstanceOf[DynamicValue.Primitive[Short]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveIntCase: Schema.Case[DynamicValue.Primitive[Int], DynamicValue] = + Schema.Case( + "Int", + Schema.primitive[Int].transform(i => DynamicValue.Primitive(i, StandardType[Int]), _.value), { + case dv @ DynamicValue.Primitive(_: Int, _) => dv.asInstanceOf[DynamicValue.Primitive[Int]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveLongCase: Schema.Case[DynamicValue.Primitive[Long], DynamicValue] = + Schema.Case( + "Long", + Schema.primitive[Long].transform(l => DynamicValue.Primitive(l, StandardType[Long]), _.value), { + case dv @ DynamicValue.Primitive(_: Long, _) => dv.asInstanceOf[DynamicValue.Primitive[Long]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveFloatCase: Schema.Case[DynamicValue.Primitive[Float], DynamicValue] = + Schema.Case( + "Float", + Schema.primitive[Float].transform(f => DynamicValue.Primitive(f, StandardType[Float]), _.value), { + case dv @ DynamicValue.Primitive(_: Float, _) => dv.asInstanceOf[DynamicValue.Primitive[Float]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveDoubleCase: Schema.Case[DynamicValue.Primitive[Double], DynamicValue] = + Schema.Case( + "Double", + Schema.primitive[Double].transform(d => DynamicValue.Primitive(d, StandardType[Double]), _.value), { + case dv @ DynamicValue.Primitive(_: Double, _) => dv.asInstanceOf[DynamicValue.Primitive[Double]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveBinaryCase: Schema.Case[DynamicValue.Primitive[Chunk[Byte]], DynamicValue] = + Schema.Case( + "Binary", + Schema.primitive[Chunk[Byte]].transform(ch => DynamicValue.Primitive(ch, StandardType[Chunk[Byte]]), _.value), { + case dv @ DynamicValue.Primitive(_: Chunk[_], _) => dv.asInstanceOf[DynamicValue.Primitive[Chunk[Byte]]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveCharCase: Schema.Case[DynamicValue.Primitive[Char], DynamicValue] = + Schema.Case( + "Char", + Schema.primitive[Char].transform(ch => DynamicValue.Primitive(ch, StandardType[Char]), _.value), { + case dv @ DynamicValue.Primitive(_: Char, _) => dv.asInstanceOf[DynamicValue.Primitive[Char]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveBigDecimalCase: Schema.Case[DynamicValue.Primitive[BigDecimal], DynamicValue] = + Schema.Case( + "BigDecimal", + Schema.primitive[BigDecimal].transform(bd => DynamicValue.Primitive(bd, StandardType[BigDecimal]), _.value), { + case dv @ DynamicValue.Primitive(_: BigDecimal, _) => dv.asInstanceOf[DynamicValue.Primitive[BigDecimal]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveBigIntegerCase: Schema.Case[DynamicValue.Primitive[BigInteger], DynamicValue] = + Schema.Case( + "BigInteger", + Schema.primitive[BigInteger].transform(bi => DynamicValue.Primitive(bi, StandardType[BigInteger]), _.value), { + case dv @ DynamicValue.Primitive(_: BigInteger, _) => dv.asInstanceOf[DynamicValue.Primitive[BigInteger]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveDayOfWeekCase: Schema.Case[DynamicValue.Primitive[DayOfWeek], DynamicValue] = + Schema.Case( + "DayOfWeek", + Schema.primitive[DayOfWeek].transform(dw => DynamicValue.Primitive(dw, StandardType[DayOfWeek]), _.value), { + case dv @ DynamicValue.Primitive(_: DayOfWeek, _) => dv.asInstanceOf[DynamicValue.Primitive[DayOfWeek]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveMonthCase: Schema.Case[DynamicValue.Primitive[Month], DynamicValue] = + Schema.Case( + "Month", + Schema.primitive[Month].transform(m => DynamicValue.Primitive(m, StandardType[Month]), _.value), { + case dv @ DynamicValue.Primitive(_: Month, _) => dv.asInstanceOf[DynamicValue.Primitive[Month]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveMonthDayCase: Schema.Case[DynamicValue.Primitive[MonthDay], DynamicValue] = + Schema.Case( + "MonthDay", + Schema.primitive[MonthDay].transform(md => DynamicValue.Primitive(md, StandardType[MonthDay]), _.value), { + case dv @ DynamicValue.Primitive(_: MonthDay, _) => dv.asInstanceOf[DynamicValue.Primitive[MonthDay]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitivePeriodCase: Schema.Case[DynamicValue.Primitive[Period], DynamicValue] = + Schema.Case( + "Period", + Schema.primitive[Period].transform(p => DynamicValue.Primitive(p, StandardType[Period]), _.value), { + case dv @ DynamicValue.Primitive(_: Period, _) => dv.asInstanceOf[DynamicValue.Primitive[Period]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveYearCase: Schema.Case[DynamicValue.Primitive[Year], DynamicValue] = + Schema.Case( + "Year", + Schema.primitive[Year].transform(y => DynamicValue.Primitive(y, StandardType[Year]), _.value), { + case dv @ DynamicValue.Primitive(_: Year, _) => dv.asInstanceOf[DynamicValue.Primitive[Year]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveYearMonthCase: Schema.Case[DynamicValue.Primitive[YearMonth], DynamicValue] = + Schema.Case( + "YearMonth", + Schema.primitive[YearMonth].transform(ym => DynamicValue.Primitive(ym, StandardType[YearMonth]), _.value), { + case dv @ DynamicValue.Primitive(_: YearMonth, _) => dv.asInstanceOf[DynamicValue.Primitive[YearMonth]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveZoneIdCase: Schema.Case[DynamicValue.Primitive[ZoneId], DynamicValue] = + Schema.Case( + "ZoneId", + Schema.primitive[ZoneId].transform(zid => DynamicValue.Primitive(zid, StandardType[ZoneId]), _.value), { + case dv @ DynamicValue.Primitive(_: ZoneId, _) => dv.asInstanceOf[DynamicValue.Primitive[ZoneId]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveZoneOffsetCase: Schema.Case[DynamicValue.Primitive[ZoneOffset], DynamicValue] = + Schema.Case( + "ZoneOffset", + Schema.primitive[ZoneOffset].transform(zo => DynamicValue.Primitive(zo, StandardType[ZoneOffset]), _.value), { + case dv @ DynamicValue.Primitive(_: ZoneOffset, _) => dv.asInstanceOf[DynamicValue.Primitive[ZoneOffset]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveInstantCase: Schema.Case[DynamicValue.Primitive[Instant], DynamicValue] = + Schema.Case( + "Instant", + Schema.primitive[Instant].transform(i => DynamicValue.Primitive(i, StandardType[Instant]), _.value), { + case dv @ DynamicValue.Primitive(_: Instant, _) => dv.asInstanceOf[DynamicValue.Primitive[Instant]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveLocalDateCase: Schema.Case[DynamicValue.Primitive[LocalDate], DynamicValue] = + Schema.Case( + "LocalDate", + Schema.primitive[LocalDate].transform(ld => DynamicValue.Primitive(ld, StandardType[LocalDate]), _.value), { + case dv @ DynamicValue.Primitive(_: LocalDate, _) => dv.asInstanceOf[DynamicValue.Primitive[LocalDate]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveLocalTimeCase: Schema.Case[DynamicValue.Primitive[LocalTime], DynamicValue] = + Schema.Case( + "LocalTime", + Schema.primitive[LocalTime].transform(lt => DynamicValue.Primitive(lt, StandardType[LocalTime]), _.value), { + case dv @ DynamicValue.Primitive(_: LocalTime, _) => dv.asInstanceOf[DynamicValue.Primitive[LocalTime]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveLocalDateTimeCase: Schema.Case[DynamicValue.Primitive[LocalDateTime], DynamicValue] = + Schema.Case( + "LocalDateTime", + Schema + .primitive[LocalDateTime] + .transform(ldt => DynamicValue.Primitive(ldt, StandardType[LocalDateTime]), _.value), { + case dv @ DynamicValue.Primitive(_: LocalDateTime, _) => dv.asInstanceOf[DynamicValue.Primitive[LocalDateTime]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveOffsetTimeCase: Schema.Case[DynamicValue.Primitive[OffsetTime], DynamicValue] = + Schema.Case( + "OffsetTime", + Schema.primitive[OffsetTime].transform(ot => DynamicValue.Primitive(ot, StandardType[OffsetTime]), _.value), { + case dv @ DynamicValue.Primitive(_: OffsetTime, _) => dv.asInstanceOf[DynamicValue.Primitive[OffsetTime]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveOffsetDateTimeCase: Schema.Case[DynamicValue.Primitive[OffsetDateTime], DynamicValue] = + Schema.Case( + "OffsetDateTime", + Schema + .primitive[OffsetDateTime] + .transform(odt => DynamicValue.Primitive(odt, StandardType[OffsetDateTime]), _.value), { + case dv @ DynamicValue.Primitive(_: OffsetDateTime, _) => + dv.asInstanceOf[DynamicValue.Primitive[OffsetDateTime]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveZonedDateTimeCase: Schema.Case[DynamicValue.Primitive[ZonedDateTime], DynamicValue] = + Schema.Case( + "ZonedDateTime", + Schema + .primitive[ZonedDateTime] + .transform(zdt => DynamicValue.Primitive(zdt, StandardType[ZonedDateTime]), _.value), { + case dv @ DynamicValue.Primitive(_: ZonedDateTime, _) => dv.asInstanceOf[DynamicValue.Primitive[ZonedDateTime]] + case _ => throw new IllegalArgumentException + } + ) + + private val primitiveUUIDCase: Schema.Case[DynamicValue.Primitive[UUID], DynamicValue] = + Schema.Case( + "UUID", + Schema.primitive[UUID].transform(uuid => DynamicValue.Primitive(uuid, StandardType[UUID]), _.value), { + case dv @ DynamicValue.Primitive(_: UUID, _) => dv.asInstanceOf[DynamicValue.Primitive[UUID]] + case _ => throw new IllegalArgumentException + } + ) + } diff --git a/zio-schema/shared/src/main/scala/zio/schema/Schema.scala b/zio-schema/shared/src/main/scala/zio/schema/Schema.scala index 46435ee78..dea45e9f9 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/Schema.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/Schema.scala @@ -216,6 +216,8 @@ object Schema extends TupleSchemas with RecordSchemas with EnumSchemas { implicit val none: Schema[None.type] = Schema[Unit].transform(_ => None, _ => ()) + implicit val dynamicValue: Schema[DynamicValue] = DynamicValueSchema() + implicit def chunk[A](implicit schemaA: Schema[A]): Schema[Chunk[A]] = Schema.Sequence[Chunk[A], A](schemaA, identity, identity, Chunk.empty) diff --git a/zio-schema/shared/src/main/scala/zio/schema/StandardType.scala b/zio-schema/shared/src/main/scala/zio/schema/StandardType.scala index 72da0833c..61284ced4 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/StandardType.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/StandardType.scala @@ -62,21 +62,21 @@ object StandardType { case Tags.CHAR => Some(CharType) case Tags.BIG_DECIMAL => Some(BigDecimalType) case Tags.BIG_INTEGER => Some(BigIntegerType) - case Tags.MONTH => Some(Month) - case Tags.MONTH_DAY => Some(MonthDay) - case Tags.PERIOD => Some(Period) + case Tags.MONTH => Some(MonthType) + case Tags.MONTH_DAY => Some(MonthDayType) + case Tags.PERIOD => Some(PeriodType) case Tags.DAY_OF_WEEK => Some(DayOfWeekType) - case Tags.YEAR => Some(Year) - case Tags.YEAR_MONTH => Some(YearMonth) - case Tags.ZONE_ID => Some(ZoneId) - case Tags.ZONE_OFFSET => Some(ZoneOffset) - case Tags.INSTANT => Some(Instant(DateTimeFormatter.ISO_INSTANT)) - case Tags.LOCAL_DATE => Some(LocalDate(DateTimeFormatter.ISO_LOCAL_DATE)) - case Tags.LOCAL_TIME => Some(LocalTime(DateTimeFormatter.ISO_LOCAL_TIME)) - case Tags.LOCAL_DATE_TIME => Some(LocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME)) - case Tags.OFFSET_TIME => Some(OffsetTime(DateTimeFormatter.ISO_OFFSET_TIME)) - case Tags.OFFSET_DATE_TIME => Some(OffsetDateTime(DateTimeFormatter.ISO_OFFSET_DATE_TIME)) - case Tags.ZONED_DATE_TIME => Some(ZonedDateTime(DateTimeFormatter.ISO_ZONED_DATE_TIME)) + case Tags.YEAR => Some(YearType) + case Tags.YEAR_MONTH => Some(YearMonthType) + case Tags.ZONE_ID => Some(ZoneIdType) + case Tags.ZONE_OFFSET => Some(ZoneOffsetType) + case Tags.INSTANT => Some(InstantType(DateTimeFormatter.ISO_INSTANT)) + case Tags.LOCAL_DATE => Some(LocalDateType(DateTimeFormatter.ISO_LOCAL_DATE)) + case Tags.LOCAL_TIME => Some(LocalTimeType(DateTimeFormatter.ISO_LOCAL_TIME)) + case Tags.LOCAL_DATE_TIME => Some(LocalDateTimeType(DateTimeFormatter.ISO_LOCAL_DATE_TIME)) + case Tags.OFFSET_TIME => Some(OffsetTimeType(DateTimeFormatter.ISO_OFFSET_TIME)) + case Tags.OFFSET_DATE_TIME => Some(OffsetDateTimeType(DateTimeFormatter.ISO_OFFSET_DATE_TIME)) + case Tags.ZONED_DATE_TIME => Some(ZonedDateTimeType(DateTimeFormatter.ISO_ZONED_DATE_TIME)) case Tags.UUID => Some(UUIDType) case units => try { @@ -84,6 +84,8 @@ object StandardType { } catch { case _: Throwable => None } } + def apply[A](implicit standatdType: StandardType[A]): StandardType[A] = standatdType + def fromTemporalUnits(units: String): Option[StandardType[java.time.Duration]] = ChronoUnit.values().find(_.toString == units).map(Duration(_)) @@ -175,20 +177,20 @@ object StandardType { Right(java.time.temporal.WeekFields.of(java.util.Locale.getDefault).getFirstDayOfWeek) } - implicit object Month extends StandardType[java.time.Month] { + implicit object MonthType extends StandardType[java.time.Month] { override def tag = Tags.MONTH override def compare(x: Month, y: Month): Int = x.getValue.compareTo(y.getValue) override def defaultValue: Either[String, java.time.Month] = Right(java.time.Month.JANUARY) } - implicit object MonthDay extends StandardType[java.time.MonthDay] { + implicit object MonthDayType extends StandardType[java.time.MonthDay] { override def tag = Tags.MONTH_DAY override def compare(x: MonthDay, y: MonthDay): Int = x.compareTo(y) override def defaultValue: Either[String, java.time.MonthDay] = Right(java.time.MonthDay.of(java.time.Month.JANUARY, 1)) } - implicit object Period extends StandardType[java.time.Period] { + implicit object PeriodType extends StandardType[java.time.Period] { override def tag = Tags.PERIOD override def compare(x: Period, y: Period): Int = { val startDate = time.LocalDate.of(0, 1, 1) @@ -197,25 +199,25 @@ object StandardType { override def defaultValue: Either[String, java.time.Period] = Right(java.time.Period.ZERO) } - implicit object Year extends StandardType[java.time.Year] { + implicit object YearType extends StandardType[java.time.Year] { override def tag = Tags.YEAR override def compare(x: Year, y: Year): Int = x.getValue.compareTo(y.getValue) override def defaultValue: Either[String, java.time.Year] = Right(java.time.Year.now) } - implicit object YearMonth extends StandardType[java.time.YearMonth] { + implicit object YearMonthType extends StandardType[java.time.YearMonth] { override def tag = Tags.YEAR_MONTH override def compare(x: YearMonth, y: YearMonth): Int = x.compareTo(y) override def defaultValue: Either[String, java.time.YearMonth] = Right(java.time.YearMonth.now) } - implicit object ZoneId extends StandardType[java.time.ZoneId] { + implicit object ZoneIdType extends StandardType[java.time.ZoneId] { override def tag = Tags.ZONE_ID override def compare(x: ZoneId, y: ZoneId): Int = x.getId.compareTo(y.getId) // TODO is there a better comparison override def defaultValue: Either[String, java.time.ZoneId] = Right(java.time.ZoneId.systemDefault) } - implicit object ZoneOffset extends StandardType[java.time.ZoneOffset] { + implicit object ZoneOffsetType extends StandardType[java.time.ZoneOffset] { override def tag = Tags.ZONE_OFFSET override def compare(x: ZoneOffset, y: ZoneOffset): Int = x.compareTo(y) override def defaultValue: Either[String, java.time.ZoneOffset] = Right(java.time.ZoneOffset.UTC) @@ -227,43 +229,43 @@ object StandardType { override def defaultValue: Either[String, java.time.Duration] = Right(java.time.Duration.ZERO) } - final case class Instant(formatter: DateTimeFormatter) extends StandardType[java.time.Instant] { + final case class InstantType(formatter: DateTimeFormatter) extends StandardType[java.time.Instant] { override def tag = Tags.INSTANT override def compare(x: time.Instant, y: time.Instant): Int = x.compareTo(y) override def defaultValue: Either[String, java.time.Instant] = Right(java.time.Instant.EPOCH) } - final case class LocalDate(formatter: DateTimeFormatter) extends StandardType[java.time.LocalDate] { + final case class LocalDateType(formatter: DateTimeFormatter) extends StandardType[java.time.LocalDate] { override def tag = Tags.LOCAL_DATE override def compare(x: time.LocalDate, y: time.LocalDate): Int = x.compareTo(y) override def defaultValue: Either[String, java.time.LocalDate] = Right(java.time.LocalDate.now) } - final case class LocalTime(formatter: DateTimeFormatter) extends StandardType[java.time.LocalTime] { + final case class LocalTimeType(formatter: DateTimeFormatter) extends StandardType[java.time.LocalTime] { override def tag = Tags.LOCAL_TIME override def compare(x: time.LocalTime, y: time.LocalTime): Int = x.compareTo(y) override def defaultValue: Either[String, java.time.LocalTime] = Right(java.time.LocalTime.MIDNIGHT) } - final case class LocalDateTime(formatter: DateTimeFormatter) extends StandardType[java.time.LocalDateTime] { + final case class LocalDateTimeType(formatter: DateTimeFormatter) extends StandardType[java.time.LocalDateTime] { override def tag = Tags.LOCAL_DATE_TIME override def compare(x: time.LocalDateTime, y: time.LocalDateTime): Int = x.compareTo(y) override def defaultValue: Either[String, java.time.LocalDateTime] = Right(java.time.LocalDateTime.now) } - final case class OffsetTime(formatter: DateTimeFormatter) extends StandardType[java.time.OffsetTime] { + final case class OffsetTimeType(formatter: DateTimeFormatter) extends StandardType[java.time.OffsetTime] { override def tag = Tags.OFFSET_TIME override def compare(x: time.OffsetTime, y: time.OffsetTime): Int = x.compareTo(y) override def defaultValue: Either[String, java.time.OffsetTime] = Right(java.time.OffsetTime.now) } - final case class OffsetDateTime(formatter: DateTimeFormatter) extends StandardType[java.time.OffsetDateTime] { + final case class OffsetDateTimeType(formatter: DateTimeFormatter) extends StandardType[java.time.OffsetDateTime] { override def tag = Tags.OFFSET_DATE_TIME override def compare(x: time.OffsetDateTime, y: time.OffsetDateTime): Int = x.compareTo(y) override def defaultValue: Either[String, java.time.OffsetDateTime] = Right(java.time.OffsetDateTime.now) } - final case class ZonedDateTime(formatter: DateTimeFormatter) extends StandardType[java.time.ZonedDateTime] { + final case class ZonedDateTimeType(formatter: DateTimeFormatter) extends StandardType[java.time.ZonedDateTime] { override def tag = Tags.ZONED_DATE_TIME override def compare(x: time.ZonedDateTime, y: time.ZonedDateTime): Int = x.compareTo(y) override def defaultValue: Either[String, java.time.ZonedDateTime] = Right(java.time.ZonedDateTime.now) @@ -272,17 +274,17 @@ object StandardType { trait DefaultJavaTimeSchemas { implicit val instantSchema: Schema[java.time.Instant] = - Schema.primitive(StandardType.Instant(DateTimeFormatter.ISO_INSTANT)) + Schema.primitive(StandardType.InstantType(DateTimeFormatter.ISO_INSTANT)) implicit val localDateSchema: Schema[java.time.LocalDate] = - Schema.primitive(StandardType.LocalDate(DateTimeFormatter.ISO_LOCAL_DATE)) + Schema.primitive(StandardType.LocalDateType(DateTimeFormatter.ISO_LOCAL_DATE)) implicit val localTimeSchema: Schema[java.time.LocalTime] = - Schema.primitive(StandardType.LocalTime(DateTimeFormatter.ISO_LOCAL_TIME)) + Schema.primitive(StandardType.LocalTimeType(DateTimeFormatter.ISO_LOCAL_TIME)) implicit val localDateTimeSchema: Schema[java.time.LocalDateTime] = - Schema.primitive(StandardType.LocalDateTime(DateTimeFormatter.ISO_LOCAL_DATE_TIME)) + Schema.primitive(StandardType.LocalDateTimeType(DateTimeFormatter.ISO_LOCAL_DATE_TIME)) implicit val offsetTimeSchema: Schema[java.time.OffsetTime] = - Schema.primitive(StandardType.OffsetTime(DateTimeFormatter.ISO_OFFSET_TIME)) + Schema.primitive(StandardType.OffsetTimeType(DateTimeFormatter.ISO_OFFSET_TIME)) implicit val offsetDateTimeSchema: Schema[java.time.OffsetDateTime] = - Schema.primitive(StandardType.OffsetDateTime(DateTimeFormatter.ISO_OFFSET_DATE_TIME)) + Schema.primitive(StandardType.OffsetDateTimeType(DateTimeFormatter.ISO_OFFSET_DATE_TIME)) implicit val zonedDateTimeSchema: Schema[java.time.ZonedDateTime] = - Schema.primitive(StandardType.ZonedDateTime(DateTimeFormatter.ISO_ZONED_DATE_TIME)) + Schema.primitive(StandardType.ZonedDateTimeType(DateTimeFormatter.ISO_ZONED_DATE_TIME)) } diff --git a/zio-schema/shared/src/test/scala/zio/schema/SchemaAssertions.scala b/zio-schema/shared/src/test/scala/zio/schema/SchemaAssertions.scala index d94435501..c3b77fec5 100644 --- a/zio-schema/shared/src/test/scala/zio/schema/SchemaAssertions.scala +++ b/zio-schema/shared/src/test/scala/zio/schema/SchemaAssertions.scala @@ -31,16 +31,28 @@ object SchemaAssertions { Assertion.assertion("hasSameAst")(param(expected))(actual => equalsAst(expected, actual)) private def equalsAst(expected: Schema[_], actual: Schema[_], depth: Int = 0): Boolean = (expected, actual) match { - case (Schema.Primitive(StandardType.Duration(_), _), Schema.Primitive(StandardType.Duration(_), _)) => true - case (Schema.Primitive(StandardType.Instant(_), _), Schema.Primitive(StandardType.Instant(_), _)) => true - case (Schema.Primitive(StandardType.LocalDate(_), _), Schema.Primitive(StandardType.LocalDate(_), _)) => true - case (Schema.Primitive(StandardType.LocalTime(_), _), Schema.Primitive(StandardType.LocalTime(_), _)) => true - case (Schema.Primitive(StandardType.LocalDateTime(_), _), Schema.Primitive(StandardType.LocalDateTime(_), _)) => + case (Schema.Primitive(StandardType.Duration(_), _), Schema.Primitive(StandardType.Duration(_), _)) => true + case (Schema.Primitive(StandardType.InstantType(_), _), Schema.Primitive(StandardType.InstantType(_), _)) => true + case (Schema.Primitive(StandardType.LocalDateType(_), _), Schema.Primitive(StandardType.LocalDateType(_), _)) => true - case (Schema.Primitive(StandardType.ZonedDateTime(_), _), Schema.Primitive(StandardType.ZonedDateTime(_), _)) => + case (Schema.Primitive(StandardType.LocalTimeType(_), _), Schema.Primitive(StandardType.LocalTimeType(_), _)) => true - case (Schema.Primitive(StandardType.OffsetTime(_), _), Schema.Primitive(StandardType.OffsetTime(_), _)) => true - case (Schema.Primitive(StandardType.OffsetDateTime(_), _), Schema.Primitive(StandardType.OffsetDateTime(_), _)) => + case ( + Schema.Primitive(StandardType.LocalDateTimeType(_), _), + Schema.Primitive(StandardType.LocalDateTimeType(_), _) + ) => + true + case ( + Schema.Primitive(StandardType.ZonedDateTimeType(_), _), + Schema.Primitive(StandardType.ZonedDateTimeType(_), _) + ) => + true + case (Schema.Primitive(StandardType.OffsetTimeType(_), _), Schema.Primitive(StandardType.OffsetTimeType(_), _)) => + true + case ( + Schema.Primitive(StandardType.OffsetDateTimeType(_), _), + Schema.Primitive(StandardType.OffsetDateTimeType(_), _) + ) => true case (Schema.Primitive(tpe1, _), Schema.Primitive(tpe2, _)) => tpe1 == tpe2 case (Schema.Optional(expected, _), Schema.Optional(actual, _)) => equalsAst(expected, actual, depth)