Skip to content

Commit

Permalink
Merge pull request #30 from RDPerera/main
Browse files Browse the repository at this point in the history
Upgrade to lang.regex
  • Loading branch information
niveathika committed Jan 18, 2024
2 parents bee9ad8 + d88fbcd commit 54a72bd
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 67 deletions.
24 changes: 3 additions & 21 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ version = "1.1.1"
dependencies = [
{org = "ballerina", name = "file"},
{org = "ballerina", name = "io"},
{org = "ballerina", name = "lang.regexp"},
{org = "ballerina", name = "log"},
{org = "ballerina", name = "regex"},
{org = "ballerina", name = "test"}
]
modules = [
Expand Down Expand Up @@ -70,14 +70,8 @@ version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "lang.string"
version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.regexp"}
modules = [
{org = "ballerina", packageName = "lang.regexp", moduleName = "lang.regexp"}
]

[[package]]
Expand Down Expand Up @@ -120,18 +114,6 @@ dependencies = [
{org = "ballerina", name = "jballerina.java"}
]

[[package]]
org = "ballerina"
name = "regex"
version = "1.4.3"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.string"}
]
modules = [
{org = "ballerina", packageName = "regex", moduleName = "regex"}
]

[[package]]
org = "ballerina"
name = "test"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/component_group_reader.bal
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ isolated function readComponentGroup(string compositeText, EdiSchema ediSchema,
return ();
}

string[] components = split(compositeText, ediSchema.delimiters.component);
string[] components = check split(compositeText, ediSchema.delimiters.component);
if fieldSchema.truncatable {
int minFields = getMinimumCompositeFields(fieldSchema);
if components.length() < minFields {
Expand Down
2 changes: 1 addition & 1 deletion ballerina/edi_translator.bal
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type EdiContext record {|
public isolated function fromEdiString(string ediText, EdiSchema schema) returns json|Error {
EdiContext context = {schema};
EdiUnitSchema[] currentMapping = context.schema.segments;
context.ediText = splitSegments(ediText, context.schema.delimiters.segment);
context.ediText = check splitSegments(ediText, context.schema.delimiters.segment);
EdiSegmentGroup rootGroup = check readSegmentGroup(currentMapping, context, true);
return rootGroup;
}
Expand Down
2 changes: 1 addition & 1 deletion ballerina/repetition_reader.bal
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import ballerina/log;

isolated function readRepetition(string repeatText, string repeatDelimiter, EdiSchema mapping, EdiFieldSchema fieldMapping)
returns SimpleArray|EdiComponentGroup[]|Error {
string[] fields = split(repeatText, repeatDelimiter);
string[] fields = check split(repeatText, repeatDelimiter);
SimpleArray|EdiComponentGroup[] repeatValues = getArray(fieldMapping.dataType);
if fields.length() == 0 {
// None of the repeating values are provided. Return an empty array.
Expand Down
11 changes: 4 additions & 7 deletions ballerina/segment_group_reader.bal
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// under the License.

import ballerina/log;
import ballerina/regex;

type SegmentGroupContext record {|
int schemaIndex = 0;
Expand All @@ -31,9 +30,7 @@ isolated function readSegmentGroup(EdiUnitSchema[] currentUnitSchema, EdiContext
if segSchema is () {
return error Error("Segment schema cannot be empty.");
}
string sDesc = context.ediText[context.rawIndex];
string segmentDesc = regex:replaceAll(sDesc, "\n", "");

string segmentDesc = removeLineBreaks(context.ediText[context.rawIndex]);
// There can be segments that do not follow standard EDI format (e.g. EDIFACT UNA segment).
// Therefore, it is necessary to check and skip ignore segments before spliting into fields.
boolean ignoreCurrentSegment = false;
Expand Down Expand Up @@ -81,11 +78,11 @@ isolated function readSegmentGroup(EdiUnitSchema[] currentUnitSchema, EdiContext
}
}
if rootGroup && ediSchema.delimiters.'field != "FL" {
foreach int i in context.rawIndex...(context.ediText.length() - 1) {
foreach int i in context.rawIndex ... (context.ediText.length() - 1) {
string unmatchedRaw = context.ediText[context.rawIndex];
string[] unmatchedSegFields = split(unmatchedRaw, ediSchema.delimiters.'field);
string[] unmatchedSegFields = check split(unmatchedRaw, ediSchema.delimiters.'field);
if ediSchema.ignoreSegments.indexOf(unmatchedSegFields[0], 0) == () {
return error Error(string `Segment text does not match with the schema.
return error Error(string `Segment text does not match with the schema.
Segment: ${context.ediText[context.rawIndex]}, Curren row: ${context.rawIndex}`);
}
}
Expand Down
2 changes: 1 addition & 1 deletion ballerina/subcomponent_group_reader.bal
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ isolated function readSubcomponentGroup(string scGroupText, EdiSchema schema, Ed
return ();
}

string[] subcomponents = split(scGroupText, schema.delimiters.subcomponent);
string[] subcomponents = check split(scGroupText, schema.delimiters.subcomponent);
if compSchema.truncatable {
int minFields = getMinimumSubcomponentFields(compSchema);
if subcomponents.length() < minFields {
Expand Down
4 changes: 2 additions & 2 deletions ballerina/tests/edi_modification_tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ function testSegmentModification(string testName) returns error? {
string ediOut = check toEdiString(message, schema);
string ediExpected = check getOutputEDI(testName);

ediOut = prepareEDI(ediOut, schema);
ediIn = prepareEDI(ediExpected, schema);
ediOut = check prepareEDI(ediOut, schema);
ediIn = check prepareEDI(ediExpected, schema);

test:assertEquals(ediOut, ediIn);
}
Expand Down
12 changes: 6 additions & 6 deletions ballerina/tests/segment_reading_tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ function testSegments(string testName) returns error? {
string ediOut = check toEdiString(message, schema);
check saveEDIMessage(testName, ediOut);

ediOut = prepareEDI(ediOut, schema);
ediIn = prepareEDI(ediIn, schema);
ediOut = check prepareEDI(ediOut, schema);
ediIn = check prepareEDI(ediIn, schema);

test:assertEquals(ediOut, ediIn);
}
Expand All @@ -32,8 +32,8 @@ function testFixedLengthEDIs(string testName) returns error? {
string ediOut = check toEdiString(message, schema);
check saveEDIMessage(testName, ediOut);

ediOut = prepareEDI(ediOut, schema);
ediIn = prepareEDI(ediIn, schema);
ediOut = check prepareEDI(ediOut, schema);
ediIn = check prepareEDI(ediIn, schema);

test:assertEquals(ediOut, ediIn);
}
Expand All @@ -51,8 +51,8 @@ function testDynamicLengthEDIs(string testName) returns error? {
string ediOut = check toEdiString(message, schema);
check saveEDIMessage(testName, ediOut);

ediOut = prepareEDI(ediOut, schema);
ediIn = prepareEDI(ediIn, schema);
ediOut = check prepareEDI(ediOut, schema);
ediIn = check prepareEDI(ediIn, schema);

test:assertEquals(ediOut, ediIn);
}
Expand Down
14 changes: 7 additions & 7 deletions ballerina/tests/test_utils.bal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ballerina/io;
import ballerina/regex;
import ballerina/file;
import ballerina/lang.regexp;

function getTestSchema(string testName) returns EdiSchema|error {
string schemaPath = check file:joinPath("tests", "resources", testName, "schema.json");
Expand Down Expand Up @@ -37,11 +37,11 @@ function saveJsonMessage(string testName, json message) returns error? {
check io:fileWriteJson(path, message);
}

function prepareEDI(string edi, EdiSchema schema) returns string {
string e1 = regex:replaceAll(edi, " ", "");
e1 = regex:replaceAll(e1, "\n", "");
e1 = regex:replaceAll(e1, validateDelimiter((schema.delimiters.decimalSeparator ?: ".")) + "0", "");
e1 = regex:replaceAll(e1, "0", "");
return e1;
function prepareEDI(string edi, EdiSchema schema) returns string|error {
string:RegExp decSeprator = check regexp:fromString(schema.delimiters.decimalSeparator ?: ".");
string:RegExp nulSpaces = re ` |\n|0`;
string preparedEDI = nulSpaces.replaceAll(edi, "");
preparedEDI = decSeprator.replaceAll(preparedEDI, "");
return preparedEDI;
}

58 changes: 40 additions & 18 deletions ballerina/utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@
// specific language governing permissions and limitations
// under the License.

import ballerina/regex;
import ballerina/lang.regexp;

isolated function convertToType(string value, EdiDataType dataType, string? decimalSeparator) returns SimpleType|error {
string v = value.trim();
match dataType {
STRING => {
return v;
}
INT => {
return int:fromString(decimalSeparator != () ? regex:replace(v, decimalSeparator, ".") : v);
}
FLOAT => {
return float:fromString(decimalSeparator != () ? regex:replace(v, decimalSeparator, ".") : v);
INT|FLOAT => {
if decimalSeparator != () {
string:RegExp decimalSep = check regexp:fromString(decimalSeparator);
v = decimalSep.replace(v, ".");
}
match dataType {
INT => {
return int:fromString(v);
}
FLOAT => {
return float:fromString(v);
}
}
}
}
return error("Undefined type for value:" + value);
Expand Down Expand Up @@ -96,31 +104,40 @@ isolated function splitFields(string segmentText, string fieldDelimiter, EdiUnit
return error Error(string `Start index and field length is not provided for fixed length schema field. Segment: ${segSchema.code}, Field: ${fieldSchema.tag}`);
}
int startIndex = fieldSchema.startIndex - 1;
int endIndex = startIndex + feildLength;
int endIndex = startIndex + feildLength;
if startIndex >= segmentText.length() {
break;
}
endIndex = segmentText.length() < endIndex? segmentText.length() : endIndex;
endIndex = segmentText.length() < endIndex ? segmentText.length() : endIndex;
string fieldText = segmentText.substring(startIndex, endIndex);
fields.push(fieldText);
fields.push(fieldText);
}
return fields;
} else {
return split(segmentText, fieldDelimiter);
}
}

isolated function split(string text, string delimiter) returns string[] {
isolated function split(string text, string delimiter) returns string[]|Error {
string preparedText = prepareToSplit(text, delimiter);
string validatedDelimiter = validateDelimiter(delimiter);
return regex:split(preparedText, validatedDelimiter);
string:RegExp|error validatedDelimiter = regexp:fromString(validateDelimiter(delimiter));
if validatedDelimiter is error {
return error Error("Invalid delimiter: " + delimiter);
}
return validatedDelimiter.split(preparedText);
}

isolated function splitSegments(string text, string delimiter) returns string[] {
string validatedDelimiter = validateDelimiter(delimiter);
string[] segmentLines = regex:split(text, validatedDelimiter);
isolated function splitSegments(string text, string delimiter) returns string[]|Error {
string:RegExp|error validatedDelimiter = regexp:fromString(validateDelimiter(delimiter));
if validatedDelimiter is error {
return error Error("Invalid delimiter: " + delimiter);
}
string[] segmentLines = validatedDelimiter.split(text);
if segmentLines[segmentLines.length() - 1] == "" {
string _ = segmentLines.remove(segmentLines.length() - 1);
}
foreach int i in 0 ... (segmentLines.length() - 1) {
segmentLines[i] = regex:replaceAll(segmentLines[i], "\n", "");
segmentLines[i] = removeLineBreaks(segmentLines[i]);
}
return segmentLines;
}
Expand Down Expand Up @@ -227,7 +244,8 @@ isolated function serializeSimpleType(SimpleType v, EdiSchema schema, int fixedL
if sv.endsWith(".0") {
sv = sv.substring(0, sv.length() - 2);
} else if schema.delimiters.decimalSeparator != "." {
sv = regex:replace(sv, "\\.", schema.delimiters.decimalSeparator ?: ".");
string:RegExp separator = re `\\.`;
sv = separator.replace(sv, schema.delimiters.decimalSeparator ?: ".");
}
}
return fixedLength > 0 ? addPadding(sv, fixedLength) : sv;
Expand All @@ -236,9 +254,13 @@ isolated function serializeSimpleType(SimpleType v, EdiSchema schema, int fixedL
isolated function addPadding(string value, int requiredLength) returns string {
string paddedValue = value;
int lengthDiff = requiredLength - value.length();
foreach int i in 1...lengthDiff {
foreach int i in 1 ... lengthDiff {
paddedValue += " ";
}
return paddedValue;
}

isolated function removeLineBreaks(string value) returns string {
string:RegExp newline = re `\n`;
return newline.replaceAll(value, "");
}
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ subprojects {
ballerinaStdLibs "io.ballerina.stdlib:io-ballerina:${project.stdlibIoVersion}"
ballerinaStdLibs "io.ballerina.stdlib:log-ballerina:${project.stdlibLogVersion}"
ballerinaStdLibs "io.ballerina.stdlib:file-ballerina:${project.stdlibFileVersion}"
ballerinaStdLibs "io.ballerina.stdlib:regex-ballerina:${project.stdlibRegexVersion}"

ballerinaStdLibs "io.ballerina.stdlib:os-ballerina:${project.stdlibOsVersion}"
ballerinaStdLibs "io.ballerina.stdlib:time-ballerina:${project.stdlibTimeVersion}"
Expand Down
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- [Add support for field length constraints (min/max)](https://github.com/ballerina-platform/ballerina-library/issues/5896).
- [Add support for field length constraints (min/max)](https://github.com/ballerina-platform/ballerina-library/issues/5896).
- [Updated dependencies to use lang.regex instead of ballerina/regex](https://github.com/ballerina-platform/ballerina-library/issues/5941)

0 comments on commit 54a72bd

Please sign in to comment.