-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#683 Add support for '_' for hierarchical key generation at leaf level.
- Loading branch information
Showing
6 changed files
with
247 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...rser/src/main/scala/za/co/absa/cobrix/cobol/reader/parameters/ParameterParsingUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2018 ABSA Group Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package za.co.absa.cobrix.cobol.reader.parameters | ||
|
||
object ParameterParsingUtils { | ||
/** Splits segment ids defined in spark-cobol options for hierarchical id generation. */ | ||
def splitSegmentIds(segmentIdsToSplit: scala.collection.Seq[String]): Array[Array[String]] = { | ||
segmentIdsToSplit.toArray | ||
.map{ ids => | ||
ids.split(',') | ||
.map(_.trim()) | ||
.map(id => if (id == "*") "_" else id) | ||
} | ||
} | ||
|
||
/** Validates segment ids for hierarchical record id generation. */ | ||
def validateSegmentIds(segmentIds: Array[Array[String]]): Unit = { | ||
val maxLevel = segmentIds.length - 1 | ||
segmentIds.zipWithIndex.foreach { | ||
case (ids, level) => | ||
if (ids.contains("_") && level < maxLevel) | ||
throw new IllegalArgumentException(s"The '_' as a segment id can only be used on the leaf level (segment_id_level$maxLevel), found at 'segment_id_level$level'") | ||
if (ids.contains("*") && level < maxLevel) | ||
throw new IllegalArgumentException(s"The '*' as a segment id can only be used on the leaf level (segment_id_level$maxLevel), found at 'segment_id_level$level'") | ||
if ((ids.contains("*") || ids.contains("_")) && ids.length > 1) | ||
throw new IllegalArgumentException(s"The '*' or '_' as a segment id cannot be used with other ids 'segment_id_level$level = ${ids.mkString(",")}' is incorrect.") | ||
} | ||
|
||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
...src/test/scala/za/co/absa/cobrix/cobol/reader/parameters/ParameterParsingUtilsSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2018 ABSA Group Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package za.co.absa.cobrix.cobol.reader.parameters | ||
|
||
import org.scalatest.wordspec.AnyWordSpec | ||
|
||
class ParameterParsingUtilsSuite extends AnyWordSpec { | ||
"splitSegmentIds()" should { | ||
"split input segment ids" in { | ||
val segmentIds = Seq("A,B,C", "D,E,F") | ||
|
||
val actual = ParameterParsingUtils.splitSegmentIds(segmentIds) | ||
|
||
assert(actual.length == 2) | ||
assert(actual(0).sameElements(Array("A", "B", "C"))) | ||
assert(actual(1).sameElements(Array("D", "E", "F"))) | ||
} | ||
|
||
"trim if split with spaces" in { | ||
val segmentIds = Seq("A, B, C", "D, E, F") | ||
|
||
val actual = ParameterParsingUtils.splitSegmentIds(segmentIds) | ||
|
||
assert(actual.length == 2) | ||
assert(actual(0).sameElements(Array("A", "B", "C"))) | ||
assert(actual(1).sameElements(Array("D", "E", "F"))) | ||
} | ||
|
||
"handle empty strings" in { | ||
val segmentIds = Seq("", "") | ||
|
||
val actual = ParameterParsingUtils.splitSegmentIds(segmentIds) | ||
|
||
assert(actual.length == 2) | ||
assert(actual(0).head == "") | ||
assert(actual(1).head == "") | ||
} | ||
|
||
} | ||
|
||
|
||
"validateSegmentIds()" should { | ||
"validate segment ids" in { | ||
val segmentIds = Array( | ||
Array("A", "B", "C"), | ||
Array("D", "E", "F") | ||
) | ||
|
||
ParameterParsingUtils.validateSegmentIds(segmentIds) | ||
} | ||
|
||
"throw an exception if '_' is used on the wrong level" in { | ||
val segmentIds = Array( | ||
Array("_"), | ||
Array("A", "B", "C") | ||
) | ||
|
||
val ex = intercept[IllegalArgumentException] { | ||
ParameterParsingUtils.validateSegmentIds(segmentIds) | ||
} | ||
|
||
assert(ex.getMessage.contains("The '_' as a segment id can only be used on the leaf level (segment_id_level1), found at 'segment_id_level0'")) | ||
} | ||
|
||
"throw an exception if '*' is used on the wrong level" in { | ||
val segmentIds = Array( | ||
Array("A"), | ||
Array("B"), | ||
Array("*"), | ||
Array("C") | ||
) | ||
|
||
val ex = intercept[IllegalArgumentException] { | ||
ParameterParsingUtils.validateSegmentIds(segmentIds) | ||
} | ||
|
||
assert(ex.getMessage.contains("The '*' as a segment id can only be used on the leaf level (segment_id_level3), found at 'segment_id_level2'")) | ||
} | ||
|
||
"throw an exception if '*' or '_' is used with other ids" in { | ||
val segmentIds = Array( | ||
Array("A", "B", "C"), | ||
Array("D", "*", "F", "G") | ||
) | ||
|
||
val ex = intercept[IllegalArgumentException] { | ||
ParameterParsingUtils.validateSegmentIds(segmentIds) | ||
} | ||
|
||
assert(ex.getMessage.contains("'*' or '_' as a segment id cannot be used with other ids")) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters