Skip to content

Commit

Permalink
Merge pull request #19 from chathurace/main
Browse files Browse the repository at this point in the history
Change EDI reading code to skip ignoreSegments before starting any processing work
  • Loading branch information
chathurace committed Aug 15, 2023
2 parents 73e814a + 64d6486 commit e6d2ede
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerina"
name = "edi"
version = "1.0.6"
version = "1.0.7"
authors = ["Ballerina"]
keywords = ["edi"]
repository = "https://github.com/ballerina-platform/module-ballerina-edi"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ distribution-version = "2201.7.0"
[[package]]
org = "ballerina"
name = "edi"
version = "1.0.6"
version = "1.0.7"
dependencies = [
{org = "ballerina", name = "file"},
{org = "ballerina", name = "io"},
Expand Down
15 changes: 12 additions & 3 deletions ballerina/segment_group_reader.bal
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ isolated function readSegmentGroup(EdiUnitSchema[] currentUnitSchema, EdiContext
}
string sDesc = context.ediText[context.rawIndex];
string segmentDesc = regex:replaceAll(sDesc, "\n", "");
string[] fields = check splitFields(segmentDesc, ediSchema.delimiters.'field, segSchema);
if ediSchema.ignoreSegments.indexOf(fields[0], 0) != () {

// 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;
foreach string ignoreSegment in ediSchema.ignoreSegments {
if segmentDesc.startsWith(ignoreSegment) {
ignoreCurrentSegment = true;
break;
}
}
if ignoreCurrentSegment {
context.rawIndex += 1;
continue;
}

string[] fields = check splitFields(segmentDesc, ediSchema.delimiters.'field, segSchema);
if segSchema is EdiSegSchema {
log:printDebug(string `Trying to match with segment mapping ${printSegMap(segSchema)}`);
if segSchema.code != fields[0] {
Expand Down
25 changes: 25 additions & 0 deletions ballerina/tests/edi_modification_tests.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import ballerina/test;

@test:Config {
dataProvider: ediModificationsDataProvider
}
function testSegmentModification(string testName) returns error? {
EdiSchema schema = check getTestSchema(testName);
string ediIn = check getEDIMessage(testName);
json message = check fromEdiString(ediIn, schema);
check saveJsonMessage(testName, message);

string ediOut = check toEdiString(message, schema);
string ediExpected = check getOutputEDI(testName);

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

test:assertEquals(ediOut, ediIn);
}

function ediModificationsDataProvider() returns string[][] {
return [
["ignore_segments"]
];
}
9 changes: 9 additions & 0 deletions ballerina/tests/resources/ignore_segments/message.edi
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
UNA:*? ~
IGN*12*tty:43~
HDR*ORDER_1201*ABC_Store*2008-01-01~
ITM*A-250*12~
ITM*A-45*100~
ITM*D-10*58~
ITM*K-80*250~
ITM*T-46*28~
UNZ*45*kk~
29 changes: 29 additions & 0 deletions ballerina/tests/resources/ignore_segments/message.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"header": {
"orderId": "ORDER_1201",
"organization": "ABC_Store",
"date": "2008-01-01"
},
"items": [
{
"item": "A-250",
"quantity": 12
},
{
"item": "A-45",
"quantity": 100
},
{
"item": "D-10",
"quantity": 58
},
{
"item": "K-80",
"quantity": 250
},
{
"item": "T-46",
"quantity": 28
}
]
}
6 changes: 6 additions & 0 deletions ballerina/tests/resources/ignore_segments/output.edi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
HDR*ORDER_1201*ABC_Store*2008-01-01~
ITM*A-250*12~
ITM*A-45*100~
ITM*D-10*58~
ITM*K-80*250~
ITM*T-46*28~
1 change: 1 addition & 0 deletions ballerina/tests/resources/ignore_segments/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"header":{"code":"HDR", "orderId":"ORDER_1201", "organization":"ABC_Store", "date":"2008-01-01"}, "items":[{"code":"ITM", "item":"A-250", "quantity":12}, {"code":"ITM", "item":"A-45", "quantity":100}, {"code":"ITM", "item":"D-10", "quantity":58}, {"code":"ITM", "item":"K-80", "quantity":250}, {"code":"ITM", "item":"T-46", "quantity":28}]}
18 changes: 18 additions & 0 deletions ballerina/tests/resources/ignore_segments/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "SimpleOrder",
"delimiters" : {"segment" : "~", "field" : "*", "component": ":", "repetition": "^"},
"ignoreSegments": ["UNA", "IGN", "UNZ"],
"segments" : [
{
"code": "HDR",
"tag" : "header",
"fields" : [{"tag": "code"}, {"tag" : "orderId"}, {"tag" : "organization"}, {"tag" : "date"}]
},
{
"code": "ITM",
"tag" : "items",
"maxOccurances" : -1,
"fields" : [{"tag": "code"}, {"tag" : "item"}, {"tag" : "quantity", "dataType" : "int"}]
}
]
}
5 changes: 5 additions & 0 deletions ballerina/tests/test_utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ function getEDIMessage(string testName) returns string|error {
return check io:fileReadString(inputPath);
}

function getOutputEDI(string testName) returns string|error {
string inputPath = check file:joinPath("tests", "resources", testName, "output.edi");
return check io:fileReadString(inputPath);
}

function saveEDIMessage(string testName, string message) returns error? {
string path = check file:joinPath("tests", "resources", testName, "output.edi");
check io:fileWriteString(path, message);
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
org.gradle.caching=true
group=io.ballerina.stdlib
version=1.0.6
version=1.0.7

checkstylePluginVersion=10.12.0
spotbugsPluginVersion=5.0.14
Expand Down

0 comments on commit e6d2ede

Please sign in to comment.