From 84e165e8a934f60a92ba6159ba7e9cf772aece9e Mon Sep 17 00:00:00 2001 From: Kamil Podsiadlo Date: Tue, 16 Nov 2021 15:53:11 +0100 Subject: [PATCH] Implement alias lookup & add tests --- .../yaml/internal/load/compose/Composer.scala | 6 +- .../virtuslab/yaml/decoder/DecoderSuite.scala | 103 ++++++++++++++++++ 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/yaml/shared/src/main/scala/org/virtuslab/yaml/internal/load/compose/Composer.scala b/yaml/shared/src/main/scala/org/virtuslab/yaml/internal/load/compose/Composer.scala index 62e550d08..b036eadb3 100644 --- a/yaml/shared/src/main/scala/org/virtuslab/yaml/internal/load/compose/Composer.scala +++ b/yaml/shared/src/main/scala/org/virtuslab/yaml/internal/load/compose/Composer.scala @@ -51,8 +51,10 @@ object ComposerImpl extends Composer: s.metadata.anchor.foreach(anchor => aliases.put(anchor, node)) Right(Result(node, tail)) // todo #88 - case _: EventKind.Alias => - Left(ComposerError(s"Aliases aren't currently supported")) + case EventKind.Alias(alias) => + aliases.get(alias) match + case Some(node) => Right(Result(node, tail)) + case None => Left(ComposerError(s"There is no anchor for $alias alias")) case event => Left(ComposerError(s"Expected YAML node, but found: $event")) case Nil => Left(ComposerError("No events available")) diff --git a/yaml/shared/src/test/scala/org/virtuslab/yaml/decoder/DecoderSuite.scala b/yaml/shared/src/test/scala/org/virtuslab/yaml/decoder/DecoderSuite.scala index 8a0139290..2e85cccd3 100644 --- a/yaml/shared/src/test/scala/org/virtuslab/yaml/decoder/DecoderSuite.scala +++ b/yaml/shared/src/test/scala/org/virtuslab/yaml/decoder/DecoderSuite.scala @@ -186,6 +186,109 @@ class DecoderSuite extends munit.FunSuite: assertEquals(yaml.as[Spec], Right(expectedSpec)) } + test("alias for scalar node") { + val yaml = + s"""|- &a 5 + |- *a + |""".stripMargin + + assertEquals(yaml.as[Any], Right(List(5, 5))) + } + + test("alias for sequence node") { + val yaml = + s"""|seq1: &a + | - 1 + | - 2 + |seq2: *a + |""".stripMargin + + assertEquals( + yaml.as[Any], + Right( + Map( + "seq1" -> List(1, 2), + "seq2" -> List(1, 2) + ) + ) + ) + } + + test("alias for value in sequence") { + val yaml = + s"""|- &b + | name: Mark McGwire + | hr: 65 + |- *b + |""".stripMargin + + assertEquals( + yaml.as[Any], + Right( + List( + Map("name" -> "Mark McGwire", "hr" -> 65), + Map("name" -> "Mark McGwire", "hr" -> 65) + ) + ) + ) + } + + test("alias for flow sequence node") { + val yaml = + s"""|seq1: &a [1, 2] + |seq2: *a + |""".stripMargin + + assertEquals( + yaml.as[Any], + Right( + Map( + "seq1" -> List(1, 2), + "seq2" -> List(1, 2) + ) + ) + ) + } + + test("alias for mapping node") { + val yaml = + s"""|map1: &a + | 1: 2 + | k1: v1 + |map2: *a + |""".stripMargin + + assertEquals( + yaml.as[Any], + Right( + Map( + "map1" -> Map(1 -> 2, "k1" -> "v1"), + "map2" -> Map(1 -> 2, "k1" -> "v1") + ) + ) + ) + } + + test("alias for flow mapping node") { + val yaml = + s"""|map1: &a { + | 1: 2, + | k1: v1 + |} + |map2: *a + |""".stripMargin + + assertEquals( + yaml.as[Any], + Right( + Map( + "map1" -> Map(1 -> 2, "k1" -> "v1"), + "map2" -> Map(1 -> 2, "k1" -> "v1") + ) + ) + ) + } + test("decode into Map[Any, Any]") { val yaml =