Skip to content

Commit

Permalink
Add support for BigInteger and BigDecimal zio#32 (zio#33)
Browse files Browse the repository at this point in the history
* Add support for BigInteger and BigDecimal

* Fix formatting
  • Loading branch information
DamianReeves authored and landlockedsurfer committed May 28, 2022
1 parent 5ec691e commit 398b3d3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 27 deletions.
3 changes: 3 additions & 0 deletions core/src/main/scala/zio/schema/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ object Schema {

def apply[A](implicit codec: Schema[A]): Schema[A] = codec

implicit val bigInt: Schema[BigInt] = primitive[java.math.BigInteger].transform(BigInt(_), _.bigInteger)
implicit val bigDecimal: Schema[BigDecimal] = primitive[java.math.BigDecimal].transform(BigDecimal(_), _.bigDecimal)

def caseClassN[A, Z](t1: (String, Schema[A]))(f: A => Z, g: Z => Option[A]): Schema[Z] =
Schema
.record(Map(t1))
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/zio/schema/StandardType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ object StandardType {
implicit object BinaryType extends StandardType[Chunk[Byte]]
implicit object CharType extends StandardType[Char]

implicit object BigDecimalType extends StandardType[java.math.BigDecimal]
implicit object BigIntegerType extends StandardType[java.math.BigInteger]

//java.time specific types
implicit object DayOfWeekType extends StandardType[DayOfWeek]
implicit object Month extends StandardType[java.time.Month]
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/zio/schema/codec/JsonCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ object JsonCodec extends Codec {
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.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]
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/scala/zio/schema/codec/ProtobufCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ object ProtobufCodec extends Codec {
case StandardType.DoubleType => true
case StandardType.BinaryType => false
case StandardType.CharType => true
case StandardType.BigIntegerType => false
case StandardType.BigDecimalType => false
case StandardType.DayOfWeekType => true
case StandardType.Month => true
case StandardType.MonthDay => false
Expand Down Expand Up @@ -649,6 +651,8 @@ object ProtobufCodec extends Codec {
case StandardType.DoubleType => Some(0.0)
case StandardType.BinaryType => Some(Chunk.empty)
case StandardType.CharType => None
case StandardType.BigIntegerType => Some(java.math.BigInteger.ZERO)
case StandardType.BigDecimalType => Some(java.math.BigDecimal.ZERO)
case StandardType.DayOfWeekType => None
case StandardType.Month => None
case StandardType.MonthDay => None
Expand Down
58 changes: 31 additions & 27 deletions core/src/test/scala/zio/schema/StandardTypeGen.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package zio.schema

import java.time.format.DateTimeFormatter
import java.time.temporal.{ ChronoUnit }
import java.time.temporal.ChronoUnit

import zio.random.Random
import zio.test.{ Gen, Sized }
Expand All @@ -17,6 +17,8 @@ object StandardTypeGen {
Gen.const(StandardType.FloatType),
Gen.const(StandardType.DoubleType),
Gen.const(StandardType.BinaryType),
Gen.const(StandardType.BigDecimalType),
Gen.const(StandardType.BigIntegerType),
Gen.const(StandardType.CharType),
Gen.const(StandardType.DayOfWeekType),
Gen.const(StandardType.Duration(ChronoUnit.SECONDS)),
Expand All @@ -41,32 +43,34 @@ object StandardTypeGen {

val anyStandardTypeAndGen: Gen[Random, StandardTypeAndGen[_]] = {
anyStandardType.map {
case typ: StandardType.StringType.type => typ -> Gen.anyString
case typ: StandardType.BoolType.type => typ -> Gen.boolean
case typ: StandardType.ShortType.type => typ -> Gen.anyShort
case typ: StandardType.IntType.type => typ -> Gen.anyInt
case typ: StandardType.LongType.type => typ -> Gen.anyLong
case typ: StandardType.FloatType.type => typ -> Gen.anyFloat
case typ: StandardType.DoubleType.type => typ -> Gen.anyDouble
case typ: StandardType.BinaryType.type => typ -> Gen.chunkOf(Gen.anyByte)
case typ: StandardType.CharType.type => typ -> Gen.anyASCIIChar
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 _ => StandardType.UnitType -> Gen.unit: StandardTypeAndGen[_]
case typ: StandardType.StringType.type => typ -> Gen.anyString
case typ: StandardType.BoolType.type => typ -> Gen.boolean
case typ: StandardType.ShortType.type => typ -> Gen.anyShort
case typ: StandardType.IntType.type => typ -> Gen.anyInt
case typ: StandardType.LongType.type => typ -> Gen.anyLong
case typ: StandardType.FloatType.type => typ -> Gen.anyFloat
case typ: StandardType.DoubleType.type => typ -> Gen.anyDouble
case typ: StandardType.BinaryType.type => typ -> Gen.chunkOf(Gen.anyByte)
case typ: StandardType.CharType.type => typ -> Gen.anyASCIIChar
case typ: StandardType.BigDecimalType.type => typ -> Gen.anyDouble.map(d => java.math.BigDecimal.valueOf(d))
case typ: StandardType.BigIntegerType.type => typ -> Gen.anyLong.map(n => java.math.BigInteger.valueOf(n))
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 _ => StandardType.UnitType -> Gen.unit: StandardTypeAndGen[_]
}
}
}

0 comments on commit 398b3d3

Please sign in to comment.