diff --git a/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala b/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala index f5cb999fc..5c28e7a2c 100644 --- a/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala +++ b/zio-schema-json/shared/src/main/scala/zio/schema/codec/JsonCodec.scala @@ -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") } diff --git a/zio-schema-msg-pack/src/main/scala/zio/schema/codec/MessagePackDecoder.scala b/zio-schema-msg-pack/src/main/scala/zio/schema/codec/MessagePackDecoder.scala index 76ee5a696..cad77533f 100644 --- a/zio-schema-msg-pack/src/main/scala/zio/schema/codec/MessagePackDecoder.scala +++ b/zio-schema-msg-pack/src/main/scala/zio/schema/codec/MessagePackDecoder.scala @@ -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]] = diff --git a/zio-schema-msg-pack/src/test/scala-2/zio/schema/codec/MessagePackCodecSpec.scala b/zio-schema-msg-pack/src/test/scala-2/zio/schema/codec/MessagePackCodecSpec.scala index b2c96bda7..b7dd45c33 100644 --- a/zio-schema-msg-pack/src/test/scala-2/zio/schema/codec/MessagePackCodecSpec.scala +++ b/zio-schema-msg-pack/src/test/scala-2/zio/schema/codec/MessagePackCodecSpec.scala @@ -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 { diff --git a/zio-schema/jvm/src/main/scala/zio/schema/StandardType.scala b/zio-schema/jvm/src/main/scala/zio/schema/StandardType.scala index 3a0f2c4ba..0f052fead 100644 --- a/zio-schema/jvm/src/main/scala/zio/schema/StandardType.scala +++ b/zio-schema/jvm/src/main/scala/zio/schema/StandardType.scala @@ -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) } diff --git a/zio-schema/native/src/main/scala/zio/schema/StandardType.scala b/zio-schema/native/src/main/scala/zio/schema/StandardType.scala index f3b844230..d1d56d9d8 100644 --- a/zio-schema/native/src/main/scala/zio/schema/StandardType.scala +++ b/zio-schema/native/src/main/scala/zio/schema/StandardType.scala @@ -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) diff --git a/zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala b/zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala index f06358f0f..6d34df523 100644 --- a/zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala +++ b/zio-schema/shared/src/main/scala/zio/schema/DynamicValue.scala @@ -284,6 +284,7 @@ object DynamicValue { .:+:(primitiveOffsetDateTimeCase) .:+:(primitiveZonedDateTimeCase) .:+:(primitiveUUIDCase) + .:+:(primitiveCurrencyCase) .:+:(singletonCase) ) @@ -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 + } + ) }