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

Allow customization of maxAliasesForCollections loader option #205

Merged
merged 6 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
133 changes: 133 additions & 0 deletions src/main/scala/io/circe/yaml/Parser.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package io.circe.yaml

import cats.syntax.either._
import io.circe._
import java.io.{ Reader, StringReader }
import org.yaml.snakeyaml.LoaderOptions
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.SafeConstructor
import org.yaml.snakeyaml.nodes._
import scala.collection.JavaConverters._

final case class Parser(
maxAliasesForCollections: Int = 50
) {

private val loaderOptions = {
val options = new LoaderOptions()
options.setMaxAliasesForCollections(maxAliasesForCollections)
options
}

/**
* Parse YAML from the given [[Reader]], returning either [[ParsingFailure]] or [[Json]]
* @param yaml
* @return
*/
def parse(yaml: Reader): Either[ParsingFailure, Json] = for {
parsed <- parseSingle(yaml)
json <- yamlToJson(parsed)
} yield json

def parse(yaml: String): Either[ParsingFailure, Json] = parse(new StringReader(yaml))

def parseDocuments(yaml: Reader): Stream[Either[ParsingFailure, Json]] = parseStream(yaml).map(yamlToJson)
def parseDocuments(yaml: String): Stream[Either[ParsingFailure, Json]] = parseDocuments(new StringReader(yaml))

private[this] def parseSingle(reader: Reader) =
Either.catchNonFatal(new Yaml(loaderOptions).compose(reader)).leftMap(err => ParsingFailure(err.getMessage, err))

private[this] def parseStream(reader: Reader) =
new Yaml(loaderOptions).composeAll(reader).asScala.toStream

private[this] object CustomTag {
def unapply(tag: Tag): Option[String] = if (!tag.startsWith(Tag.PREFIX))
Some(tag.getValue)
else
None
}

private[this] class FlatteningConstructor extends SafeConstructor {
def flatten(node: MappingNode): MappingNode = {
flattenMapping(node)
node
}

def construct(node: ScalarNode): Object =
getConstructor(node).construct(node)
}

private[this] def yamlToJson(node: Node): Either[ParsingFailure, Json] = {
// Isn't thread-safe internally, may hence not be shared
val flattener: FlatteningConstructor = new FlatteningConstructor

def convertScalarNode(node: ScalarNode) = Either
.catchNonFatal(node.getTag match {
case Tag.INT if node.getValue.startsWith("0x") || node.getValue.contains("_") =>
Json.fromJsonNumber(flattener.construct(node) match {
case int: Integer => JsonLong(int.toLong)
case long: java.lang.Long => JsonLong(long)
case bigint: java.math.BigInteger =>
JsonDecimal(bigint.toString)
case other => throw new NumberFormatException(s"Unexpected number type: ${other.getClass}")
})
case Tag.INT | Tag.FLOAT =>
JsonNumber.fromString(node.getValue).map(Json.fromJsonNumber).getOrElse {
throw new NumberFormatException(s"Invalid numeric string ${node.getValue}")
}
case Tag.BOOL =>
Json.fromBoolean(flattener.construct(node) match {
case b: java.lang.Boolean => b
case _ => throw new IllegalArgumentException(s"Invalid boolean string ${node.getValue}")
})
case Tag.NULL => Json.Null
case CustomTag(other) =>
Json.fromJsonObject(JsonObject.singleton(other.stripPrefix("!"), Json.fromString(node.getValue)))
case other => Json.fromString(node.getValue)
})
.leftMap { err =>
ParsingFailure(err.getMessage, err)
}

def convertKeyNode(node: Node) = node match {
case scalar: ScalarNode => Right(scalar.getValue)
case _ => Left(ParsingFailure("Only string keys can be represented in JSON", null))
}

if (node == null) {
Right(Json.False)
} else {
node match {
case mapping: MappingNode =>
flattener
.flatten(mapping)
.getValue
.asScala
.foldLeft(
Either.right[ParsingFailure, JsonObject](JsonObject.empty)
) { (objEither, tup) =>
for {
obj <- objEither
key <- convertKeyNode(tup.getKeyNode)
value <- yamlToJson(tup.getValueNode)
} yield obj.add(key, value)
}
.map(Json.fromJsonObject)
case sequence: SequenceNode =>
sequence.getValue.asScala
.foldLeft(Either.right[ParsingFailure, List[Json]](List.empty[Json])) { (arrEither, node) =>
for {
arr <- arrEither
value <- yamlToJson(node)
} yield value :: arr
}
.map(arr => Json.fromValues(arr.reverse))
case scalar: ScalarNode => convertScalarNode(scalar)
}
}
}
}

object Parser {
val defaultParser = Parser()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of nitpicky, but can we rename this to default (which follows the naming convention in e.g. circe-generic-extras) and put an explicit type annotation on it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, updated

}
113 changes: 6 additions & 107 deletions src/main/scala/io/circe/yaml/parser/package.scala
Original file line number Diff line number Diff line change
@@ -1,120 +1,19 @@
package io.circe.yaml

import cats.syntax.either._
import io.circe._
import java.io.{ Reader, StringReader }
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.SafeConstructor
import org.yaml.snakeyaml.nodes._
import scala.collection.JavaConverters._

package object parser {
import java.io.Reader

package object parser {
/**
* Parse YAML from the given [[Reader]], returning either [[ParsingFailure]] or [[Json]]
* @param yaml
* @return
*/
def parse(yaml: Reader): Either[ParsingFailure, Json] = for {
parsed <- parseSingle(yaml)
json <- yamlToJson(parsed)
} yield json

def parse(yaml: String): Either[ParsingFailure, Json] = parse(new StringReader(yaml))

def parseDocuments(yaml: Reader): Stream[Either[ParsingFailure, Json]] = parseStream(yaml).map(yamlToJson)
def parseDocuments(yaml: String): Stream[Either[ParsingFailure, Json]] = parseDocuments(new StringReader(yaml))

private[this] def parseSingle(reader: Reader) =
Either.catchNonFatal(new Yaml().compose(reader)).leftMap(err => ParsingFailure(err.getMessage, err))

private[this] def parseStream(reader: Reader) =
new Yaml().composeAll(reader).asScala.toStream

private[this] object CustomTag {
def unapply(tag: Tag): Option[String] = if (!tag.startsWith(Tag.PREFIX))
Some(tag.getValue)
else
None
}

private[this] class FlatteningConstructor extends SafeConstructor {
def flatten(node: MappingNode): MappingNode = {
flattenMapping(node)
node
}

def construct(node: ScalarNode): Object =
getConstructor(node).construct(node)
}

private[this] def yamlToJson(node: Node): Either[ParsingFailure, Json] = {
// Isn't thread-safe internally, may hence not be shared
val flattener: FlatteningConstructor = new FlatteningConstructor

def convertScalarNode(node: ScalarNode) = Either
.catchNonFatal(node.getTag match {
case Tag.INT if node.getValue.startsWith("0x") || node.getValue.contains("_") =>
Json.fromJsonNumber(flattener.construct(node) match {
case int: Integer => JsonLong(int.toLong)
case long: java.lang.Long => JsonLong(long)
case bigint: java.math.BigInteger =>
JsonDecimal(bigint.toString)
case other => throw new NumberFormatException(s"Unexpected number type: ${other.getClass}")
})
case Tag.INT | Tag.FLOAT =>
JsonNumber.fromString(node.getValue).map(Json.fromJsonNumber).getOrElse {
throw new NumberFormatException(s"Invalid numeric string ${node.getValue}")
}
case Tag.BOOL =>
Json.fromBoolean(flattener.construct(node) match {
case b: java.lang.Boolean => b
case _ => throw new IllegalArgumentException(s"Invalid boolean string ${node.getValue}")
})
case Tag.NULL => Json.Null
case CustomTag(other) =>
Json.fromJsonObject(JsonObject.singleton(other.stripPrefix("!"), Json.fromString(node.getValue)))
case other => Json.fromString(node.getValue)
})
.leftMap { err =>
ParsingFailure(err.getMessage, err)
}
def parse(yaml: Reader): Either[ParsingFailure, Json] = Parser.defaultParser.parse(yaml)

def convertKeyNode(node: Node) = node match {
case scalar: ScalarNode => Right(scalar.getValue)
case _ => Left(ParsingFailure("Only string keys can be represented in JSON", null))
}
def parse(yaml: String): Either[ParsingFailure, Json] = Parser.defaultParser.parse(yaml)

if (node == null) {
Right(Json.False)
} else {
node match {
case mapping: MappingNode =>
flattener
.flatten(mapping)
.getValue
.asScala
.foldLeft(
Either.right[ParsingFailure, JsonObject](JsonObject.empty)
) { (objEither, tup) =>
for {
obj <- objEither
key <- convertKeyNode(tup.getKeyNode)
value <- yamlToJson(tup.getValueNode)
} yield obj.add(key, value)
}
.map(Json.fromJsonObject)
case sequence: SequenceNode =>
sequence.getValue.asScala
.foldLeft(Either.right[ParsingFailure, List[Json]](List.empty[Json])) { (arrEither, node) =>
for {
arr <- arrEither
value <- yamlToJson(node)
} yield value :: arr
}
.map(arr => Json.fromValues(arr.reverse))
case scalar: ScalarNode => convertScalarNode(scalar)
}
}
}
def parseDocuments(yaml: Reader): Stream[Either[ParsingFailure, Json]] = Parser.defaultParser.parseDocuments(yaml)
def parseDocuments(yaml: String): Stream[Either[ParsingFailure, Json]] = Parser.defaultParser.parseDocuments(yaml)
}
33 changes: 33 additions & 0 deletions src/test/scala/io/circe/yaml/ParserTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,37 @@ class ParserTests extends AnyFlatSpec with Matchers with EitherValues {
.value == Json.False
)
}

it should "parse aliases" in {
assert(
Parser(maxAliasesForCollections = 2).parse(
"""
| aliases:
| - &alias1
| foo:
| bar
| baz:
| - *alias1
| - *alias1
|""".stripMargin
).isRight
)
}


it should "fail to parse too many aliases" in {
assert(
Parser(maxAliasesForCollections = 1).parse(
"""
| aliases:
| - &alias1
| foo:
| bar
| baz:
| - *alias1
| - *alias1
|""".stripMargin
).isLeft
)
}
}