Skip to content

Commit

Permalink
Issue#375 (zio#432)
Browse files Browse the repository at this point in the history
* Simplify the java.time.* StandardTypes

* fix:
- fix compile errors

* fix compile errors

* fixed tests, fix Patch implementation, formatted

* removed unused code

* formatted

* formatted

Co-authored-by: Nikita Sozinov <nikita_sozinov@mail.ru>
  • Loading branch information
devsprint and lookingformira authored Nov 17, 2022
1 parent 6bdebc1 commit ceb646d
Show file tree
Hide file tree
Showing 24 changed files with 533 additions and 614 deletions.
60 changes: 30 additions & 30 deletions tests/shared/src/test/scala-2/zio/schema/DynamicValueGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,36 @@ object DynamicValueGen {
def gen[A1](typ: StandardType[A1], gen: Gen[Sized, A1]) = gen.map(DynamicValue.Primitive(_, typ))

standardType match {
case typ: StandardType.BinaryType.type => gen(typ, Gen.chunkOf(Gen.byte))
case typ: StandardType.BoolType.type => gen(typ, Gen.oneOf(Gen.const(true), Gen.const(false)))
case typ: StandardType.CharType.type => gen(typ, Gen.asciiChar)
case typ: StandardType.DoubleType.type => gen(typ, Gen.double)
case typ: StandardType.StringType.type => gen(typ, Gen.string)
case typ: StandardType.ShortType.type => gen(typ, Gen.short)
case typ: StandardType.ByteType.type => gen(typ, Gen.byte)
case typ: StandardType.IntType.type => gen(typ, Gen.int)
case typ: StandardType.LongType.type => gen(typ, Gen.long)
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.DayOfWeekType.type => gen(typ, JavaTimeGen.anyDayOfWeek)
case typ: StandardType.DurationType.type => gen(typ, JavaTimeGen.anyDuration)
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.uuid)
case typ: StandardType.BinaryType.type => gen(typ, Gen.chunkOf(Gen.byte))
case typ: StandardType.BoolType.type => gen(typ, Gen.oneOf(Gen.const(true), Gen.const(false)))
case typ: StandardType.CharType.type => gen(typ, Gen.asciiChar)
case typ: StandardType.DoubleType.type => gen(typ, Gen.double)
case typ: StandardType.StringType.type => gen(typ, Gen.string)
case typ: StandardType.ShortType.type => gen(typ, Gen.short)
case typ: StandardType.ByteType.type => gen(typ, Gen.byte)
case typ: StandardType.IntType.type => gen(typ, Gen.int)
case typ: StandardType.LongType.type => gen(typ, Gen.long)
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.DayOfWeekType.type => gen(typ, JavaTimeGen.anyDayOfWeek)
case typ: StandardType.DurationType.type => gen(typ, JavaTimeGen.anyDuration)
case typ: StandardType.InstantType.type => gen(typ, JavaTimeGen.anyInstant)
case typ: StandardType.LocalDateType.type => gen(typ, JavaTimeGen.anyLocalDate)
case typ: StandardType.LocalDateTimeType.type => gen(typ, JavaTimeGen.anyLocalDateTime)
case typ: StandardType.LocalTimeType.type => 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.type => gen(typ, JavaTimeGen.anyOffsetDateTime)
case typ: StandardType.OffsetTimeType.type => 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.type => 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.uuid)
}
}

Expand Down
3 changes: 1 addition & 2 deletions tests/shared/src/test/scala-2/zio/schema/PatchSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import zio.test.Assertion._
import zio.test._
import zio.{ Chunk, Scope, URIO }

object PatchSpec extends ZIOSpecDefault with DefaultJavaTimeSchemas {
object PatchSpec extends ZIOSpecDefault {

def spec: Spec[TestEnvironment with Scope, Any] = suite("PatchSpec")(
suite("identity law")(
Expand Down Expand Up @@ -182,7 +182,6 @@ object PatchSpec extends ZIOSpecDefault with DefaultJavaTimeSchemas {
val afterInvert = diff.invert.invert
val patched = schema.diff(l, r).patch(l)
val patchedAfterInvert = afterInvert.patch(l)
if (patched.isLeft || patchedAfterInvert.isLeft) println(diff)
assert(patched)(isRight(equalTo(r))) && assert(patchedAfterInvert)(isRight(equalTo(r)))
} else {
assertTrue(true)
Expand Down
16 changes: 7 additions & 9 deletions tests/shared/src/test/scala-2/zio/schema/SchemaGen.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package zio.schema

import java.time.format.DateTimeFormatter

import scala.collection.immutable.ListMap

import zio.Chunk
Expand Down Expand Up @@ -689,36 +687,36 @@ object SchemaGen {
),
SchemaTest("DayOfWeek", StandardType.DayOfWeekType, JavaTimeGen.anyDayOfWeek),
SchemaTest("Duration", StandardType.DurationType, JavaTimeGen.anyDuration),
SchemaTest("Instant", StandardType.InstantType(DateTimeFormatter.ISO_DATE_TIME), JavaTimeGen.anyInstant),
SchemaTest("LocalDate", StandardType.LocalDateType(DateTimeFormatter.ISO_DATE), JavaTimeGen.anyLocalDate),
SchemaTest("Instant", StandardType.InstantType, JavaTimeGen.anyInstant),
SchemaTest("LocalDate", StandardType.LocalDateType, JavaTimeGen.anyLocalDate),
SchemaTest(
"LocalDateTime",
StandardType.LocalDateTimeType(DateTimeFormatter.ISO_LOCAL_DATE_TIME),
StandardType.LocalDateTimeType,
JavaTimeGen.anyLocalDateTime
),
SchemaTest(
"LocalTime",
StandardType.LocalTimeType(DateTimeFormatter.ISO_LOCAL_TIME),
StandardType.LocalTimeType,
JavaTimeGen.anyLocalTime
),
SchemaTest("Month", StandardType.MonthType, JavaTimeGen.anyMonth),
SchemaTest("MonthDay", StandardType.MonthDayType, JavaTimeGen.anyMonthDay),
SchemaTest(
"OffsetDateTime",
StandardType.OffsetDateTimeType(DateTimeFormatter.ISO_OFFSET_DATE_TIME),
StandardType.OffsetDateTimeType,
JavaTimeGen.anyOffsetDateTime
),
SchemaTest(
"OffsetTime",
StandardType.OffsetTimeType(DateTimeFormatter.ISO_OFFSET_TIME),
StandardType.OffsetTimeType,
JavaTimeGen.anyOffsetTime
),
SchemaTest("Period", StandardType.PeriodType, JavaTimeGen.anyPeriod),
SchemaTest("Year", StandardType.YearType, JavaTimeGen.anyYear),
SchemaTest("YearMonth", StandardType.YearMonthType, JavaTimeGen.anyYearMonth),
SchemaTest(
"ZonedDateTime",
StandardType.ZonedDateTimeType(DateTimeFormatter.ISO_ZONED_DATE_TIME),
StandardType.ZonedDateTimeType,
JavaTimeGen.anyZonedDateTime
),
SchemaTest("ZoneId", StandardType.ZoneIdType, JavaTimeGen.anyZoneId),
Expand Down
73 changes: 36 additions & 37 deletions tests/shared/src/test/scala-2/zio/schema/StandardTypeGen.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zio.schema

import java.math.{ BigDecimal => JBigDecimal, BigInteger => JBigInt }
import java.time.format.DateTimeFormatter

import zio.test.{ Gen, Sized }

Expand All @@ -23,18 +22,18 @@ object StandardTypeGen {
(StandardType.UUIDType),
(StandardType.DayOfWeekType),
(StandardType.DurationType),
(StandardType.InstantType(DateTimeFormatter.ISO_INSTANT)),
(StandardType.LocalDateType(DateTimeFormatter.ISO_LOCAL_DATE)),
(StandardType.LocalDateTimeType(DateTimeFormatter.ISO_LOCAL_DATE_TIME)),
(StandardType.LocalTimeType(DateTimeFormatter.ISO_LOCAL_TIME)),
(StandardType.InstantType),
(StandardType.LocalDateType),
(StandardType.LocalDateTimeType),
(StandardType.LocalTimeType),
(StandardType.MonthType),
(StandardType.MonthDayType),
(StandardType.OffsetDateTimeType(DateTimeFormatter.ISO_OFFSET_DATE_TIME)),
(StandardType.OffsetTimeType(DateTimeFormatter.ISO_OFFSET_TIME)),
(StandardType.OffsetDateTimeType),
(StandardType.OffsetTimeType),
(StandardType.PeriodType),
(StandardType.YearType),
(StandardType.YearMonthType),
(StandardType.ZonedDateTimeType(DateTimeFormatter.ISO_ZONED_DATE_TIME)),
(StandardType.ZonedDateTimeType),
(StandardType.ZoneIdType)
)
//FIXME For some reason adding this causes other unrelated tests to break.
Expand All @@ -53,35 +52,35 @@ object StandardTypeGen {

val anyStandardTypeAndGen: Gen[Any, StandardTypeAndGen[_]] = {
anyStandardType.map {
case typ: StandardType.StringType.type => typ -> Gen.string
case typ: StandardType.BoolType.type => typ -> Gen.boolean
case typ: StandardType.ShortType.type => typ -> Gen.short
case typ: StandardType.IntType.type => typ -> Gen.int
case typ: StandardType.LongType.type => typ -> Gen.long
case typ: StandardType.FloatType.type => typ -> Gen.float
case typ: StandardType.DoubleType.type => typ -> Gen.double
case typ: StandardType.BinaryType.type => typ -> Gen.chunkOf(Gen.byte)
case typ: StandardType.CharType.type => typ -> Gen.asciiChar
case typ: StandardType.UUIDType.type => typ -> Gen.uuid
case typ: StandardType.BigDecimalType.type => typ -> javaBigDecimal
case typ: StandardType.BigIntegerType.type => typ -> javaBigInt
case typ: StandardType.DayOfWeekType.type => typ -> JavaTimeGen.anyDayOfWeek
case typ: StandardType.DurationType.type => typ -> JavaTimeGen.anyDuration
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[_]
case typ: StandardType.StringType.type => typ -> Gen.string
case typ: StandardType.BoolType.type => typ -> Gen.boolean
case typ: StandardType.ShortType.type => typ -> Gen.short
case typ: StandardType.IntType.type => typ -> Gen.int
case typ: StandardType.LongType.type => typ -> Gen.long
case typ: StandardType.FloatType.type => typ -> Gen.float
case typ: StandardType.DoubleType.type => typ -> Gen.double
case typ: StandardType.BinaryType.type => typ -> Gen.chunkOf(Gen.byte)
case typ: StandardType.CharType.type => typ -> Gen.asciiChar
case typ: StandardType.UUIDType.type => typ -> Gen.uuid
case typ: StandardType.BigDecimalType.type => typ -> javaBigDecimal
case typ: StandardType.BigIntegerType.type => typ -> javaBigInt
case typ: StandardType.DayOfWeekType.type => typ -> JavaTimeGen.anyDayOfWeek
case typ: StandardType.DurationType.type => typ -> JavaTimeGen.anyDuration
case typ: StandardType.InstantType.type => typ -> JavaTimeGen.anyInstant
case typ: StandardType.LocalDateType.type => typ -> JavaTimeGen.anyLocalDate
case typ: StandardType.LocalDateTimeType.type => typ -> JavaTimeGen.anyLocalDateTime
case typ: StandardType.LocalTimeType.type => typ -> JavaTimeGen.anyLocalTime
case typ: StandardType.MonthType.type => typ -> JavaTimeGen.anyMonth
case typ: StandardType.MonthDayType.type => typ -> JavaTimeGen.anyMonthDay
case typ: StandardType.OffsetDateTimeType.type => typ -> JavaTimeGen.anyOffsetDateTime
case typ: StandardType.OffsetTimeType.type => 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.type => 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[_]
}
}
}
2 changes: 1 addition & 1 deletion tests/shared/src/test/scala-2/zio/schema/types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object types {
//scalafmt: { maxColumn = 400 }
sealed trait Arities

object Arities extends DefaultJavaTimeSchemas {
object Arities {
implicit val durationSchema: Schema[java.time.Duration] = Schema.primitive(StandardType.DurationType)

case object Arity0 extends Arities
Expand Down
16 changes: 7 additions & 9 deletions tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package zio.schema

import java.time.format.DateTimeFormatter

import zio.Chunk
import zio.schema.CaseSet.caseOf
import zio.schema.Schema.{ Lazy, Primitive }
Expand Down Expand Up @@ -110,37 +108,37 @@ object DefaultValueSpec extends ZIOSpecDefault {
)
},
test("Instant default value") {
assert(Primitive(StandardType.InstantType(DateTimeFormatter.ISO_INSTANT)).defaultValue)(
assert(Primitive(StandardType.InstantType).defaultValue)(
isRight(equalTo(java.time.Instant.EPOCH))
)
},
test("LocalDate default value") {
assert(Primitive(StandardType.LocalDateType(DateTimeFormatter.ISO_LOCAL_DATE)).defaultValue)(
assert(Primitive(StandardType.LocalDateType).defaultValue)(
isRight(isSubtype[java.time.LocalDate](anything))
)
},
test("LocalTime default value") {
assert(Primitive(StandardType.LocalTimeType(DateTimeFormatter.ISO_LOCAL_TIME)).defaultValue)(
assert(Primitive(StandardType.LocalTimeType).defaultValue)(
isRight(equalTo(java.time.LocalTime.MIDNIGHT))
)
},
test("LocalDateTime default value") {
assert(Primitive(StandardType.LocalDateTimeType(DateTimeFormatter.ISO_LOCAL_DATE_TIME)).defaultValue)(
assert(Primitive(StandardType.LocalDateTimeType).defaultValue)(
isRight(isSubtype[java.time.LocalDateTime](anything))
)
},
test("OffsetTime default value") {
assert(Primitive(StandardType.OffsetTimeType(DateTimeFormatter.ISO_OFFSET_TIME)).defaultValue)(
assert(Primitive(StandardType.OffsetTimeType).defaultValue)(
isRight(isSubtype[java.time.OffsetTime](anything))
)
},
test("OffsetDateTime default value") {
assert(Primitive(StandardType.OffsetDateTimeType(DateTimeFormatter.ISO_OFFSET_DATE_TIME)).defaultValue)(
assert(Primitive(StandardType.OffsetDateTimeType).defaultValue)(
isRight(isSubtype[java.time.OffsetDateTime](anything))
)
},
test("ZonedDateTime default value") {
assert(Primitive(StandardType.ZonedDateTimeType(DateTimeFormatter.ISO_ZONED_DATE_TIME)).defaultValue)(
assert(Primitive(StandardType.ZonedDateTimeType).defaultValue)(
isRight(isSubtype[java.time.ZonedDateTime](anything))
)
}
Expand Down
Loading

0 comments on commit ceb646d

Please sign in to comment.