Skip to content

Commit

Permalink
Issue #220 - do not convert quoted strings into other primitive types (
Browse files Browse the repository at this point in the history
…#221)

* issue220 - do not convert quoted strings into other primitive types

* more tests

* formatting

* add more unit tests

* code cleanup

* code cleanup

* more unit tests

* move tests to ScalarSuite

* minimize changes

---------

Co-authored-by: sergei.winitzki <[email protected]>
  • Loading branch information
winitzki and sergei.winitzki authored May 2, 2023
1 parent d80f945 commit 1f68475
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
7 changes: 6 additions & 1 deletion core/shared/src/main/scala/org/virtuslab/yaml/Tag.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.virtuslab.yaml

import org.virtuslab.yaml.internal.load.reader.token.ScalarStyle
import org.virtuslab.yaml.internal.load.reader.token.ScalarStyle.{DoubleQuoted, SingleQuoted}

import scala.reflect.ClassTag

sealed trait Tag {
Expand Down Expand Up @@ -35,9 +38,11 @@ object Tag {
private val minusInfinity = "-(\\.inf|\\.Inf|\\.INF)".r
private val plusInfinity = "\\+?(\\.inf|\\.Inf|\\.INF)".r

def resolveTag(value: String): Tag = {
def resolveTag(value: String, style: Option[ScalarStyle] = None): Tag = {
val assumeString = style.exists(s => s == DoubleQuoted || s == SingleQuoted)
value match {
case null => nullTag
case _ if assumeString => str
case nullPattern(_*) => nullTag
case booleanPattern(_*) => boolean
case int10Pattern(_*) => int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object ComposerImpl extends Composer {
case EventKind.MappingStart(NodeEventMetadata(anchor, _)) =>
composeMappingNode(tail, anchor, aliases)
case s: EventKind.Scalar =>
val tag: Tag = s.metadata.tag.getOrElse(Tag.resolveTag(s.value))
val tag: Tag = s.metadata.tag.getOrElse(Tag.resolveTag(s.value, Some(s.style)))
val node = Node.ScalarNode(s.value, tag, head.pos)
s.metadata.anchor.foreach(anchor => aliases.put(anchor, node))
Right(Result(node, tail))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,54 @@ class ScalarSpec extends BaseYamlSuite {
)
assertEquals(yaml.events, Right(expectedEvents))
}
test("quoted integer and bool values") {
val yaml =
"""
| - "123"
| - "bool"
| - 'bool'
| - "0xFFFF"
| - 123
| - bool
| - 0xFFFF
| """.stripMargin
val expectedEvents = List(
StreamStart,
DocumentStart(),
SequenceStart(),
Scalar("123", ScalarStyle.DoubleQuoted),
Scalar("bool", ScalarStyle.DoubleQuoted),
Scalar("bool", ScalarStyle.SingleQuoted),
Scalar("0xFFFF", ScalarStyle.DoubleQuoted),
Scalar("123", ScalarStyle.Plain),
Scalar("bool", ScalarStyle.Plain),
Scalar("0xFFFF", ScalarStyle.Plain),
SequenceEnd,
DocumentEnd(),
StreamEnd
)
assertEquals(yaml.events, Right(expectedEvents))
}

test("quoted values are read as String type") {
import org.virtuslab.yaml.{StringOps SO, _}
val isStringType: Seq[Boolean] = Seq(
""" "123" """,
""" "0xFFFF" """,
""" "true" """,
""" "null" """,
""" "123.456" """,
""" "-.inf" """,
""" '123' """,
""" '0xFFFF' """,
""" 'true' """,
""" 'null' """,
""" '123.456' """,
""" '-.inf' """
).map { yaml =>
val obj = SO(yaml).as[Any].toOption.get
obj.isInstanceOf[java.lang.String]
}
assertEquals(isStringType, Seq.fill(isStringType.length)(true))
}
}

0 comments on commit 1f68475

Please sign in to comment.