Skip to content

Commit

Permalink
Merge pull request #334 from VirtusLab/issue-331
Browse files Browse the repository at this point in the history
issue 331 - preserve order of keys when parsing and printing
  • Loading branch information
lbialy authored Jul 30, 2024
2 parents ce2818c + c657482 commit 204b578
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions core/shared/src/main/scala/org/virtuslab/yaml/Node.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ sealed trait Node {
}

object Node {

final case class ScalarNode private[yaml] (value: String, tag: Tag, pos: Option[Range] = None)
extends Node

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.virtuslab.yaml.internal.load.compose

import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.immutable.ListMap

import org.virtuslab.yaml.ComposerError
import org.virtuslab.yaml.Node
Expand Down Expand Up @@ -127,7 +128,7 @@ object ComposerImpl extends Composer {
}

parseMappings(events, Nil).map { case (Result(nodes, rest), pos) =>
val mapping = Node.MappingNode(nodes.toMap, Tag.map, pos)
val mapping = Node.MappingNode(ListMap.from(nodes), Tag.map, pos)
anchorOpt.foreach(anchor => aliases.put(anchor, mapping))
Result(
mapping,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,8 @@ class DecoderSuite extends munit.FunSuite:
yaml.as[Float] match
case Left(e: ConstructError) =>
assertEquals(e.expected, Some("Float"))
case Left(e) =>
fail(s"Should fail, but got $e", e)
case Right(value) =>
fail(s"Should fail, but got $value")
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,19 @@ class ParserSuite extends BaseYamlSuite {

assertEquals(yaml.events, Right(expectedEvents))
}

test("parsing keeps order of keys") {
val yaml = """
|P:
| a: 0
| b: 1
| c: 2
| d: 3
| e: 4
|""".stripMargin

val node = yaml.asNode.toOption.get

assertEquals(node.asYaml.trim, yaml.trim)
}
}

0 comments on commit 204b578

Please sign in to comment.