Skip to content

Commit

Permalink
Implement alias lookup & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Podsiadlo committed Nov 16, 2021
1 parent 2d3d285 commit 84e165e
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 84e165e

Please sign in to comment.