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 issues regarding finites type with byte values #63

Merged
2 changes: 1 addition & 1 deletion ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.11.0-20241121-075100-c4c87cbc"
distribution-version = "2201.11.0-20241204-163800-0d1e4836"

[[package]]
org = "ballerina"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
org.gradle.caching=true
group=io.ballerina.lib
version=1.2.0-SNAPSHOT
ballerinaLangVersion=2201.11.0-20241121-075100-c4c87cbc
ballerinaLangVersion=2201.11.0-20241204-163800-0d1e4836

checkstyleToolVersion=10.12.0
puppycrawlCheckstyleVersion=10.12.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
import java.util.Comparator;
import java.util.List;

import static io.ballerina.runtime.api.constants.RuntimeConstants.BBYTE_MAX_VALUE;
import static io.ballerina.runtime.api.constants.RuntimeConstants.BBYTE_MIN_VALUE;

/**
* Native implementation of data:fromStringWithType(string).
*
Expand Down Expand Up @@ -79,16 +82,22 @@ public static Object fromStringWithType(BString string, BTypedesc typed) {

public static Object fromStringWithType(BString string, Type expType) {
String value = string.getValue();
int tag = expType.getTag();

try {
switch (expType.getTag()) {
if (TypeTags.isStringTypeTag(tag)) {
return string;
}

switch (tag) {
case TypeTags.INT_TAG:
return stringToInt(value);
case TypeTags.BYTE_TAG:
return stringToByte(value);
case TypeTags.FLOAT_TAG:
return stringToFloat(value);
case TypeTags.DECIMAL_TAG:
return stringToDecimal(value);
case TypeTags.STRING_TAG:
return string;
case TypeTags.BOOLEAN_TAG:
return stringToBoolean(value);
case TypeTags.NULL_TAG:
Expand Down Expand Up @@ -145,6 +154,19 @@ private static Long stringToInt(String value) throws NumberFormatException {
return Long.parseLong(value);
}

private static int stringToByte(String value) throws NumberFormatException {
Long number = Long.parseLong(value);
int intValue = number.intValue();
if (isByteLiteral(intValue)) {
return intValue;
}
throw new NumberFormatException();
}

private static boolean isByteLiteral(long longValue) {
return (longValue >= BBYTE_MIN_VALUE && longValue <= BBYTE_MAX_VALUE);
}

private static Double stringToFloat(String value) throws NumberFormatException {
if (hasFloatOrDecimalLiteralSuffix(value)) {
throw new NumberFormatException();
Expand Down
Loading