Skip to content

Commit

Permalink
changing null to (). some variable name changes from mapping to schema
Browse files Browse the repository at this point in the history
  • Loading branch information
chathurace committed Apr 27, 2023
1 parent 3ebd5f1 commit c99d448
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 95 deletions.
43 changes: 20 additions & 23 deletions edi-module/component_group_reader.bal
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,49 @@ class ComponentGroupReader {

SubcomponentGroupReader subcomponentGroupReader = new();

function read(string compositeText, EDISchema mapping, EDIFieldSchema emap)
function read(string compositeText, EDISchema ediSchema, EDIFieldSchema fieldSchema)
returns EDIComponentGroup|error? {
if compositeText.trim().length() == 0 {
// Composite value is not provided. Return null, which will cause this field to be not included.
return null;
return ();
}

string[] components = split(compositeText, mapping.delimiters.component);
if emap.truncatable {
int minFields = getMinimumCompositeFields(emap);
string[] components = split(compositeText, ediSchema.delimiters.component);
if fieldSchema.truncatable {
int minFields = getMinimumCompositeFields(fieldSchema);
if components.length() < minFields {
return error(string `Composite mapping's field count does not match minimum field count of the truncatable field ${emap.tag}.
return error(string `Composite mapping's field count does not match minimum field count of the truncatable field ${fieldSchema.tag}.
Required minimum field count: ${minFields}. Found ${components.length()} fields.
Composite mapping: ${emap.toJsonString()} | Composite text: ${compositeText}`);
Composite mapping: ${fieldSchema.toJsonString()} | Composite text: ${compositeText}`);
}
} else if (emap.components.length() != components.length()) {
string errMsg = string `Composite mapping's component count does not match field ${emap.tag}.
Composite mapping: ${emap.toJsonString()} | Composite text: ${compositeText}`;
return error(errMsg);
} else if fieldSchema.components.length() != components.length() {
return error(string `Composite mapping's component count does not match field ${fieldSchema.tag}.
Composite mapping: ${fieldSchema.toJsonString()} | Composite text: ${compositeText}`);
}

EDIComponentSchema[] subMappings = emap.components;
EDIComponentSchema[] subMappings = fieldSchema.components;
EDIComponentGroup composite = {};
int componentNumber = 0;
while (componentNumber < components.length()) {
while componentNumber < components.length() {
string component = components[componentNumber];
EDIComponentSchema subMapping = subMappings[componentNumber];
if component.trim().length() == 0 {
if subMapping.required {
return error(string `Required component ${subMapping.tag} is not provided.`);
} else {
if mapping.preserveEmptyFields {
composite[subMapping.tag] = subMapping.dataType == STRING? component : null;
}
componentNumber += 1;
continue;
}
if ediSchema.preserveEmptyFields {
composite[subMapping.tag] = subMapping.dataType == STRING? component : ();
}
componentNumber += 1;
continue;
}

if subMapping.subcomponents.length() > 0 {
EDISubcomponentGroup? scGroup = check self.subcomponentGroupReader.read(component, mapping, subMapping);
if scGroup is EDISubcomponentGroup || mapping.preserveEmptyFields {
EDISubcomponentGroup? scGroup = check self.subcomponentGroupReader.read(component, ediSchema, subMapping);
if scGroup is EDISubcomponentGroup || ediSchema.preserveEmptyFields {
composite[subMapping.tag] = scGroup;
}
} else {
SimpleType|error value = convertToType(component, subMapping.dataType, mapping.delimiters.decimalSeparator);
SimpleType|error value = convertToType(component, subMapping.dataType, ediSchema.delimiters.decimalSeparator);
if value is SimpleType? {
composite[subMapping.tag] = value;
} else {
Expand Down
2 changes: 1 addition & 1 deletion edi-module/component_writer.bal
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ComponentWriter {
}

function serializeComponentGroup(json componentGroup, EDISegSchema segMap, EDIFieldSchema fmap) returns string|error {
if componentGroup is null {
if componentGroup is () {
if fmap.required {
return error(string `Mandatory composite field "${fmap.tag}" of segment "${segMap.tag}" is not provided.`);
} else {
Expand Down
128 changes: 64 additions & 64 deletions edi-module/segment_group_reader.bal
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ballerina/log;
import ballerina/regex;

type SegmentGroupContext record {|
int mappingIndex = 0;
int schemaIndex = 0;
EDISegmentGroup segmentGroup = {};
EDIUnitSchema[] unitSchemas;
|};
Expand All @@ -11,46 +11,46 @@ class SegmentGroupReader {

SegmentReader segmentReader = new();

function read(EDIUnitSchema[] currentMapping, EDIContext context, boolean rootGroup) returns EDISegmentGroup|error {
SegmentGroupContext sgContext = {unitSchemas: currentMapping};
function read(EDIUnitSchema[] currentUnitSchema, EDIContext context, boolean rootGroup) returns EDISegmentGroup|error {
SegmentGroupContext sgContext = {unitSchemas: currentUnitSchema};
EDISchema ediSchema = context.schema;
while context.rawIndex < context.rawSegments.length() {
string sDesc = context.rawSegments[context.rawIndex];
string segmentDesc = regex:replaceAll(sDesc, "\n", "");
string[] fields = split(segmentDesc, ediSchema.delimiters.'field);
if ediSchema.ignoreSegments.indexOf(fields[0], 0) != null {
if ediSchema.ignoreSegments.indexOf(fields[0], 0) != () {
context.rawIndex += 1;
continue;
}

boolean segmentMapped = false;
while sgContext.mappingIndex < sgContext.unitSchemas.length() {
EDIUnitSchema? segMapping = currentMapping[sgContext.mappingIndex];
if (segMapping is EDISegSchema) {
log:printDebug(string `Trying to match with segment mapping ${printSegMap(segMapping)}`);
if segMapping.code != fields[0] {
check self.ignoreMapping(segMapping, sgContext, context);
while sgContext.schemaIndex < sgContext.unitSchemas.length() {
EDIUnitSchema? segSchema = currentUnitSchema[sgContext.schemaIndex];
if (segSchema is EDISegSchema) {
log:printDebug(string `Trying to match with segment mapping ${printSegMap(segSchema)}`);
if segSchema.code != fields[0] {
check self.ignoreSchema(segSchema, sgContext, context);
continue;
}
EDISegment ediRecord = check self.segmentReader.read(segMapping, fields, ediSchema, segmentDesc);
check self.placeEDISegment(ediRecord, segMapping, sgContext, context);
EDISegment ediRecord = check self.segmentReader.read(segSchema, fields, ediSchema, segmentDesc);
check self.placeEDISegment(ediRecord, segSchema, sgContext, context);
context.rawIndex += 1;
segmentMapped = true;
break;

} else if segMapping is EDISegGroupSchema {
log:printDebug(string `Trying to match with segment group mapping ${printSegGroupMap(segMapping)}`);
EDIUnitSchema firstSegMapping = segMapping.segments[0];
if firstSegMapping is EDISegGroupSchema {
return error("First item of segment group must be a segment. Found a segment group.\nSegment group: " + printSegGroupMap(segMapping));
} else if segSchema is EDISegGroupSchema {
log:printDebug(string `Trying to match with segment group mapping ${printSegGroupMap(segSchema)}`);
EDIUnitSchema firstSegSchema = segSchema.segments[0];
if firstSegSchema is EDISegGroupSchema {
return error("First item of segment group must be a segment. Found a segment group.\nSegment group: " + printSegGroupMap(segSchema));
}
if firstSegMapping.code != fields[0] {
check self.ignoreMapping(segMapping, sgContext, context);
if firstSegSchema.code != fields[0] {
check self.ignoreSchema(segSchema, sgContext, context);
continue;
}
EDISegmentGroup segmentGroup = check self.read(segMapping.segments, context, false);
EDISegmentGroup segmentGroup = check self.read(segSchema.segments, context, false);
if segmentGroup.length() > 0 {
check self.placeEDISegmentGroup(segmentGroup, segMapping, sgContext, context);
check self.placeEDISegmentGroup(segmentGroup, segSchema, sgContext, context);
}
segmentMapped = true;
break;
Expand All @@ -61,114 +61,114 @@ class SegmentGroupReader {
Curren row: ${context.rawIndex}`);
}

if sgContext.mappingIndex >= sgContext.unitSchemas.length() {
if sgContext.schemaIndex >= sgContext.unitSchemas.length() {
// We have completed mapping with this segment group.
break;
}
}
check self.validateRemainingMappings(sgContext);
check self.validateRemainingSchemas(sgContext);
return sgContext.segmentGroup;
}

# Ignores the given mapping if any of the below two conditions are satisfied. This function will be called
# if a mapping cannot be mapped with the next available segment text.
# Ignores the given segment of segment group schema if any of the below two conditions are satisfied.
# This function will be called if a schema cannot be mapped with the next available segment text.
#
# 1. Given mapping is optional
# 2. Given mapping is a repeatable one and it has already occured at least once
# 1. Given schema is optional
# 2. Given schema is a repeatable one and it has already occured at least once
#
# If above conditions are not met, mapping cannot be ignored, and should result in an error.
# If above conditions are not met, schema cannot be ignored, and should result in an error.
#
# + segMapping - Segment mapping or segment group mapping to be ignored
# + segSchema - Segment schema or segment group schema to be ignored
# + sgContext - Segment group parsing context
# + context - EDI parsing context
# + return - Return error if the given mapping cannot be ignored.
function ignoreMapping(EDIUnitSchema segMapping, SegmentGroupContext sgContext, EDIContext context) returns error? {
function ignoreSchema(EDIUnitSchema segSchema, SegmentGroupContext sgContext, EDIContext context) returns error? {

// If the current segment mapping is optional, we can ignore the current mapping and compare the
// current segment with the next mapping.
if segMapping.minOccurances == 0 {
log:printDebug(string `Ignoring optional segment: ${printEDIUnitMapping(segMapping)} | Segment text: ${context.rawIndex < context.rawSegments.length()? context.rawSegments[context.rawIndex] : "-- EOF --"}`);
sgContext.mappingIndex += 1;
if segSchema.minOccurances == 0 {
log:printDebug(string `Ignoring optional segment: ${printEDIUnitMapping(segSchema)} | Segment text: ${context.rawIndex < context.rawSegments.length()? context.rawSegments[context.rawIndex] : "-- EOF --"}`);
sgContext.schemaIndex += 1;
return;
}

// If the current segment mapping represents a repeatable segment, and we have already encountered
// at least one such segment, we can ignore the current mapping and compare the current segment with
// the next mapping.
if segMapping.maxOccurances != 1 {
var segments = sgContext.segmentGroup[segMapping.tag];
if segSchema.maxOccurances != 1 {
var segments = sgContext.segmentGroup[segSchema.tag];
if (segments is EDISegment[]|EDISegmentGroup[]) {
if segments.length() > 0 {
// This repeatable segment has already occured at least once. So move to the next mapping.
sgContext.mappingIndex += 1;
log:printDebug(string `Completed reading repeatable segment: ${printEDIUnitMapping(segMapping)} | Segment text: ${context.rawIndex < context.rawSegments.length()? context.rawSegments[context.rawIndex] : "-- EOF --"}`);
sgContext.schemaIndex += 1;
log:printDebug(string `Completed reading repeatable segment: ${printEDIUnitMapping(segSchema)} | Segment text: ${context.rawIndex < context.rawSegments.length()? context.rawSegments[context.rawIndex] : "-- EOF --"}`);
return;
}
}
}

return error(string `Mandatory unit ${printEDIUnitMapping(segMapping)} missing in the EDI.
return error(string `Mandatory unit ${printEDIUnitMapping(segSchema)} missing in the EDI.
Current segment text: ${context.rawSegments[context.rawIndex]}
Current mapping index: ${sgContext.mappingIndex}`);
Current mapping index: ${sgContext.schemaIndex}`);
}

function placeEDISegment(EDISegment segment, EDISegSchema segMapping, SegmentGroupContext sgContext, EDIContext context) returns error? {
if (segMapping.maxOccurances == 1) {
function placeEDISegment(EDISegment segment, EDISegSchema segSchema, SegmentGroupContext sgContext, EDIContext context) returns error? {
if (segSchema.maxOccurances == 1) {
// Current segment has matched with the current mapping AND current segment is not repeatable.
// So we can move to the next mapping.
log:printDebug(string `Completed reading non-repeatable segment: ${printSegMap(segMapping)}.
log:printDebug(string `Completed reading non-repeatable segment: ${printSegMap(segSchema)}.
Segment text: ${context.rawSegments[context.rawIndex]}`);
sgContext.mappingIndex += 1;
sgContext.segmentGroup[segMapping.tag] = segment;
sgContext.schemaIndex += 1;
sgContext.segmentGroup[segSchema.tag] = segment;
} else {
// Current mapping points to a repeatable segment. So we are using a EDISegment[] array to hold segments.
// Also we can't increment the mapping index here as next segment can also match with the current mapping
// as the segment is repeatable.
var segments = sgContext.segmentGroup[segMapping.tag];
var segments = sgContext.segmentGroup[segSchema.tag];
if (segments is EDISegment[]) {
if (segMapping.maxOccurances != -1 && segments.length() >= segMapping.maxOccurances) {
return error(string `${segMapping.code} is repeatable segment with maximum limit of ${segMapping.maxOccurances}.
if (segSchema.maxOccurances != -1 && segments.length() >= segSchema.maxOccurances) {
return error(string `${segSchema.code} is repeatable segment with maximum limit of ${segSchema.maxOccurances}.
EDI document contains more such segments than the allowed limit. Current row: ${context.rawIndex}`);
}
segments.push(segment);
} else if segments is null {
} else if segments is () {
segments = [segment];
sgContext.segmentGroup[segMapping.tag] = segments;
sgContext.segmentGroup[segSchema.tag] = segments;
} else {
return error(string `${segMapping.code} must be a segment array.`);
return error(string `${segSchema.code} must be a segment array.`);
}
}
}

function placeEDISegmentGroup(EDISegmentGroup segmentGroup, EDISegGroupSchema segMapping, SegmentGroupContext sgContext, EDIContext context) returns error? {
if segMapping.maxOccurances == 1 {
function placeEDISegmentGroup(EDISegmentGroup segmentGroup, EDISegGroupSchema segGroupSchema, SegmentGroupContext sgContext, EDIContext context) returns error? {
if segGroupSchema.maxOccurances == 1 {
// This is a non-repeatable mapping. So we have to compare the next segment with the next mapping.
log:printDebug(string `Completed reading non-repeating segment group ${printSegGroupMap(segMapping)} | Current segment text: ${context.rawIndex < context.rawSegments.length()? context.rawSegments[context.rawIndex] : "-- EOF --"}`);
sgContext.mappingIndex += 1;
sgContext.segmentGroup[segMapping.tag] = segmentGroup;
log:printDebug(string `Completed reading non-repeating segment group ${printSegGroupMap(segGroupSchema)} | Current segment text: ${context.rawIndex < context.rawSegments.length()? context.rawSegments[context.rawIndex] : "-- EOF --"}`);
sgContext.schemaIndex += 1;
sgContext.segmentGroup[segGroupSchema.tag] = segmentGroup;
} else {
// This is a repeatable mapping. So we compare the next segment also with the current mapping.
// i.e. we don't increment the mapping index.
var segmentGroups = sgContext.segmentGroup[segMapping.tag];
var segmentGroups = sgContext.segmentGroup[segGroupSchema.tag];
if segmentGroups is EDISegmentGroup[] {
if segMapping.maxOccurances != -1 && segmentGroups.length() >= segMapping.maxOccurances {
return error(string `${printSegGroupMap(segMapping)} is repeatable segment group with maximum limit of ${segMapping.maxOccurances}.
if segGroupSchema.maxOccurances != -1 && segmentGroups.length() >= segGroupSchema.maxOccurances {
return error(string `${printSegGroupMap(segGroupSchema)} is repeatable segment group with maximum limit of ${segGroupSchema.maxOccurances}.
EDI document contains more such segment groups than the allowed limit. Current row: ${context.rawIndex}`);
}
segmentGroups.push(segmentGroup);
} else if segmentGroups is null {
} else if segmentGroups is () {
segmentGroups = [segmentGroup];
sgContext.segmentGroup[segMapping.tag] = segmentGroups;
sgContext.segmentGroup[segGroupSchema.tag] = segmentGroups;
} else {
return error(string `${segMapping.tag} must be a segment group array.`);
return error(string `${segGroupSchema.tag} must be a segment group array.`);
}

}
}

function validateRemainingMappings(SegmentGroupContext sgContext) returns error? {
if sgContext.mappingIndex < sgContext.unitSchemas.length() - 1 {
int i = sgContext.mappingIndex + 1;
function validateRemainingSchemas(SegmentGroupContext sgContext) returns error? {
if sgContext.schemaIndex < sgContext.unitSchemas.length() - 1 {
int i = sgContext.schemaIndex + 1;
while i < sgContext.unitSchemas.length() {
EDIUnitSchema umap = sgContext.unitSchemas[i];
int minOccurs = 1;
Expand Down
Loading

0 comments on commit c99d448

Please sign in to comment.