@@ -6,6 +6,7 @@ import org.virtuslab.yaml.internal.load.compose.ComposerImpl
66import org .virtuslab .yaml .internal .load .parse .Parser
77import org .virtuslab .yaml .internal .load .parse .ParserImpl
88import org .virtuslab .yaml .internal .load .reader .Tokenizer
9+ import scala .annotation .tailrec
910
1011package object yaml {
1112
@@ -35,13 +36,13 @@ package object yaml {
3536 implicit class StringOps (val str : String ) extends AnyVal {
3637
3738 /**
38- * Parse YAML from the given [[String ]], returning either [[YamlError ]] or [[T ]].
39- *
40- * According to the specification:
41- * - [[Parser ]] takes input string and produces sequence of events
42- * - then [[Composer ]] produces a representation graph from events
43- * - finally [[YamlDecoder ]] (construct phase from the YAML spec) constructs data type [[T ]] from the YAML representation.
44- */
39+ * Parse YAML from the given [[String ]], returning either [[YamlError ]] or [[T ]].
40+ *
41+ * According to the specification:
42+ * - [[Parser ]] takes input string and produces sequence of events
43+ * - then [[Composer ]] produces a representation graph from events
44+ * - finally [[YamlDecoder ]] (construct phase from the YAML spec) constructs data type [[T ]] from the YAML representation.
45+ */
4546 def as [T ](implicit
4647 c : YamlDecoder [T ],
4748 settings : LoadSettings = LoadSettings .empty
@@ -51,6 +52,29 @@ package object yaml {
5152 t <- node.as[T ]
5253 } yield t
5354
55+ /**
56+ * Parse YAML from the given [[String ]], returning either [[YamlError ]] or a list of [[T ]].
57+ * The error will be the first failure in parsing or converting to [[T ]].
58+ */
59+ def asMany [T ](implicit
60+ c : YamlDecoder [T ],
61+ settings : LoadSettings = LoadSettings .empty
62+ ): Either [YamlError , List [T ]] =
63+ parseAllYamls(str).flatMap { nodes =>
64+ @ tailrec
65+ def parseNodes (n : List [Node ], out : List [T ]): Either [YamlError , List [T ]] =
66+ n match {
67+ case Nil => Right (out.reverse)
68+ case head :: tail =>
69+ head.as[T ] match {
70+ case Left (error) => Left (error)
71+ case Right (value) => parseNodes(tail, value :: out)
72+ }
73+ }
74+
75+ parseNodes(nodes, Nil )
76+ }
77+
5478 def asNode : Either [YamlError , Node ] = parseYaml(str)
5579 }
5680
0 commit comments