Skip to content

Commit

Permalink
Revert "Add parser option to check if field starts at first index"
Browse files Browse the repository at this point in the history
This reverts commit fea4405.
  • Loading branch information
marianptelus committed Mar 3, 2022
1 parent fea4405 commit 7cdbad5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,12 @@ class RecordParser(
default: () -> R = {
throw FileParserException(name?.let { "The field $name is mandatory" } ?: "Field is mandatory")
},
mustStartAtFirstIndex: Boolean = false,
noinline parser: ((String) -> R)? = null
): R {
if (mustStartAtFirstIndex && line[from] == ' ') {
throw FileParserException(name?.let { "The field $name must start at index $from" } ?: "Field must start at index $from")
} else {
val str = readField(from, toExclusive)
return if (str.isNotBlank()) {
parser?.let { parser(str) } ?: defaultTypeParser(str)
} else default.invoke()
}
val str = readField(from, toExclusive)
return if (str.isNotBlank()) {
parser?.let { parser(str) } ?: defaultTypeParser(str)
} else default.invoke()
}

fun readField(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FileParserBasicDslTest {
StringReader(
"""
aaa 23 2021-03-09
bbb c 23 2021-04-19
bbb cc 23 2021-04-19
d e f1 34 2021-05-22
""".trimIndent()
)
Expand All @@ -43,18 +43,14 @@ class FileParserBasicDslTest {
)
}
}
Assertions.assertEquals(0, parser.getRecords()[0].issues.size)
Assertions.assertEquals(0, parser.getRecords()[1].issues.size)
Assertions.assertEquals(0, parser.getRecords()[2].issues.size)

val header = parser.getRecords()[0].value as Header
Assertions.assertEquals("aaa", header.string1)
Assertions.assertEquals(23, header.int1)
Assertions.assertEquals(LocalDate.parse("2021-03-09"), header.date1)

var item = parser.getRecords()[1].value as Item
Assertions.assertEquals("bbb", item.string1)
Assertions.assertEquals("c", item.string2)
Assertions.assertEquals("cc", item.string2)
Assertions.assertEquals(23, item.int1)
Assertions.assertEquals(LocalDate.parse("2021-04-19"), item.date1)

Expand Down Expand Up @@ -122,59 +118,10 @@ class FileParserBasicDslTest {
)
}
}
Assertions.assertEquals(1, parser.getRecords()[0].issues.size)
Assertions.assertEquals(1, parser.getRecords()[1].issues.size)
Assertions.assertEquals(1, parser.getRecords()[2].issues.size)

Assertions.assertEquals("Line length should be 18 but was 17", parser.getRecords()[0].issues[0].message)
Assertions.assertEquals("Line length should be 22 but was 20", parser.getRecords()[1].issues[0].message)
Assertions.assertEquals("Line length should be 22 but was 20", parser.getRecords()[2].issues[0].message)
}

@Test
fun `field does not start at first index`() {
val parser = parser(
BufferedReader(
StringReader(
"""
aaa 23 2021-03-09
bbb c 23 2021-04-19
d e f1 34 2021-05-22
""".trimIndent()
)
)
) {
header(17) {
Header(
string1 = optionalField(0, 4, "field1"),
int1 = mandatoryField(4, 7, "field2"),
date1 = optionalField(7, 17, "field3")
)
}
line {
Item(
string1 = optionalField(0, 4, "field1"),
string2 = mandatoryField(4, 7, "field2", mustStartAtFirstIndex = true),
int1 = optionalField(7, 10, "field3"),
date1 = optionalField(10, 20, "field4")
)
}
}
Assertions.assertEquals(0, parser.getRecords()[0].issues.size)
Assertions.assertEquals(1, parser.getRecords()[1].issues.size)
Assertions.assertEquals(0, parser.getRecords()[2].issues.size)
}

Assertions.assertEquals("The field field2 must start at index 4", parser.getRecords()[1].issues[0].message)

val header = parser.getRecords()[0].value as Header
Assertions.assertEquals("aaa", header.string1)
Assertions.assertEquals(23, header.int1)
Assertions.assertEquals(LocalDate.parse("2021-03-09"), header.date1)

val secondItem = parser.getRecords()[2].value as Item
Assertions.assertEquals("d e", secondItem.string1)
Assertions.assertEquals("f1", secondItem.string2)
Assertions.assertEquals(34, secondItem.int1)
Assertions.assertEquals(LocalDate.parse("2021-05-22"), secondItem.date1)
}
}

0 comments on commit 7cdbad5

Please sign in to comment.