Skip to content

Commit

Permalink
Fix Option parsing (VirtusLab#128)
Browse files Browse the repository at this point in the history
* Parse Option instance
  • Loading branch information
kpodsiad authored and UnconventionalMindset committed Jan 18, 2023
1 parent f28c8d8 commit 155f2a3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,9 @@ object YamlDecoder:
}
}

given [T](using c: YamlDecoder[T]): YamlDecoder[Option[T]] = YamlDecoder {
case ScalarNode(value, tag) =>
value match
case _ if tag == Tag.nullTag => Right(None)
case _ =>
c.construct(ScalarNode(value)).map(Option(_))
given [T](using c: YamlDecoder[T]): YamlDecoder[Option[T]] = YamlDecoder { node =>
if node.tag == Tag.nullTag then Right(None)
else c.construct(node).map(Option(_))
}

private def constructFromNodes[T](nodes: Seq[Node])(using
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,15 @@ class DecoderSuite extends munit.FunSuite:

assertEquals(yaml.as[Any], Right(expected))
}

test("option") {
case class Foo(int: Int, string: String) derives YamlCodec

val foo =
"""|- int: 1
| string: "1"
|- !!null
|""".stripMargin.as[List[Option[Foo]]]

assertEquals(foo, Right(List(Some(Foo(1, "1")), None)))
}

0 comments on commit 155f2a3

Please # to comment.