Skip to content

Commit

Permalink
Merge pull request #974 from Nadeeshan96/main-use-convert-api
Browse files Browse the repository at this point in the history
Use convert API instead of `cloneWithType`
  • Loading branch information
Bhashinee authored Apr 19, 2023
2 parents e0dfce9 + f620628 commit 75d4594
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.ballerina.runtime.api.utils.JsonUtils;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.utils.ValueUtils;
import io.ballerina.runtime.api.utils.XmlUtils;
import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BError;
Expand Down Expand Up @@ -69,7 +70,6 @@
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.handler.codec.http.HttpHeaders;
import org.ballerinalang.langlib.value.CloneReadOnly;
import org.ballerinalang.langlib.value.CloneWithType;
import org.ballerinalang.langlib.value.FromJsonString;
import org.ballerinalang.langlib.value.FromJsonStringWithType;
import org.slf4j.Logger;
Expand Down Expand Up @@ -474,13 +474,13 @@ public static void dispatchOnText(WebSocketConnectionInfo connectionInfo, WebSoc
bValue = XmlUtils.parse(stringAggregator.getAggregateString());
break;
case TypeTags.RECORD_TYPE_TAG:
bValue = CloneWithType.convert(param, JsonUtils.parse(
stringAggregator.getAggregateString()));
bValue = ValueUtils.convert(JsonUtils.parse(stringAggregator.getAggregateString()),
param);
break;
case TypeTags.UNION_TAG:
if (WebSocketUtil.hasStringType(param)) {
bValue = CloneWithType.convert(param,
StringUtils.fromString(stringAggregator.getAggregateString()));
bValue = ValueUtils.convert(
StringUtils.fromString(stringAggregator.getAggregateString()), param);
break;
}
// fall through
Expand Down Expand Up @@ -706,11 +706,11 @@ private static void createBvaluesForBinary(MethodType onBinaryMessageResource, B
bValue = XmlUtils.parse(getBString(byteArray));;
break;
case TypeTags.RECORD_TYPE_TAG:
bValue = CloneWithType.convert(param, JsonUtils.parse(getBString(byteArray)));
bValue = ValueUtils.convert(JsonUtils.parse(getBString(byteArray)), param);
break;
case TypeTags.UNION_TAG:
if (hasByteArrayType(param)) {
bValue = CloneWithType.convert(param, ValueCreator.createArrayValue(byteArray));
bValue = ValueUtils.convert(ValueCreator.createArrayValue(byteArray), param);
break;
}
// fall through
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.ballerina.runtime.api.utils.JsonUtils;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.utils.ValueUtils;
import io.ballerina.runtime.api.utils.XmlUtils;
import io.ballerina.runtime.api.values.BError;
import io.ballerina.runtime.api.values.BObject;
Expand All @@ -41,7 +42,6 @@
import io.ballerina.stdlib.websocket.WebSocketUtil;
import io.ballerina.stdlib.websocket.observability.WebSocketObservabilityUtil;
import io.ballerina.stdlib.websocket.server.WebSocketConnectionInfo;
import org.ballerinalang.langlib.value.CloneWithType;
import org.ballerinalang.langlib.value.FromJsonStringWithType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -94,12 +94,12 @@ public void onMessage(WebSocketTextMessage webSocketTextMessage) {
message = XmlUtils.parse(stringAggregator.getAggregateString());
break;
case TypeTags.RECORD_TYPE_TAG:
message = CloneWithType.convert(targetType, JsonUtils.parse(
message = cloneWithType(targetType, JsonUtils.parse(
stringAggregator.getAggregateString()));
break;
case TypeTags.UNION_TAG:
if (WebSocketUtil.hasStringType(targetType)) {
message = CloneWithType.convert(targetType,
message = cloneWithType(targetType,
StringUtils.fromString(stringAggregator.getAggregateString()));
break;
}
Expand Down Expand Up @@ -188,11 +188,11 @@ public void onMessage(WebSocketBinaryMessage webSocketBinaryMessage) {
message = XmlUtils.parse(getBString(binMsg));;
break;
case TypeTags.RECORD_TYPE_TAG:
message = CloneWithType.convert(targetType, JsonUtils.parse(getBString(binMsg)));
message = cloneWithType(targetType, JsonUtils.parse(getBString(binMsg)));
break;
case TypeTags.UNION_TAG:
if (WebSocketUtil.hasByteArrayType(targetType)) {
message = CloneWithType.convert(targetType, ValueCreator.createArrayValue(binMsg));
message = cloneWithType(targetType, ValueCreator.createArrayValue(binMsg));
break;
}
// fall through
Expand Down Expand Up @@ -318,4 +318,12 @@ public void setTargetType(BTypedesc targetType) {
public void setFutureCompleted(AtomicBoolean futureCompleted) {
this.futureCompleted = futureCompleted;
}

private Object cloneWithType(Type targetType, Object value) {
try {
return ValueUtils.convert(value, targetType);
} catch (BError e) {
return e;
}
}
}

0 comments on commit 75d4594

Please sign in to comment.