Skip to content

Commit

Permalink
Update the copybook type names
Browse files Browse the repository at this point in the history
  • Loading branch information
DimuthuMadushan committed Nov 7, 2023
1 parent 7a89a96 commit 16f340b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 47 deletions.
4 changes: 1 addition & 3 deletions copybook-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,5 @@ compileJava {
}
}

test.dependsOn copyJar
copyAllDependencies.dependsOn test

copyAllDependencies.dependsOn copyJar
build.dependsOn(copyAllDependencies)
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class Constants {

public static final String MESSAGE_FOR_INVALID_FILE_EXTENSION = "File \"%s\" is invalid. Copybook tool support" +
" only the Copybook definition files with .cpy or .cbl extension. %nPlease provide the path of the input " +
" only the Copybook definition files with .cpy or .cob extension. %nPlease provide the path of the input " +
" file with -i or --input flag.%ne.g: bal copybook --input <Copybook Definition File>";
public static final String MESSAGE_CAN_NOT_READ_COPYBOOK_FILE =
"Provided Definition file \"%s\" is not allowed to be read";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,14 @@ private static List<String> getNumberAnnotFields(DataItem node) {

List<String> fields = new ArrayList<>();
int maxIntegerDigits = 1;
if (node.isSinged()) {
if (node.isSinged() || node.getPicture().startsWith(NEGATIVE_DECIMAL_PIC) || node.getPicture().startsWith(
GeneratorConstants.POSITIVE_DECIMAL_PIC)) {
maxIntegerDigits = node.getReadLength() - node.getFloatingPointLength() - 2;
} else {
if (node.getPicture().startsWith(NEGATIVE_DECIMAL_PIC)) {
maxIntegerDigits = node.getReadLength() - node.getFloatingPointLength() - 2;
} else {
int minValue = 0;
String fieldRef = GeneratorConstants.MIN_VALUE + GeneratorConstants.COLON + minValue;
fields.add(fieldRef);
maxIntegerDigits = node.getReadLength() - node.getFloatingPointLength() - 1;
}
int minValue = 0;
String fieldRef = GeneratorConstants.MIN_VALUE + GeneratorConstants.COLON + minValue;
fields.add(fieldRef);
maxIntegerDigits = node.getReadLength() - node.getFloatingPointLength() - 1;
}
int maxFractionDigits = node.getFloatingPointLength();
String fieldRef = GeneratorConstants.MAX_INTEGER_DIGITS + GeneratorConstants.COLON + maxIntegerDigits;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,19 @@
import static io.ballerina.tools.copybook.generator.GeneratorConstants.DECIMAL;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.DECIMAL_TYPE;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.ESCAPE_PATTERN;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.FLOATING_POINT;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.DECIMAL_POINT;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.IMPORT;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.INT;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.INTEGER_IN_BINARY_TYPE;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.INTEGER_TYPE;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.UNSIGNED_INTEGER_TYPE;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.NEGATIVE_DECIMAL_PIC;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.NEGATIVE_DECIMAL_TYPE;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.POSITIVE_DECIMAL_PIC;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.SEMICOLON;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.SIGNED_DECIMAL_TYPE;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.SIGNED_INTEGER_TYPE;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.SLASH;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.STRING;
import static io.ballerina.tools.copybook.generator.GeneratorConstants.UNSIGNED_DECIMAL_TYPE;

public class CodeGeneratorUtils {

Expand All @@ -82,6 +83,7 @@ public static TypeGenerator getTypeGenerator(CopybookNode schemaValue) {
}

public static String getTypeReferenceName(CopybookNode copybookNode, boolean isRecordFieldReference) {

if (copybookNode instanceof DataItem dataItem) {
if (isRecordFieldReference) {
return extractTypeReferenceName(dataItem);
Expand Down Expand Up @@ -160,22 +162,22 @@ public static String extractTypeReferenceName(DataItem dataItem) {
if (dataItem.getFloatingPointLength() > 0) {
if (dataItem.isSinged()) {
typeName = SIGNED_DECIMAL_TYPE + (dataItem.getReadLength() - dataItem.getFloatingPointLength() - 2)
+ FLOATING_POINT + dataItem.getFloatingPointLength();
+ DECIMAL_POINT + dataItem.getFloatingPointLength();
} else if (dataItem.getPicture().startsWith(NEGATIVE_DECIMAL_PIC) ||
dataItem.getPicture().startsWith(POSITIVE_DECIMAL_PIC)) {
typeName = DECIMAL_TYPE +
(dataItem.getReadLength() - dataItem.getFloatingPointLength() - 2)
+ DECIMAL_POINT + dataItem.getFloatingPointLength();
} else {
if (dataItem.getPicture().startsWith(NEGATIVE_DECIMAL_PIC)) {
typeName = NEGATIVE_DECIMAL_TYPE +
(dataItem.getReadLength() - dataItem.getFloatingPointLength() - 2)
+ FLOATING_POINT + dataItem.getFloatingPointLength();
} else {
typeName = DECIMAL_TYPE + (dataItem.getReadLength() - dataItem.getFloatingPointLength() - 1) +
FLOATING_POINT + dataItem.getFloatingPointLength();
}
typeName = UNSIGNED_DECIMAL_TYPE +
(dataItem.getReadLength() - dataItem.getFloatingPointLength() - 1) +
DECIMAL_POINT + dataItem.getFloatingPointLength();
}
} else {
if (dataItem.isSinged()) {
typeName = SIGNED_INTEGER_TYPE + dataItem.getReadLength();
} else {
typeName = INTEGER_TYPE + dataItem.getReadLength();
typeName = UNSIGNED_INTEGER_TYPE + dataItem.getReadLength();
}
}
} else if (dataItem.getPicture().contains(COMP_PIC)) {
Expand Down Expand Up @@ -236,6 +238,7 @@ public static String escapeIdentifier(String identifier) {
}

public static String getFileName(String filePath) {

String fileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
fileName = fileName.substring(0, fileName.lastIndexOf('.'));
return String.join("", fileName, BAL_EXTENSION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ public class GeneratorConstants {

// Copybook types
public static final String SIGNED_DECIMAL_TYPE = "SignedDecimal";
public static final String NEGATIVE_DECIMAL_TYPE = "NegativeDecimal";
public static final String UNSIGNED_DECIMAL_TYPE = "UnsignedDecimal";
public static final String DECIMAL_TYPE = "Decimal";
public static final String SIGNED_INTEGER_TYPE = "SignedInteger";
public static final String INTEGER_TYPE = "Integer";
public static final String UNSIGNED_INTEGER_TYPE = "UnsignedInteger";
public static final String ALPHA_NUMERIC_TYPE = "AlphaNumeric";
public static final String INTEGER_IN_BINARY_TYPE = "IntegerInBinary";
public static final String ARRAY_TYPE = "Array";
public static final String FLOATING_POINT = "V";
public static final String DECIMAL_POINT = "V";
public static final String NEGATIVE_DECIMAL_PIC = "-9";
public static final String POSITIVE_DECIMAL_PIC = "+9";
public static final String COMP_PIC = "COMP";

// Ballerina types
Expand Down
30 changes: 15 additions & 15 deletions copybook-cli/src/test/resources/expectedGenTypes/copybook.bal

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions copybook-cli/src/test/resources/expectedGenTypes/redefine.bal
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ballerina/constraint;
public type AlphaNumeric100 string;

@constraint:Int {minValue: 0, maxDigits: 4}
public type Integer4 int;
public type UnsignedInteger4 int;

@constraint:String {maxLength: 96}
public type AlphaNumeric96 string;
Expand All @@ -17,6 +17,6 @@ public type AlphaNumeric46 string;

public type TEST record {
AlphaNumeric100 Field\-1?;
record {Integer4 Field\-2?; AlphaNumeric96 Field\-3?;} Group\-1?;
record {Integer4 Field\-4?; AlphaNumeric50 Field\-5?; AlphaNumeric46 Field\-6?;} Group\-2?;
record {UnsignedInteger4 Field\-2?; AlphaNumeric96 Field\-3?;} Group\-1?;
record {UnsignedInteger4 Field\-4?; AlphaNumeric50 Field\-5?; AlphaNumeric46 Field\-6?;} Group\-2?;
};

0 comments on commit 16f340b

Please sign in to comment.