Skip to content

Commit

Permalink
#324 Make parseSimple() signature backwards compatible.
Browse files Browse the repository at this point in the history
  • Loading branch information
yruslan committed Feb 3, 2022
1 parent 6d5a4cc commit 298e2f4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class Copybook(val ast: CopybookAST) extends Serializable {
new Copybook(CopybookParser.calculateBinaryProperties(newRoot))
}

def dropFillers(dropValueFillers: Boolean, dropGroupFillers: Boolean): Copybook = {
def dropFillers(dropGroupFillers: Boolean, dropValueFillers: Boolean): Copybook = {
def dropFillersAst(group: Group): Option[Group] = {
if (dropGroupFillers && group.isFiller) {
None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,34 +54,51 @@ object CopybookParser {

case class RecordBoundary(name: String, begin: Int, end: Int)


/**
* Tokenizes a Cobol Copybook contents and returns the AST.
*
* This method accepts arguments that affect only structure of the output AST.
*
* FILLER fields renaming/removal scenarios can be seen in `ParseCopybookFeaturesSpec.scala`
* @param copyBookContents A string containing all lines of a copybook
* @param dropGroupFillers Drop GROUPs marked as fillers from the output AST
* (the name of this parameter is retained for compatibility, fields won't be actually removed from
* the AST unless dropFillersFromAst is set to true).
*
* When dropGroupFillers is set to true, FILLER fields will retain their names,
* and 'isFiller() = true' for FILLER GROUPs.
*
* When dropGroupFillers is set to false, FILLER fields will be renamed to 'FILLER_1, FILLER_2, ...'
* to retain uniqueness of names in the output schema.
*
* @param dropValueFillers Drop primitive fields marked as fillers from the output AST
* (the name of this parameter is retained for compatibility, fields won't be actually removed from
* the AST unless dropFillersFromAst is set to true).
*
* When dropValueFillers is set to true, FILLER fields will retain their names,
* and 'isFiller() = true' for FILLER primitive fields.
*
* When dropValueFillers is set to false, FILLER fields will be renamed to 'FILLER_P1, FILLER_P2, ...'
* to retain uniqueness of names in the output schema.
*
* @param copyBookContents A string containing all lines of a copybook
* @param giveUniqueNameToGroupFillers If true each FILLER GROUP field will have a unique name that helps retaining it in
* target schemas where column names must be unique. FILLERs become FILLER_1, FILLER_2, etc.
* @param giveUniqueNameToValueFillers If true each FILLER value field will have a unique name that helps retaining it in
* target schemas where column names must be unique. FILLERs become FILLER_P1, FILLER_P2, etc.
* @param dropFillersFromAst If true FILLER fields that are not renamed will be removed from the AST.
* @param commentPolicy Specifies a policy for comments truncation inside a copybook
* @param commentPolicy Specifies a policy for comments truncation inside a copybook
* @param dropFillersFromAst If true, fillers are going to be dropped from AST according to dropGroupFillers and dropValueFillers.
* If false, fillers will remain in the AST, but still can be recognizable by 'isFiller()' method.
* @return Seq[Group] where a group is a record inside the copybook
*/
def parseSimple(copyBookContents: String,
giveUniqueNameToGroupFillers: Boolean = true,
giveUniqueNameToValueFillers: Boolean = false,
dropFillersFromAst: Boolean = false,
commentPolicy: CommentPolicy = CommentPolicy()): Copybook = {
dropGroupFillers: Boolean = false,
dropValueFillers: Boolean = true,
commentPolicy: CommentPolicy = CommentPolicy(),
dropFillersFromAst: Boolean = false
): Copybook = {
val copybook = parse(copyBookContents = copyBookContents,
dropGroupFillers = !giveUniqueNameToGroupFillers,
dropValueFillers = !giveUniqueNameToValueFillers,
dropGroupFillers = dropGroupFillers,
dropValueFillers = dropValueFillers,
commentPolicy = commentPolicy)

if (dropFillersFromAst && (!giveUniqueNameToGroupFillers || !giveUniqueNameToValueFillers)) {
copybook.dropFillers(!giveUniqueNameToValueFillers, !giveUniqueNameToGroupFillers)
if (dropFillersFromAst && (dropGroupFillers || dropValueFillers)) {
copybook.dropFillers(dropGroupFillers, dropValueFillers)
} else {
copybook
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class ParseCopybookFeaturesSpec extends FunSuite with SimpleComparisonBase {
}

test("Test parseSimple() not dropping fillers") {
val copybook = CopybookParser.parseSimple(copybookFillers, giveUniqueNameToValueFillers = true, giveUniqueNameToGroupFillers = true)
val copybook = CopybookParser.parseSimple(copybookFillers, dropGroupFillers = false, dropValueFillers = false, dropFillersFromAst = false)
val layout = copybook.generateRecordLayoutPositions()

val expectedLayout =
Expand Down Expand Up @@ -148,7 +148,7 @@ class ParseCopybookFeaturesSpec extends FunSuite with SimpleComparisonBase {
assertEqualsMultiline(layout, expectedLayout)
}
test("Test parseSimple() drop value fillers") {
val copybook = CopybookParser.parseSimple(copybookFillers, giveUniqueNameToValueFillers = false, giveUniqueNameToGroupFillers = true, dropFillersFromAst = true)
val copybook = CopybookParser.parseSimple(copybookFillers, dropGroupFillers = false, dropValueFillers = true, dropFillersFromAst = true)
val layout = copybook.generateRecordLayoutPositions()

val expectedLayout =
Expand All @@ -173,7 +173,7 @@ class ParseCopybookFeaturesSpec extends FunSuite with SimpleComparisonBase {
}

test("Test parseSimple() drop group fillers") {
val copybook = CopybookParser.parseSimple(copybookFillers, giveUniqueNameToValueFillers = true, giveUniqueNameToGroupFillers = false, dropFillersFromAst = true)
val copybook = CopybookParser.parseSimple(copybookFillers, dropGroupFillers = true, dropValueFillers = false, dropFillersFromAst = true)
val layout = copybook.generateRecordLayoutPositions()

val expectedLayout =
Expand All @@ -199,7 +199,7 @@ class ParseCopybookFeaturesSpec extends FunSuite with SimpleComparisonBase {
}

test("Test parseSimple() drop all fillers") {
val copybook = CopybookParser.parseSimple(copybookFillers, giveUniqueNameToValueFillers = false, giveUniqueNameToGroupFillers = false, dropFillersFromAst = true)
val copybook = CopybookParser.parseSimple(copybookFillers, dropGroupFillers = true, dropValueFillers = true, dropFillersFromAst = true)
val layout = copybook.generateRecordLayoutPositions()

val expectedLayout =
Expand Down

0 comments on commit 298e2f4

Please sign in to comment.