Skip to content

Commit

Permalink
fixing formatting and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrapyre committed May 6, 2024
1 parent d331c6b commit 83c8fb9
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 20 deletions.
1 change: 1 addition & 0 deletions tests/shared/src/test/scala-2/zio/schema/SchemaGen.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package zio.schema

import scala.collection.immutable.ListMap

import zio.Chunk
import zio.schema.PlatformSpecificGen.platformSpecificSchemasAndGens
import zio.test.{ Gen, Sized }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package zio.schema

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

import zio.schema.PlatformSpecificGen.{ platformSpecificStandardTypeAndGen, platformSpecificStandardTypes }
import zio.test.{ Gen, Sized }

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

object StandardTypeGen {

//IMPORTANT! - Updating the following list without updating the schema primitive case set in zio.schema.DynamicValue.schema will trigger a set of very obscure test failures
Expand Down Expand Up @@ -36,9 +36,8 @@ object StandardTypeGen {
StandardType.YearType,
StandardType.YearMonthType,
StandardType.ZonedDateTimeType,
StandardType.ZoneIdType
//FIXME For some reason adding this causes other unrelated tests to break.
// StandardType.ZoneOffsetType
StandardType.ZoneIdType,
StandardType.ZoneOffsetType
)
) ++ platformSpecificStandardTypes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ object AvroCodec {
val zonedDateTime = a.asInstanceOf[java.time.ZonedDateTime]
zonedDateTime.toString
case StandardType.CurrencyType =>
a.asInstanceOf[java.util.Currency].getCurrencyCode()
a.asInstanceOf[java.util.Currency].getCurrencyCode
}

private def encodeSequence[A](schema: Schema[A], v: Chunk[A]): Any = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package zio.schema.codec

object JsonPlatformSpecific {
@SuppressWarnings(Array(
"scalafix:ExplicitResultTypes"
))
val platformSpecificEncoderTests = Seq.empty
@SuppressWarnings(Array(
"scalafix:ExplicitResultTypes"
))
val platformSpecificDecoderTests = Seq.empty
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package zio.schema.codec

import zio.schema.{ Schema, StandardType }
import zio.schema.codec.JsonCodecSpec.{ assertDecodes, assertEncodesJson, stringify, test }
import zio.schema.{ Schema, StandardType }
import zio.test.{ Gen, check }

object JsonPlatformSpecific {

@SuppressWarnings(Array(
"scalafix:ExplicitResultTypes"
))
val platformSpecificEncoderTests = Seq(
test("Currency") {
assertEncodesJson(
Expand All @@ -15,10 +18,13 @@ object JsonPlatformSpecific {
}
)

@SuppressWarnings(Array(
"scalafix:ExplicitResultTypes"
))
val platformSpecificDecoderTests = Seq(
test("Currency") {
check(Gen.currency)(
currency => assertDecodes(Schema[java.util.Currency], currency, stringify(currency.getCurrencyCode()))
currency => assertDecodes(Schema[java.util.Currency], currency, stringify(currency.getCurrencyCode))
)
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package zio.schema.codec

object JsonPlatformSpecific {
@SuppressWarnings(Array(
"scalafix:ExplicitResultTypes"
))
val platformSpecificEncoderTests = Seq.empty
@SuppressWarnings(Array(
"scalafix:ExplicitResultTypes"
))
val platformSpecificDecoderTests = Seq.empty
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package zio.schema.codec

import java.time.{ ZoneId, ZoneOffset }

import scala.collection.immutable.ListMap

import zio.Console._
import zio._
import zio.json.JsonDecoder.JsonError
import zio.json.ast.Json
import zio.json.{ DeriveJsonEncoder, JsonEncoder }
import zio.schema.CaseSet._
import zio.schema._
import zio.schema.annotation._
import zio.schema.codec.DecodeError
import zio.schema.codec.DecodeError.ReadError
import zio.schema.codec.JsonCodec.JsonEncoder.charSequenceToByteChunk
import zio.schema.codec.JsonCodecSpec.PaymentMethod.{ CreditCard, PayPal, WireTransfer }
Expand All @@ -19,7 +21,8 @@ import zio.schema.meta.MetaSchema
import zio.stream.ZStream
import zio.test.Assertion._
import zio.test.TestAspect._
import zio.test._
import zio.test.{ TestResult, _ }
import zio.{ ZIO, _ }

object JsonCodecSpec extends ZIOSpecDefault {

Expand Down Expand Up @@ -1413,7 +1416,7 @@ object JsonCodecSpec extends ZIOSpecDefault {
assertZIO(stream)(equalTo(json))
}

def assertEncodesJson[A](schema: Schema[A], value: A)(implicit enc: JsonEncoder[A]) = {
def assertEncodesJson[A](schema: Schema[A], value: A)(implicit enc: JsonEncoder[A]): ZIO[Any, Nothing, TestResult] = {
val stream = ZStream
.succeed(value)
.via(JsonCodec.schemaBasedBinaryCodec[A](schema).streamEncoder)
Expand All @@ -1440,7 +1443,7 @@ object JsonCodecSpec extends ZIOSpecDefault {
value: A,
chunk: Chunk[Byte],
cfg: JsonCodec.Config = JsonCodec.Config.default
) = {
): ZIO[Any, DecodeError, TestResult] = {
val result = ZStream.fromChunk(chunk).via(JsonCodec.schemaBasedBinaryCodec[A](cfg)(schema).streamDecoder).runCollect
assertZIO(result)(equalTo(Chunk(value)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private[codec] class MessagePackEncoder {
packer.packString(v.toString)
()
case (StandardType.CurrencyType, v: java.util.Currency) =>
packer.packString(v.getCurrencyCode())
packer.packString(v.getCurrencyCode)
()
case (_, _) =>
throw new NotImplementedError(s"No encoder for $standardType")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package zio.schema.codec

object ProtobufPlatformSpecific {
@SuppressWarnings(Array(
"scalafix:ExplicitResultTypes"
))
val platformSpecificEncodeAndDecode = Seq.empty
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import zio.test.{ Gen, assert, check }

object ProtobufPlatformSpecific {

@SuppressWarnings(Array(
"scalafix:ExplicitResultTypes"
))
val platformSpecificEncodeAndDecode = Seq(
test("currencies") {
check(Gen.currency) { value =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package zio.schema.codec

object ProtobufPlatformSpecific {
@SuppressWarnings(Array(
"scalafix:ExplicitResultTypes"
))
val platformSpecificEncodeAndDecode = Seq.empty
}
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ object ProtobufCodec {
case (StandardType.ZonedDateTimeType, v: ZonedDateTime) =>
encodePrimitive(fieldNumber, StandardType.StringType, v.format(DateTimeFormatter.ISO_ZONED_DATE_TIME))
case (StandardType.CurrencyType, v: java.util.Currency) =>
encodePrimitive(fieldNumber, StandardType.StringType, v.getCurrencyCode())
encodePrimitive(fieldNumber, StandardType.StringType, v.getCurrencyCode)
case (_, _) =>
throw new NotImplementedError(s"No encoder for $standardType")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package zio.schema.codec

import java.time._
import java.util.UUID

import scala.collection.immutable.ListMap
import scala.util.Try

import zio.Console._
import zio._
import zio.schema.CaseSet._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ object ThriftCodec {
case (StandardType.ZonedDateTimeType, v: ZonedDateTime) =>
p.writeString(v.toString)
case (StandardType.CurrencyType, v: java.util.Currency) =>
p.writeString(v.getCurrencyCode())
p.writeString(v.getCurrencyCode)
case (_, _) =>
fail(s"No encoder for $standardType")
}
Expand Down
2 changes: 1 addition & 1 deletion zio-schema/js/src/main/scala/zio/schema/StandardType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ object StandardType {
override def tag: String = Tags.CURRENCY
override def defaultValue: Either[String, java.util.Currency] = Left("Currency generation not available in ScalaJS")
override def compare(x: java.util.Currency, y: java.util.Currency): Int =
x.getCurrencyCode().compareTo(y.getCurrencyCode())
x.getCurrencyCode.compareTo(y.getCurrencyCode)
}

implicit object BigDecimalType extends StandardType[java.math.BigDecimal] {
Expand Down
13 changes: 10 additions & 3 deletions zio-schema/jvm/src/main/scala/zio/schema/StandardType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,17 @@ object StandardType {

implicit object CurrencyType extends StandardType[java.util.Currency] {
override def tag: String = Tags.CURRENCY
override def defaultValue: Either[String, java.util.Currency] =
Right(java.util.Currency.getInstance(java.util.Locale.getDefault()))
override def defaultValue: Either[String, java.util.Currency] = {
try {
Right(java.util.Currency.getInstance(java.util.Locale.getDefault()))
} catch {
case _: NullPointerException => throw new Exception("Could not get default currency. Default locale was null.")
case ex => throw new Exception(s"Could not get default currency. ${ex.getMessage}")
}
}

override def compare(x: java.util.Currency, y: java.util.Currency): Int =
x.getCurrencyCode.compareTo(y.getCurrencyCode())
x.getCurrencyCode.compareTo(y.getCurrencyCode)
}

implicit object BigDecimalType extends StandardType[java.math.BigDecimal] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ object StandardType {
Left("Currency generation not available in Scala Native")

override def compare(x: java.util.Currency, y: java.util.Currency): Int =
x.getCurrencyCode().compareTo(y.getCurrencyCode())
x.getCurrencyCode.compareTo(y.getCurrencyCode)
}

implicit object BigDecimalType extends StandardType[java.math.BigDecimal] {
Expand Down

0 comments on commit 83c8fb9

Please sign in to comment.