Skip to content

Commit

Permalink
Applied a little refactoring for numberCoercing
Browse files Browse the repository at this point in the history
  • Loading branch information
davidepalladino-apuliasoft committed Feb 11, 2025
1 parent d2be743 commit 6aa7f70
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ private fun moveaNumber(
IntValue.ZERO
}
}
numberCoercing(elementValue, targetArray.elementType, newValue.elementType)

if (newValue.elementType is NumberType && targetArray.elementType is NumberType) {
numberCoercing(elementValue, targetArray.elementType as NumberType, newValue.elementType as NumberType)
} else {
elementValue
}
}
}
return arrayValue
Expand Down Expand Up @@ -219,9 +224,9 @@ private fun valueFromSourceExpression(interpreterCore: InterpreterCore, valueExp
* the conversion.
*
* @param sourceValue The `Value` to be coerced, generally a numeric type such as `DecimalValue` or `IntValue`.
* @param targetType The target `Type` for the coercion, used to guide the conversion, particularly
* @param targetType The target `NumberType` for the coercion, used to guide the conversion, particularly
* regarding the number of decimal places.
* @param sourceType The `Type` representing the original type of `sourceValue`, assisting in determining
* @param sourceType The `NumberType` representing the original type of `sourceValue`, assisting in determining
* the required scale and format for conversion.
*
* @return The resulting `Value` after coercion, transformed to align with `targetType`.
Expand All @@ -233,20 +238,20 @@ private fun valueFromSourceExpression(interpreterCore: InterpreterCore, valueExp
*/
private fun numberCoercing(
sourceValue: Value,
targetType: Type,
sourceType: Type
targetType: NumberType,
sourceType: NumberType
): Value {
// Number of digits between source and target must be equals.
if (sourceType is NumberType && targetType is NumberType && sourceType.numberOfDigits != targetType.numberOfDigits) {
if (sourceType.numberOfDigits != targetType.numberOfDigits) {
throw IllegalStateException("Factor 2 and Result with different type and size.")
}

return when {
// Proper conversion between a left side as decimal to right side as integer
sourceValue is DecimalValue && sourceType is NumberType && targetType is NumberType && targetType.decimalDigits == 0 ->
sourceValue is DecimalValue && targetType.decimalDigits == 0 ->
DecimalValue(sourceValue.value * 10.0.pow(targetType.entireDigits - sourceType.entireDigits).toBigDecimal())
// Or integer to decimal
sourceValue is IntValue && targetType is NumberType && targetType.decimalDigits > 0 ->
sourceValue is IntValue && targetType.decimalDigits > 0 ->
DecimalValue((sourceValue.value / 10.0.pow(targetType.decimalDigits)).toBigDecimal())
else -> sourceValue
}
Expand Down

0 comments on commit 6aa7f70

Please sign in to comment.