Skip to content

Commit

Permalink
fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrapyre committed May 4, 2024
1 parent fc75b5f commit 9253c10
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ object JsonCodec {
cases.map(case_ => case_.id -> case_.schema.asInstanceOf[Schema.CaseClass0[Z]].defaultConstruct()).toMap
ZJsonDecoder.string.mapOrFail(
s =>
caseMap.get(caseNameAliases.get(s).getOrElse(s)) match {
caseMap.get(caseNameAliases.getOrElse(s, s)) match {
case Some(z) => Right(z)
case None => Left("unrecognized string")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,17 @@ private[codec] class MessagePackDecoder(bytes: Chunk[Byte]) {
decodeString(path).map(OffsetDateTime.parse(_))
case StandardType.ZonedDateTimeType =>
decodeString(path).map(ZonedDateTime.parse(_))
case StandardType.CurrencyType => decodeString(path).map(java.util.Currency.getInstance(_))
case _ => fail(path, s"Unsupported primitive type $standardType")
case StandardType.CurrencyType =>
decodeString(path).flatMap { rawCurrencyCode =>
try {
Right {
java.util.Currency.getInstance(rawCurrencyCode)
}
} catch {
case NonFatal(err) => fail(path, s"Invalid currency code: ${err.getMessage}")
}
}
case _ => fail(path, s"Unsupported primitive type $standardType")
}

private def decodeOptional[A](path: Path, schema: Schema.Optional[A]): Result[Option[A]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import scala.collection.immutable.ListMap
import scala.util.Try
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.msgpack.core.{MessagePack, MessagePacker}
import org.msgpack.core.{ MessagePack, MessagePacker }
import org.msgpack.jackson.dataformat.MessagePackFactory
import zio.schema.CaseSet.caseOf
import zio.schema.Patch.Currency
import zio.schema._
import zio.stream.{ZSink, ZStream}
import zio.stream.{ ZSink, ZStream }
import zio.test.Assertion._
import zio.test._
import zio.{Chunk, Console, Scope, Task, ZIO}
import zio.{ Chunk, Console, Scope, Task, ZIO }

object MessagePackCodecSpec extends ZIOSpecDefault {

Expand Down
3 changes: 0 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 @@ -166,12 +166,9 @@ 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 compare(x: java.util.Currency, y: java.util.Currency): Int =
x.getCurrencyCode.compareTo(y.getCurrencyCode)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ object StandardType {

override def tag: String = Tags.CURRENCY

override def defaultValue: Either[String, java.util.Currency] = Left("Currency generation not available in Scala Native")
override def defaultValue: Either[String, java.util.Currency] =
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)
Expand Down
13 changes: 13 additions & 0 deletions zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ object DynamicValue {
.:+:(primitiveOffsetDateTimeCase)
.:+:(primitiveZonedDateTimeCase)
.:+:(primitiveUUIDCase)
.:+:(primitiveCurrencyCase)
.:+:(singletonCase)
)

Expand Down Expand Up @@ -922,4 +923,16 @@ object DynamicValue {
}
)

private val primitiveCurrencyCase: Schema.Case[DynamicValue, DynamicValue.Primitive[java.util.Currency]] =
Schema.Case(
"Currency",
Schema.primitive[java.util.Currency].transform(currency => DynamicValue.Primitive(currency, StandardType[java.util.Currency]), _.value), {
case dv @ DynamicValue.Primitive(_: java.util.Currency, _) => dv.asInstanceOf[DynamicValue.Primitive[java.util.Currency]]
case _ => throw new IllegalArgumentException
},
(dv: DynamicValue.Primitive[java.util.Currency]) => dv.asInstanceOf[DynamicValue], {
case DynamicValue.Primitive(_: java.util.Currency, _) => true
case _ => false
}
)
}

0 comments on commit 9253c10

Please sign in to comment.