Skip to content

Commit f45898f

Browse files
giacomocavalierinicolasfara
authored andcommitted
fix: fix json parsing of IgnoredSelectors
1 parent c9a3cb2 commit f45898f

File tree

3 files changed

+91
-11
lines changed

3 files changed

+91
-11
lines changed

src/main/scala/dev/atedeg/Configuration.scala

+9-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final case class IgnoredEnumCase(caseName: String) extends IgnoredSelector
1717

1818
object IgnoredSelector {
1919

20-
def fromKind(kind: String, name: String): Option[IgnoredSelector] = kind match {
20+
def fromKind(name: String, kind: String): Option[IgnoredSelector] = kind match {
2121
case "class" => Some(IgnoredClass(name))
2222
case "trait" => Some(IgnoredTrait(name))
2323
case "type" => Some(IgnoredType(name))
@@ -70,17 +70,18 @@ object AllEntities {
7070
def read(workingDir: File): Either[Error, Set[IgnoredSelector]] =
7171
Utils.parseFileWith(allEntitiesFile(workingDir))(parse)
7272

73-
private[atedeg] def parse(raw: String): Either[Error, Set[IgnoredSelector]] =
74-
parseJsonString(raw).leftMap(CirceParsingFailure).flatMap(parseJson)
73+
private[atedeg] def parse(raw: String): Either[Error, Set[IgnoredSelector]] = {
74+
val sanitizedRaw = raw.replaceFirst("pages = ", "").replaceFirst(";", "")
75+
parseJsonString(sanitizedRaw).leftMap(CirceParsingFailure).flatMap(parseJson)
76+
}
7577

7678
private def parseJson(json: Json): Either[Error, Set[IgnoredSelector]] = {
7779
implicit val decodeIgnoredSelector: Decoder[Option[IgnoredSelector]] =
7880
Decoder.forProduct2("n", "k")(IgnoredSelector.fromKind)
79-
80-
for {
81-
pagesField <- json.findAllByKey("pages").headOption.toRight[Error](ParseError(File(entitiesFileName), "pages"))
82-
res <- pagesField.as[Set[Option[IgnoredSelector]]].leftMap(CirceDecodingFailure)
83-
} yield res.collect { case Some(s) => s }
81+
json
82+
.as[Set[Option[IgnoredSelector]]]
83+
.map(_.collect { case Some(s) => s })
84+
.leftMap(CirceDecodingFailure)
8485
}
8586
}
8687

src/test/scala/dev/atedeg/ConfigParserTests.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import org.scalatest.matchers.should.Matchers
66
@SuppressWarnings(Array("org.wartremover.warts.NonUnitStatements"))
77
class ConfigParserTests extends AnyFlatSpec with Matchers {
88

9-
"parse" should " fail with an empty string" in {
9+
"parse" should "fail with an empty string" in {
1010
Configuration.parse("") should matchPattern { case Left(_) => }
1111
}
1212

13-
it should " fail with a malformed string" in {
13+
it should "fail with a malformed string" in {
1414
val malformedString =
1515
"""
1616
|ignored: []
@@ -21,7 +21,7 @@ class ConfigParserTests extends AnyFlatSpec with Matchers {
2121
Configuration.parse(malformedString) should matchPattern { case Left(_) => }
2222
}
2323

24-
it should " parse a correct string" in {
24+
it should "parse a correct string" in {
2525
val correctString =
2626
"""
2727
|ignored:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package dev.atedeg
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
import org.scalatest.matchers.should.Matchers
5+
6+
@SuppressWarnings(Array("org.wartremover.warts.NonUnitStatements"))
7+
class EntitiesParserTest extends AnyFlatSpec with Matchers {
8+
9+
"parse" should "fail with an empty string" in {
10+
AllEntities.parse("") should matchPattern { case Left(_) => }
11+
}
12+
13+
it should "fail with a malformed string" in {
14+
val malformedString = "pages: [error]"
15+
AllEntities.parse(malformedString) should matchPattern { case Left(_) => }
16+
}
17+
18+
it should "parse a correct string" in {
19+
val correctString =
20+
"""
21+
|pages = [{
22+
| "l": "",
23+
| "e": false,
24+
| "i": "",
25+
| "n": "Type",
26+
| "t": "",
27+
| "d": "",
28+
| "k": "type"
29+
| },
30+
|{
31+
| "l": "",
32+
| "e": false,
33+
| "i": "",
34+
| "n": "Enum",
35+
| "t": "",
36+
| "d": "",
37+
| "k": "enum"
38+
| },
39+
| {
40+
| "l": "",
41+
| "e": false,
42+
| "i": "",
43+
| "n": "Case",
44+
| "t": "",
45+
| "d": "",
46+
| "k": "case"
47+
| },
48+
| {
49+
| "l": "",
50+
| "e": false,
51+
| "i": "",
52+
| "n": "Trait",
53+
| "t": "",
54+
| "d": "",
55+
| "k": "trait"
56+
| },
57+
| {
58+
| "l": "",
59+
| "e": false,
60+
| "i": "",
61+
| "n": "Class",
62+
| "t": "",
63+
| "d": "",
64+
| "k": "class"
65+
| },
66+
| {
67+
| "n": "ignore me",
68+
| "k": "package"
69+
| }];""".stripMargin
70+
val expected: Set[IgnoredSelector] = Set(
71+
IgnoredClass("Class"),
72+
IgnoredEnum("Enum"),
73+
IgnoredTrait("Trait"),
74+
IgnoredEnumCase("Case"),
75+
IgnoredType("Type"),
76+
)
77+
AllEntities.parse(correctString) shouldBe Right(expected)
78+
}
79+
}

0 commit comments

Comments
 (0)