Skip to content

Commit

Permalink
Add schema for DynamicValue (#170)
Browse files Browse the repository at this point in the history
* Add Schema for DynamicValue

* Rename some of StandardType values

* Fix type casting

* Fix linting issues

Run `prepare` on the whole project
  • Loading branch information
Ivan Grebenik authored Jan 6, 2022
1 parent 9399e40 commit a594be6
Show file tree
Hide file tree
Showing 15 changed files with 790 additions and 292 deletions.
28 changes: 14 additions & 14 deletions tests/shared/src/test/scala/zio/schema/DefaultValueSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,67 +77,67 @@ 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)(
isRight(equalTo(java.time.Duration.ZERO))
)
},
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))
)
}
Expand Down
28 changes: 14 additions & 14 deletions tests/shared/src/test/scala/zio/schema/DynamicValueGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
28 changes: 14 additions & 14 deletions tests/shared/src/test/scala/zio/schema/SchemaGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

Expand Down
54 changes: 27 additions & 27 deletions tests/shared/src/test/scala/zio/schema/StandardTypeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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[_]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}

Expand Down
Loading

0 comments on commit a594be6

Please sign in to comment.