Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Decimal Length Validation Failure #29

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions ballerina/json_to_copybook_convertor.bal
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,13 @@ class JsonToCopybookConvertor {
return decimalString.padZero(dataItem.getReadLength() - supressZeroCount).padStart(dataItem.getReadLength());
}

private isolated function checkDecimalLength(string wholeNumber, string fraction, decimal input, DataItem dataItem) returns error? {
private isolated function checkDecimalLength(string wholeNumber, string fraction, decimal input,
DataItem dataItem) returns error? {
// A deducted of 1 made from readLength for decimal seperator "."
int expectedWholeNumberLength = dataItem.getReadLength() - dataItem.getFloatingPointLength() - 1;
// If PIC has + and value is positive then remove the space allocated for "+" sign
expectedWholeNumberLength -= dataItem.getPicture().startsWith("+") && input > 0d ? 1 : 0;
// If PIC has + or -, then remove the space allocated for the sign
string picture = dataItem.getPicture();
expectedWholeNumberLength -= (picture.startsWith("+") || picture.startsWith("-")) is true ? 1 : 0;
if wholeNumber.length() > expectedWholeNumberLength {
return error Error(string `Value '${input}' exceeds the maximum number of integer digits `
+ string `${expectedWholeNumberLength} at ${self.getPath()}`);
Expand Down
5 changes: 3 additions & 2 deletions ballerina/tests/resources/copybook-json/copybook-9.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
"Employee": {
"EmployeeId": 2.345234,
"ContractYears": 323.33,
"Salary": 5.200,
"Salary": 5.2002,
"Bonus": 3412.000000,
"APIT": 34567.6543
"APIT": 34567.6543,
"Stationary": -345.455
}
}
7 changes: 4 additions & 3 deletions ballerina/tests/resources/copybooks/copybook-9.cpy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
01 Employee.
03 EmployeeId PIC 9(3).9.
03 ContractYears PIC 9(2).999.
03 Salary PIC 9(6).99.
03 Bonus PIC 9(4).999.
03 APIT PIC 9(4).99.
03 Salary PIC +9(6).99.
03 Bonus PIC -9(4).999.
03 APIT PIC -9(4).99.
03 Stationary PIC -9(2).99.
4 changes: 3 additions & 1 deletion ballerina/tests/resources/errors/copybook-9.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"errors": [
"Value '2.345234' exceeds the maximum number of fraction digits 1 at '$.Employee.EmployeeId'",
"Value '323.33' exceeds the maximum number of integer digits 2 at '$.Employee.ContractYears'",
"Value '34567.6543' exceeds the maximum number of integer digits 4 at '$.Employee.APIT'"
"Value '5.2002' exceeds the maximum number of fraction digits 2 at '$.Employee.Salary'",
"Value '34567.6543' exceeds the maximum number of integer digits 4 at '$.Employee.APIT'",
"Value '-345.455' exceeds the maximum number of integer digits 2 at '$.Employee.Stationary'"
]
}
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## [Unreleased]

### Fixed
- [[#5772] Fix Decimal Length Validation Failure](https://github.com/ballerina-platform/ballerina-library/issues/5772)

### Changed
- [[#5758] Remove Rounding Up Decimal Values when Converting Json to Copybook](https://github.com/ballerina-platform/ballerina-library/issues/5758)
Loading