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

Support merge keys #32

Merged
merged 1 commit into from
Jun 9, 2017
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
14 changes: 13 additions & 1 deletion src/main/scala/io/circe/yaml/parser/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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._

Expand Down Expand Up @@ -38,6 +39,15 @@ package object parser {
None
}

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

private[this] val flattener: FlatteningConstructor = new FlatteningConstructor
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are you sure this is threadsafe? SnakeYAML's docs explicitly absolve them of thread safety. It could be that this particular bit of SnakeYAML is threadsafe, but maybe it's best to create a new FlatteningConstructor in the call to parse and pass it through all the yamlToJsons just to be sure? It sure seems like it ought to be safe since you're only using it for flatten, but Constructor does seem to have all kinds of non-threadsafe mutable state, and it wouldn't be trivial to ensure it's impossible for it to get touched by this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, better to be safe than sorry I guess. I'll make this change later.


private[this] def yamlToJson(node: Node): Either[ParsingFailure, Json] = {

def convertScalarNode(node: ScalarNode) = Either.catchNonFatal(node.getTag match {
Expand All @@ -61,7 +71,9 @@ package object parser {

node match {
case mapping: MappingNode =>
mapping.getValue.asScala.foldLeft(Either.right[ParsingFailure, JsonObject](JsonObject.empty)) {
flattener.flatten(mapping).getValue.asScala.foldLeft(
Either.right[ParsingFailure, JsonObject](JsonObject.empty)
) {
(objEither, tup) => for {
obj <- objEither
key <- convertKeyNode(tup.getKeyNode)
Expand Down
40 changes: 40 additions & 0 deletions src/test/resources/test-yamls/merge-key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[
{
"x" : 1,
"y" : 2
},
{
"x" : 0,
"y" : 2
},
{
"r" : 1e1
},
{
"r" : 1
},
{
"x" : 1,
"y" : 2,
"r" : 1e1,
"label" : "center/big"
},
{
"x" : 1,
"y" : 2,
"r" : 1e1,
"label" : "center/big"
},
{
"x" : 1,
"y" : 2,
"r" : 1e1,
"label" : "center/big"
},
{
"r" : 1e1,
"x" : 1,
"y" : 2,
"label" : "center/big"
}
]
27 changes: 27 additions & 0 deletions src/test/resources/test-yamls/merge-key.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
- &CENTER { x: 1, y: 2 }
- &LEFT { x: 0, y: 2 }
- &BIG { r: 10 }
- &SMALL { r: 1 }

# All the following maps are equal:

- # Explicit keys
x: 1
y: 2
r: 10
label: center/big

- # Merge one map
<< : *CENTER
r: 10
label: center/big

- # Merge multiple maps
<< : [ *CENTER, *BIG ]
label: center/big

- # Override
<< : [ *BIG, *LEFT, *SMALL ]
x: 1
label: center/big