From b72e049985a659e1392c183bf878d2cd447c7f06 Mon Sep 17 00:00:00 2001 From: David Geirola Date: Sat, 3 Jun 2023 15:15:10 +0200 Subject: [PATCH] WIP --- core/src/main/scala/cats/xml/Xml.scala | 8 ++--- core/src/main/scala/cats/xml/xmlData.scala | 35 ++----------------- .../test/scala/cats/xml/XmlNumberSuite.scala | 0 .../test/scala/cats/xml/XmlParserSuite.scala | 13 ++++--- .../xml/testing/VeryLongNumericString.scala | 2 +- 5 files changed, 16 insertions(+), 42 deletions(-) delete mode 100644 core/src/test/scala/cats/xml/XmlNumberSuite.scala diff --git a/core/src/main/scala/cats/xml/Xml.scala b/core/src/main/scala/cats/xml/Xml.scala index 1b160fa..e68b17f 100644 --- a/core/src/main/scala/cats/xml/Xml.scala +++ b/core/src/main/scala/cats/xml/Xml.scala @@ -64,13 +64,13 @@ object Xml { def ofString(value: String): XmlString = XmlString(value) def ofChar(value: Char): XmlChar = XmlChar(value) def ofBoolean(value: Boolean): XmlBool = XmlBool(value) - def ofByte(value: Byte): XmlNumber = XmlByte(value) - def ofShort(value: Short): XmlNumber = XmlShort(value) - def ofInt(value: Int): XmlNumber = XmlInt(value) + def ofByte(value: Byte): XmlNumber = XmlLong(value.toLong) + def ofShort(value: Short): XmlNumber = XmlLong(value.toLong) + def ofInt(value: Int): XmlNumber = XmlLong(value.toLong) def ofLong(value: Long): XmlNumber = XmlLong(value) def ofFloat(value: Float): XmlNumber = XmlFloat(value) def ofDouble(value: Double): XmlNumber = XmlDouble(value) - def ofBigInt(value: BigInt): XmlNumber = XmlBigInt(value) + def ofBigInt(value: BigInt): XmlNumber = XmlBigDecimal(BigDecimal(value)) def ofBigDecimal(value: BigDecimal): XmlNumber = XmlBigDecimal(value) def ofArray[T <: XmlData](value: Array[T]): XmlArray[T] = XmlArray(value) def ofSeq[T <: XmlData: ClassTag](value: Seq[T]): XmlArray[T] = XmlArray(value.toArray) diff --git a/core/src/main/scala/cats/xml/xmlData.scala b/core/src/main/scala/cats/xml/xmlData.scala index 36ba08a..bb12290 100644 --- a/core/src/main/scala/cats/xml/xmlData.scala +++ b/core/src/main/scala/cats/xml/xmlData.scala @@ -39,21 +39,14 @@ object XmlData { sealed trait XmlNumber extends XmlData with Serializable { final def toBigDecimal: Option[BigDecimal] = this match { - case XmlByte(value) => Some(BigDecimal(value.toInt)) - case XmlShort(value) => Some(BigDecimal(value.toInt)) - case XmlInt(value) => Some(BigDecimal(value)) case XmlLong(value) => Some(BigDecimal(value)) case XmlFloat(value) => Some(new JavaBigDecimal(java.lang.Float.toString(value))) case XmlDouble(value) => Some(JavaBigDecimal.valueOf(value)) - case XmlBigInt(value) => Some(BigDecimal(value)) case XmlBigDecimal(value) => Some(value) } final def toBigInt: Option[BigInt] = this match { - case XmlByte(value) => Some(BigInt(value.toInt)) - case XmlShort(value) => Some(BigInt(value.toInt)) - case XmlInt(value) => Some(BigInt(value)) - case XmlLong(value) => Some(BigInt(value)) + case XmlLong(value) => Some(BigInt(value)) case XmlFloat(value) => Option(new JavaBigDecimal(java.lang.Float.toString(value))) .filter(XmlNumber.bigDecimalIsWhole) @@ -62,29 +55,20 @@ object XmlData { Option(JavaBigDecimal.valueOf(value)) .filter(XmlNumber.bigDecimalIsWhole) .map(bd => new BigInt(bd.toBigInteger)) - case XmlBigInt(value) => Some(value) case XmlBigDecimal(value) => value.toBigIntExact } final def toDouble: Double = this match { - case XmlByte(value) => value.toDouble - case XmlShort(value) => value.toDouble - case XmlInt(value) => value.toDouble case XmlLong(value) => value.toDouble case XmlFloat(value) => new JavaBigDecimal(java.lang.Float.toString(value)).doubleValue case XmlDouble(value) => value - case XmlBigInt(value) => value.doubleValue case XmlBigDecimal(value) => value.doubleValue } final def toFloat: Float = this match { - case XmlByte(value) => value.toFloat - case XmlShort(value) => value.toFloat - case XmlInt(value) => value.toFloat case XmlLong(value) => value.toFloat case XmlFloat(value) => value case XmlDouble(value) => value.toFloat - case XmlBigInt(value) => value.floatValue case XmlBigDecimal(value) => value.floatValue } @@ -110,10 +94,7 @@ object XmlData { } final def toLong: Option[Long] = this match { - case XmlByte(value) => Some(value.toLong) - case XmlShort(value) => Some(value.toLong) - case XmlInt(value) => Some(value.toLong) - case XmlLong(value) => Some(value) + case XmlLong(value) => Some(value) case XmlFloat(value) => Option(new JavaBigDecimal(java.lang.Float.toString(value))) .filter(XmlNumber.bigDecimalIsValidLong) @@ -122,7 +103,6 @@ object XmlData { Option(JavaBigDecimal.valueOf(value)) .filter(XmlNumber.bigDecimalIsValidLong) .map(_.longValue) - case XmlBigInt(value) => Try(value.toLong).toOption case XmlBigDecimal(value) => Try(value.toLongExact).toOption } } @@ -140,13 +120,9 @@ object XmlData { ) <= 0 } - private[xml] final case class XmlByte(value: Byte) extends XmlNumber - private[xml] final case class XmlShort(value: Short) extends XmlNumber - private[xml] final case class XmlInt(value: Int) extends XmlNumber private[xml] final case class XmlLong(value: Long) extends XmlNumber private[xml] final case class XmlFloat(value: Float) extends XmlNumber private[xml] final case class XmlDouble(value: Double) extends XmlNumber - private[xml] final case class XmlBigInt(value: BigInt) extends XmlNumber private[xml] final case class XmlBigDecimal(value: BigDecimal) extends XmlNumber // ------------------------------------// @@ -156,21 +132,14 @@ object XmlData { case XmlChar(value) => value.toString case XmlBool(value) => value.toString case XmlArray(value) => value.mkString(",") - case XmlByte(value) => value.toString - case XmlShort(value) => value.toString - case XmlInt(value) => value.toString case XmlLong(value) => value.toString case XmlFloat(value) => value.toString case XmlDouble(value) => value.toString - case XmlBigInt(value) => value.toString case XmlBigDecimal(value) => value.toString } implicit val order: Order[XmlNumber] = { Order.from { - case (XmlByte(x), XmlByte(y)) => x.compareTo(y) - case (XmlShort(x), XmlShort(y)) => x.compareTo(y) - case (XmlInt(x), XmlInt(y)) => x.compareTo(y) case (XmlLong(x), XmlLong(y)) => x.compareTo(y) case (XmlDouble(x), XmlDouble(y)) => java.lang.Double.compare(x, y) case (XmlFloat(x), XmlFloat(y)) => java.lang.Float.compare(x, y) diff --git a/core/src/test/scala/cats/xml/XmlNumberSuite.scala b/core/src/test/scala/cats/xml/XmlNumberSuite.scala deleted file mode 100644 index e69de29..0000000 diff --git a/core/src/test/scala/cats/xml/XmlParserSuite.scala b/core/src/test/scala/cats/xml/XmlParserSuite.scala index f981c87..905b3bf 100644 --- a/core/src/test/scala/cats/xml/XmlParserSuite.scala +++ b/core/src/test/scala/cats/xml/XmlParserSuite.scala @@ -1,11 +1,17 @@ package cats.xml +import cats.xml.utils.Debug + import scala.util.{Success, Try} class XmlParserSuite extends munit.FunSuite { import cats.xml.implicits.* + Debug.enable( + xmlPrinterPrintTypesName = true + ) + test("XmlParser.parseString") { assertEquals( obtained = XmlParser[Try].parseString( @@ -102,15 +108,14 @@ class XmlParserSuite extends munit.FunSuite { ) } - test("XmlParser.parseString with long number that should be parsed as String") { - val xml: XmlNode = - xml"""""" + test("XmlParser.parseString with long number that should be parsed as BigDecimal") { + val xml: XmlNode = xml"""""" assertEquals( obtained = xml, expected = XmlNode("Data") .withAttributes( - "value" := "5340595900475325933418219074917" + "value" := BigDecimal("5340595900475325933418219074917123456789123456789") ) ) } diff --git a/core/src/test/scala/cats/xml/testing/VeryLongNumericString.scala b/core/src/test/scala/cats/xml/testing/VeryLongNumericString.scala index 559ee99..66932fc 100644 --- a/core/src/test/scala/cats/xml/testing/VeryLongNumericString.scala +++ b/core/src/test/scala/cats/xml/testing/VeryLongNumericString.scala @@ -10,6 +10,6 @@ object VeryLongNumericString { implicit val arb: Arbitrary[VeryLongNumericString] = Arbitrary( - Gen.numStr.suchThat(_.length > 31).map(VeryLongNumericString(_)) + Gen.numStr.suchThat(_.length >= 31).map(VeryLongNumericString(_)) ) }