Skip to content

Commit

Permalink
Fix data from string
Browse files Browse the repository at this point in the history
  • Loading branch information
geirolz committed May 5, 2023
1 parent 53e4d01 commit 085fd99
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
27 changes: 11 additions & 16 deletions core/src/main/scala/cats/xml/Xml.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,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
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 085fd99

Please sign in to comment.