Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Data parsing from string #94

Merged
merged 3 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("")

Check notice

Code scanning / Scalastyle (reported by Codacy)

Field name does not match the regular expression '^[A-Z][A-Za-z0-9]*$'.

Field name does not match the regular expression '^[A-Z][A-Za-z0-9]*$'.

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)

Check warning

Code scanning / Codacy-scalameta-pro (reported by Codacy)

Prohibits usage of duplicated string literals.

The string literal '5340595900475325933418219074917' appears 3 times in the file.

Check notice

Code scanning / Scalastyle (reported by Codacy)

Regular expression matched 'println'.

Regular expression matched 'println'.

Check notice

Code scanning / Scalastyle (reported by Codacy)

Regular expression matched '{0}' in a token.

Regular expression matched '{0}' in a token.

Check notice

Code scanning / Scalastyle (reported by Codacy)

The string literal "5340595900475325933418219074917" appears 3 times in the file.

The string literal "5340595900475325933418219074917" appears 3 times in the file.
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"
)
)
}
}