Skip to content

Commit 8891c5d

Browse files
committed
style: change to decodeManyYamls and have the string methods forward to those
1 parent a6f9979 commit 8891c5d

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

core/shared/src/main/scala/org/virtuslab/yaml/Yaml.scala

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

core/shared/src/test/scala/org/virtuslab/yaml/YamlPackageSuite.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ class YamlPackageSuite extends BaseYamlSuite {
2525
|123
2626
|---
2727
|42
28+
|---
29+
|256
2830
|""".stripMargin
2931

3032
val actual = yaml.asMany[Int]
3133
assert(actual.isRight)
3234
val ints = actual.right.get
33-
assertEquals(ints, List(123, 42))
35+
assertEquals(ints, List(123, 42, 256))
3436
}
3537
}

core/shared/src/test/scala/org/virtuslab/yaml/parser/ParserSuite.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class ParserSuite extends BaseYamlSuite {
207207
assertEquals(parseYaml(yaml).toOption.get.asYaml, yaml)
208208
}
209209

210-
test("parseAllYamls produces a node for each document") {
210+
test("parseManyYamls produces a node for each document") {
211211
val yaml =
212212
"""one: ah ha ha
213213
|---
@@ -216,7 +216,7 @@ class ParserSuite extends BaseYamlSuite {
216216
|three: ah ha ha
217217
|""".stripMargin
218218

219-
val nodes = parseAllYamls(yaml).toOption.get
219+
val nodes = parseManyYamls(yaml).toOption.get
220220

221221
val actual = nodes.map(_.asYaml).mkString("---\n")
222222

0 commit comments

Comments
 (0)