Skip to content

Commit

Permalink
Merge pull request #94 from geirolz/Fix_data_encoder
Browse files Browse the repository at this point in the history
Fix Data parsing from string
  • Loading branch information
geirolz authored May 5, 2023
2 parents cfa06b1 + 085fd99 commit 57fe9bc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 33 deletions.
32 changes: 14 additions & 18 deletions core/src/main/scala/cats/xml/Xml.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ object Xml {
import cats.syntax.all.*

final lazy val Null: Xml & XmlNode & XmlData = XmlNull
final lazy val True: XmlData = ofBoolean(true)
final lazy val False: XmlData = ofBoolean(false)
final lazy val True: XmlBool = ofBoolean(true)
final lazy val False: XmlBool = ofBoolean(false)
final lazy val emptyString: XmlString = ofString("")

def ofString(value: String): XmlString = XmlString(value)
def ofChar(value: Char): XmlChar = XmlChar(value)
Expand Down Expand Up @@ -86,22 +87,17 @@ object Xml {
}

def fromDataString(value: String): XmlData = {
val strData = Xml.ofString(value)
Decoder
.oneOf(
Decoder.decodeBoolean.map(Xml.ofBoolean),
Decoder.decodeByte.map(Xml.ofByte),
Decoder.decodeShort.map(Xml.ofShort),
Decoder.decodeInt.map(Xml.ofInt),
Decoder.decodeLong.map(Xml.ofLong),
Decoder.decodeFloat.map(Xml.ofFloat),
Decoder.decodeDouble.map(Xml.ofDouble),
Decoder.decodeBigInt.map(Xml.ofBigInt),
Decoder.decodeBigDecimal.map(Xml.ofBigDecimal),
Decoder.decodeChar.map(Xml.ofChar)
)
.decode(strData)
.getOrElse(strData)
fromNumberString(value)
.getOrElse {
val strData = Xml.ofString(value)
Decoder
.oneOf(
Decoder.decodeBoolean.map(Xml.ofBoolean),
Decoder.decodeChar.map(Xml.ofChar)
)
.decode(strData)
.getOrElse(strData)
}
}

/*
Expand Down
27 changes: 13 additions & 14 deletions core/src/main/scala/cats/xml/codec/Encoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,19 @@ private[xml] trait EncoderPrimitivesInstances {
implicit def dataEncodeOption[F[X] <: Option[X], T: DataEncoder]: DataEncoder[F[T]] =
DataEncoder.of[F[T]](_.fold[XmlData](Xml.Null)(DataEncoder[T].encode(_)))

implicit val encodeXmlData: DataEncoder[XmlData] = DataEncoder.of(identity)
implicit val encodeUnit: DataEncoder[Unit] = DataEncoder.of(_ => Xml.Null)
implicit val encodeString: DataEncoder[String] = DataEncoder.of(Xml.ofString(_))
implicit val encodeChar: DataEncoder[Char] = DataEncoder.of(Xml.ofChar)
implicit val encodeBoolean: DataEncoder[Boolean] = DataEncoder.of(Xml.ofBoolean)
implicit val encodeByte: DataEncoder[Byte] = DataEncoder.of(Xml.ofByte)
implicit val encodeShort: DataEncoder[Short] = DataEncoder.of(Xml.ofShort)
implicit val encodeInt: DataEncoder[Int] = DataEncoder.of(Xml.ofInt)
implicit val encodeLong: DataEncoder[Long] = DataEncoder.of(Xml.ofLong)
implicit val encodeFloat: DataEncoder[Float] = DataEncoder.of(Xml.ofFloat)
implicit val encodeDouble: DataEncoder[Double] = DataEncoder.of(Xml.ofDouble)
implicit val encodeBigDecimal: DataEncoder[BigDecimal] = DataEncoder.of(Xml.ofBigDecimal)
implicit val encodeBigInt: DataEncoder[BigInt] = DataEncoder.of(Xml.ofBigInt)

implicit def encodeXmlData[D <: XmlData]: DataEncoder[D] = DataEncoder.of(identity)
implicit val encodeUnit: DataEncoder[Unit] = DataEncoder.of(_ => Xml.Null)
implicit val encodeString: DataEncoder[String] = DataEncoder.of(Xml.ofString(_))
implicit val encodeChar: DataEncoder[Char] = DataEncoder.of(Xml.ofChar)
implicit val encodeBoolean: DataEncoder[Boolean] = DataEncoder.of(Xml.ofBoolean)
implicit val encodeByte: DataEncoder[Byte] = DataEncoder.of(Xml.ofByte)
implicit val encodeShort: DataEncoder[Short] = DataEncoder.of(Xml.ofShort)
implicit val encodeInt: DataEncoder[Int] = DataEncoder.of(Xml.ofInt)
implicit val encodeLong: DataEncoder[Long] = DataEncoder.of(Xml.ofLong)
implicit val encodeFloat: DataEncoder[Float] = DataEncoder.of(Xml.ofFloat)
implicit val encodeDouble: DataEncoder[Double] = DataEncoder.of(Xml.ofDouble)
implicit val encodeBigDecimal: DataEncoder[BigDecimal] = DataEncoder.of(Xml.ofBigDecimal)
implicit val encodeBigInt: DataEncoder[BigInt] = DataEncoder.of(Xml.ofBigInt)
}

// #################### DATA ENCODER ####################
Expand Down
12 changes: 11 additions & 1 deletion core/src/test/scala/cats/xml/XmlNumberSuite.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
package cats.xml

//TODO
class XmlNumberSuite extends munit.ScalaCheckSuite {}
class XmlNumberSuite extends munit.ScalaCheckSuite {

test("") {

Console.println(Xml.fromDataString("5340595900475325933418219074917").getClass)
assertEquals(
obtained = Xml.fromDataString("5340595900475325933418219074917"),
expected = Xml.ofString("5340595900475325933418219074917")
)
}
}
13 changes: 13 additions & 0 deletions core/src/test/scala/cats/xml/XmlParserSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,17 @@ class XmlParserSuite extends munit.FunSuite {
)
)
}

test("XmlParser.parseString with long number that should be parsed as String") {
val xml: XmlNode =
xml"""<Data value="5340595900475325933418219074917"/>"""

assertEquals(
obtained = xml,
expected = XmlNode("Data")
.withAttributes(
"value" := "5340595900475325933418219074917"
)
)
}
}

0 comments on commit 57fe9bc

Please sign in to comment.