@@ -24,7 +24,7 @@ package object yaml {
2424
2525 /** Parse multiple YAML documents from the given string.
2626 */
27- def parseAllYamls (str : String ): Either [YamlError , List [Node ]] =
27+ def parseManyYamls (str : String ): Either [YamlError , List [Node ]] =
2828 for {
2929 events <- {
3030 val parser = ParserImpl (Tokenizer .make(str))
@@ -33,6 +33,46 @@ package object yaml {
3333 nodes <- ComposerImpl .multipleFromEvents(events)
3434 } yield nodes
3535
36+ /**
37+ * Parse a YAML document from the given [[String ]], returning either [[YamlError ]] or [[T ]].
38+ *
39+ * According to the specification:
40+ * - [[Parser ]] takes input string and produces sequence of events
41+ * - then [[Composer ]] produces a representation graph from events
42+ * - finally [[YamlDecoder ]] (construct phase from the YAML spec) constructs data type [[T ]] from the YAML representation.
43+ */
44+ def decodeYaml [T ](str : String )(implicit
45+ c : YamlDecoder [T ],
46+ settings : LoadSettings = LoadSettings .empty
47+ ): Either [YamlError , T ] =
48+ for {
49+ node <- parseYaml(str)
50+ t <- node.as[T ]
51+ } yield t
52+
53+ /**
54+ * Decode all YAML documents from the given [[String ]], returning either [[YamlError ]] or a list of [[T ]].
55+ * The error will be the first failure in parsing or converting to [[T ]].
56+ */
57+ def decodeManyYamls [T ](str : String )(implicit
58+ c : YamlDecoder [T ],
59+ settings : LoadSettings = LoadSettings .empty
60+ ): Either [YamlError , List [T ]] =
61+ parseManyYamls(str).flatMap { nodes =>
62+ @ tailrec
63+ def parseNodes (n : List [Node ], out : List [T ]): Either [YamlError , List [T ]] =
64+ n match {
65+ case Nil => Right (out.reverse)
66+ case head :: tail =>
67+ head.as[T ] match {
68+ case Left (error) => Left (error)
69+ case Right (value) => parseNodes(tail, value :: out)
70+ }
71+ }
72+
73+ parseNodes(nodes, Nil )
74+ }
75+
3676 implicit class StringOps (val str : String ) extends AnyVal {
3777
3878 /**
@@ -46,11 +86,7 @@ package object yaml {
4686 def as [T ](implicit
4787 c : YamlDecoder [T ],
4888 settings : LoadSettings = LoadSettings .empty
49- ): Either [YamlError , T ] =
50- for {
51- node <- parseYaml(str)
52- t <- node.as[T ]
53- } yield t
89+ ): Either [YamlError , T ] = decodeYaml[T ](str)
5490
5591 /**
5692 * Parse YAML from the given [[String ]], returning either [[YamlError ]] or a list of [[T ]].
@@ -59,21 +95,7 @@ package object yaml {
5995 def asMany [T ](implicit
6096 c : YamlDecoder [T ],
6197 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- }
98+ ): Either [YamlError , List [T ]] = decodeManyYamls[T ](str)
7799
78100 def asNode : Either [YamlError , Node ] = parseYaml(str)
79101 }
0 commit comments