Skip to content

Commit

Permalink
Address sonarcloud scan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Oct 26, 2023
1 parent 5af645b commit 02d17de
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ static void validateConstraints(SyntaxNodeAnalysisContext ctx, NodeList<Annotati
checkAnnotationTagCompatibility(ctx, annotationNode, annotationTag, fieldType, fieldTypeSymbol);
checkAnnotationConstraintsAvailability(ctx, annotationNode, annotationTag, fieldType);
checkAnnotationConstraintsCompatibility(ctx, annotationNode, annotationTag, fieldType);
checkAnnotationConstraintsValidity(ctx, annotationNode, annotationTag, fieldType);
checkAnnotationConstraintsValidity(ctx, annotationNode, annotationTag);
}
}
}
Expand Down Expand Up @@ -108,7 +108,7 @@ private static void checkAnnotationConstraintsCompatibility(SyntaxNodeAnalysisCo
}

private static void checkAnnotationConstraintsValidity(SyntaxNodeAnalysisContext ctx, AnnotationNode annotationNode,
String annotationTag, String fieldType) {
String annotationTag) {
Optional<MappingConstructorExpressionNode> value = annotationNode.annotValue();
if (value.isPresent()) {
SeparatedNodeList<MappingFieldNode> constraints = value.get().fields();
Expand All @@ -118,36 +118,32 @@ private static void checkAnnotationConstraintsValidity(SyntaxNodeAnalysisContext
if (valueExpr.isPresent()) {
if (valueExpr.get() instanceof BasicLiteralNode ||
valueExpr.get() instanceof UnaryExpressionNode) {
getValueFromSimpleValueExpressionNode(ctx, annotationNode, annotationTag,
getValueFromSimpleValueExpressionNode(ctx, annotationTag,
node, valueExpr.get());
} else if (valueExpr.get() instanceof MappingConstructorExpressionNode) {
getValueFromMappingConstructor(ctx, annotationNode, annotationTag, fieldType,
node, valueExpr.get());
getValueFromMappingConstructor(ctx, annotationTag, node, valueExpr.get());
}
}
}
}
}

private static void getValueFromMappingConstructor(SyntaxNodeAnalysisContext ctx, AnnotationNode annotationNode,
String annotationTag, String fieldType, SpecificFieldNode node,
ExpressionNode valueExpr) {
private static void getValueFromMappingConstructor(SyntaxNodeAnalysisContext ctx, String annotationTag,
SpecificFieldNode node, ExpressionNode valueExpr) {
MappingConstructorExpressionNode expressionNode = (MappingConstructorExpressionNode) valueExpr;
SeparatedNodeList<MappingFieldNode> fields = expressionNode.fields();
for (MappingFieldNode field : fields) {
SpecificFieldNode fieldNode = (SpecificFieldNode) field;
if (fieldNode.fieldName().toString().trim().equals(CONSTRAINT_VALUE)) {
Optional<ExpressionNode> fieldExpr = fieldNode.valueExpr();
fieldExpr.ifPresent(expressionNode1 -> getValueFromSimpleValueExpressionNode(ctx,
annotationNode, annotationTag, node, expressionNode1));
annotationTag, node, expressionNode1));
}
}
}

private static void getValueFromSimpleValueExpressionNode(SyntaxNodeAnalysisContext ctx,
AnnotationNode annotationNode, String annotationTag,
SpecificFieldNode node,
ExpressionNode valueExpr) {
private static void getValueFromSimpleValueExpressionNode(SyntaxNodeAnalysisContext ctx, String annotationTag,
SpecificFieldNode node, ExpressionNode valueExpr) {
String constraintValue = valueExpr.toString().trim()
.replaceAll(SYMBOL_NEW_LINE, EMPTY)
.replaceAll(SYMBOL_DECIMAL, EMPTY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,38 +96,45 @@ record DigitParts(int integerDigits, int fractionDigits) {
default DigitParts getDigitPartsFromDouble(double value) {
String valueString = Double.toString(value);
if (valueString.matches(SCIENTIFIC_NOTATION_REGEX)) {
Pattern pattern = Pattern.compile(SCIENTIFIC_NOTATION_REGEX);
Matcher matcher = pattern.matcher(valueString);
if (matcher.matches()) {
String groupA = matcher.group(1);
String groupB = matcher.group(2);
String groupC = matcher.group(3);

int countA = value < 0 ? groupA.length() - 1 : groupA.length();
int countB = groupB.equals(ZERO_STRING) ? 0 : groupB.length();
int intC = Integer.parseInt(groupC);

int intDigits;
int fractionDigits;
if (intC > 0) {
intDigits = countA + Math.min(countB, intC);
fractionDigits = countB - Math.min(countB, intC);
} else {
intDigits = Math.max(1, countA + intC);
fractionDigits = countB - intC;
}
return new DigitParts(intDigits, fractionDigits);
}
return null;
return getDigitPartsFromScientificNotation(value, valueString);
} else {
return getDigitPartsFromString(value, valueString);
}
}

private static DigitParts getDigitPartsFromString(double value, String valueString) {
String[] parts = valueString.split(DOT_SEPARATOR);
int intDigits = value < 0 ? parts[0].length() - 1 : parts[0].length();
if (parts.length == 1) {
return new DigitParts(intDigits, 0);

Check warning on line 109 in native/src/main/java/io/ballerina/stdlib/constraint/validators/interfaces/DigitsValidator.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/constraint/validators/interfaces/DigitsValidator.java#L109

Added line #L109 was not covered by tests
} else {
String[] parts = valueString.split(DOT_SEPARATOR);
int intDigits = value < 0 ? parts[0].length() - 1 : parts[0].length();
if (parts.length == 1) {
return new DigitParts(intDigits, 0);
return new DigitParts(intDigits, parts[1].length());
}
}

private static DigitParts getDigitPartsFromScientificNotation(double value, String valueString) {
Pattern pattern = Pattern.compile(SCIENTIFIC_NOTATION_REGEX);
Matcher matcher = pattern.matcher(valueString);
int intDigits = 0;
int fractionDigits = 0;
if (matcher.matches()) {
String groupA = matcher.group(1);
String groupB = matcher.group(2);
String groupC = matcher.group(3);

int countA = value < 0 ? groupA.length() - 1 : groupA.length();
int countB = groupB.equals(ZERO_STRING) ? 0 : groupB.length();
int intC = Integer.parseInt(groupC);

if (intC > 0) {
intDigits = countA + Math.min(countB, intC);
fractionDigits = countB - Math.min(countB, intC);

Check warning on line 131 in native/src/main/java/io/ballerina/stdlib/constraint/validators/interfaces/DigitsValidator.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/stdlib/constraint/validators/interfaces/DigitsValidator.java#L130-L131

Added lines #L130 - L131 were not covered by tests
} else {
return new DigitParts(intDigits, parts[1].length());
intDigits = Math.max(1, countA + intC);
fractionDigits = countB - intC;
}
}
return new DigitParts(intDigits, fractionDigits);
}

boolean validateMaxDigits(Object fieldValue, Object constraintValue);
Expand Down

0 comments on commit 02d17de

Please sign in to comment.