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

Introduce breaking changes to toString() method #25738

Merged
merged 71 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
1b39e4e
Add initial implementation of toString functionality
mohanvive Sep 9, 2020
e37fe37
Add additional changes for toString functionality
mohanvive Sep 10, 2020
bee2256
Merge branch 'tostring-impl' of https://github.com/mohanvive/ballerin…
lasinicl Sep 10, 2020
a67e033
Merge branch 'master' of https://github.com/ballerina-platform/baller…
mohanvive Sep 10, 2020
2aeb066
Merge branch 'tostring-impl' of https://github.com/mohanvive/ballerin…
lasinicl Sep 10, 2020
ff40c9c
Add toString() for error type
lasinicl Sep 10, 2020
2badd4d
Fix test cases due to toSting impl change
mohanvive Sep 10, 2020
2c24e11
Add additional xml tags to informal toString conversion
mohanvive Sep 10, 2020
3d0384d
Fix tests to sync toString change
pcnfernando Sep 10, 2020
220cefc
Fix types package after toString change
grainier Sep 10, 2020
1170f1f
Add additional xml tags to informal toString conversion
mohanvive Sep 10, 2020
866a259
Fix parser/error handler as per review comments
grainier Sep 10, 2020
d93c421
Merge remote-tracking branch 'mohan/tostring-impl' into tostring-impl
grainier Sep 10, 2020
8541761
Revert testng changes
grainier Sep 10, 2020
e312e73
Merge pull request #6 from pcnfernando/tostring-impl
mohanvive Sep 10, 2020
fc213fb
Merge pull request #7 from grainier/tostring-impl
mohanvive Sep 10, 2020
aa5b9cf
Add toString() for error type
lasinicl Sep 10, 2020
a8318e7
Refactor code
lasinicl Sep 10, 2020
5d15dea
Add additional xml tags to informal toString conversion
mohanvive Sep 10, 2020
92a64da
Merge branch 'tostring-impl' of https://github.com/mohanvive/ballerin…
mohanvive Sep 10, 2020
8be12d2
Fix error test
lasinicl Sep 10, 2020
c7cca18
Merge branch 'tostring-impl' of https://github.com/mohanvive/ballerin…
lasinicl Sep 10, 2020
1ef8810
Add additional xml tags to informal toString conversion
mohanvive Sep 10, 2020
d2eca11
Fix assignments test
lasinicl Sep 10, 2020
850d459
Fix jvm error test
lasinicl Sep 10, 2020
2db9f3e
Fix member access test
lasinicl Sep 10, 2020
0567963
Fix binary operations test
lasinicl Sep 10, 2020
cdb10a3
Undo changes
lasinicl Sep 10, 2020
1b31fc9
Add additional xml tags to informal toString conversion
mohanvive Sep 10, 2020
63c611f
Merge pull request #5 from lasinicl/toString-impl
mohanvive Sep 10, 2020
01dd1b6
Add additional xml tags to informal toString conversion
mohanvive Sep 10, 2020
0224154
Add additional xml tags to informal toString conversion
mohanvive Sep 11, 2020
0850651
Fix error value toString()
lasinicl Sep 11, 2020
f1a77c2
Merge pull request #8 from lasinicl/toString-impl
mohanvive Sep 11, 2020
135ba8e
Fix tests to sync toString change
pcnfernando Sep 11, 2020
add5276
Merge branch 'tostring-impl' of https://github.com/mohanvive/ballerin…
pcnfernando Sep 11, 2020
06eb7f4
Add additional xml tags to informal toString conversion
mohanvive Sep 11, 2020
842c44f
Merge branch 'tostring-impl' of https://github.com/mohanvive/ballerin…
mohanvive Sep 11, 2020
b2cee6e
Add additional xml tags to informal toString conversion
mohanvive Sep 11, 2020
ae5f37f
Add additional xml tags to informal toString conversion
mohanvive Sep 11, 2020
9aa0a5d
Fix mime tests to sync toString change
pcnfernando Sep 11, 2020
d545bba
Add additional xml tags to informal toString conversion
mohanvive Sep 11, 2020
64e284b
Merge branch 'tostring-impl' of https://github.com/mohanvive/ballerin…
mohanvive Sep 11, 2020
dc554b5
Add additional xml tags to informal toString conversion
mohanvive Sep 11, 2020
8ff59bf
Fix interop package after toString change
grainier Sep 11, 2020
587ce9a
Merge branch 'tostring-impl' of github.com:mohanvive/ballerina-lang i…
grainier Sep 11, 2020
e520615
Add additional changes for toString functionality
mohanvive Sep 11, 2020
a9809f9
Fix langlib tests
lasinicl Sep 11, 2020
98ed994
Merge branch 'tostring-impl' of github.com:mohanvive/ballerina-lang i…
grainier Sep 11, 2020
013700c
Fix checkstyle issues
mohanvive Sep 11, 2020
436ed07
Merge branch 'tostring-impl' of github.com:mohanvive/ballerina-lang i…
grainier Sep 11, 2020
7e386f1
Fix interop test
grainier Sep 11, 2020
625d7f0
Merge pull request #9 from grainier/tostring-impl
mohanvive Sep 11, 2020
5d3cd66
Fix checkstyle issues
mohanvive Sep 11, 2020
d455657
Merge branch 'master' of https://github.com/ballerina-platform/baller…
mohanvive Sep 11, 2020
b36ed24
Merge pull request #10 from lasinicl/toString-impl
mohanvive Sep 11, 2020
ea442a3
Add toString for cycles
gimantha Sep 11, 2020
b6b2b17
Merge branch 'tostring-impl' of https://github.com/mohanvive/ballerin…
gimantha Sep 11, 2020
4a8f3a3
Merge pull request #11 from gimantha/toString-cycles
mohanvive Sep 11, 2020
1a0051c
Fix links
gimantha Sep 11, 2020
5d2920e
Merge pull request #12 from gimantha/toString-cycles
mohanvive Sep 11, 2020
79a5459
Fix checkstyle issues
gimantha Sep 11, 2020
b3bb50b
Merge pull request #13 from gimantha/toString-cycles
mohanvive Sep 11, 2020
eb77f5e
Fix Java doc issues
gimantha Sep 11, 2020
96d920f
Merge pull request #14 from gimantha/toString-cycles
mohanvive Sep 11, 2020
514c179
Fix spacing issue in Array, Map and Table
mohanvive Sep 11, 2020
19c6d75
Merge branch 'tostring-impl' of https://github.com/mohanvive/ballerin…
mohanvive Sep 11, 2020
4a9f135
Fix testcases according to the changes in spacing for array and table
mohanvive Sep 11, 2020
0dffd4d
Fix tests
gimantha Sep 11, 2020
d535d3a
Merge pull request #15 from gimantha/toString-cycles
mohanvive Sep 11, 2020
2a583d0
Fix IO print test assertion
pcnfernando Sep 11, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.ballerinalang.jvm;

import org.ballerinalang.jvm.values.api.BLink;

/**
* This class contains the utility methods required for identifying cycles.
*
* @since Swan Lake
*/
public class CycleUtils {

/**
* Internal class which represents the link between the members of ballerina values.
* This is used to detect the cycles in a map or array
*/
public static class Node implements BLink {
Object obj;
BLink parent;

public Node(Object obj, BLink parent) {
this.obj = obj;
this.parent = parent;
}

public boolean hasCyclesSoFar() {
Node parent = (Node) this.parent;
while (parent != null) {
if (parent.obj == obj) {
return true;
}
parent = (Node) parent.parent;
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private static void constructJsonData(MapValueImpl record, MapValue<BString, Obj
break;
case TypeTags.XML_TAG:
BString strVal = org.ballerinalang.jvm.StringUtils.fromString(
org.ballerinalang.jvm.values.utils.StringUtils.getStringValue(record.get(key)));
org.ballerinalang.jvm.values.utils.StringUtils.getStringValue(record.get(key), null));
jsonObject.put(StringUtils.fromString(name), strVal);
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.util.Map;

import static org.ballerinalang.jvm.CycleUtils.Node;
import static org.ballerinalang.jvm.util.exceptions.BallerinaErrorReasons.TABLE_KEY_CYCLIC_VALUE_REFERENCE_ERROR;

/**
Expand Down Expand Up @@ -103,29 +104,4 @@ public static Long hash(Object obj, Node parent) {
public static void handleTableStore(TableValue<Object, Object> tableValue, Object key, Object value) {
tableValue.put(key, value);
}

/**
* Internal class which represents the link between the members of ballerina values.
* This is used to detect the cycles in a map or array
*/
static class Node {
Object obj;
Node parent;

public Node(Object obj, Node parent) {
this.obj = obj;
this.parent = parent;
}

public boolean hasCyclesSoFar() {
Node parent = this.parent;
while (parent != null) {
if (parent.obj == obj) {
return true;
}
parent = parent.parent;
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ private static String anyToString(Object sourceVal) {
} else if (sourceVal instanceof Boolean) {
return Boolean.toString((Boolean) sourceVal);
} else if (sourceVal instanceof DecimalValue) {
return ((DecimalValue) sourceVal).stringValue();
return ((DecimalValue) sourceVal).stringValue(null);
} else if (sourceVal instanceof String) {
return (String) sourceVal;
} else if (sourceVal == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.ballerinalang.jvm.util.exceptions.BLangExceptionHelper;
import org.ballerinalang.jvm.util.exceptions.BallerinaException;
import org.ballerinalang.jvm.values.api.BArray;
import org.ballerinalang.jvm.values.api.BLink;
import org.ballerinalang.jvm.values.api.BString;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -237,7 +238,7 @@ public void unshift(BArray values) {
}

@Override
public abstract String stringValue();
public abstract String stringValue(BLink parent);

@Override
public abstract BType getType();
Expand Down Expand Up @@ -265,7 +266,7 @@ public Object frozenCopy(Map<Object, Object> refs) {

@Override
public String toString() {
return stringValue();
return stringValue(null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.ballerinalang.jvm.util.Flags;
import org.ballerinalang.jvm.util.exceptions.BLangExceptionHelper;
import org.ballerinalang.jvm.util.exceptions.RuntimeErrors;
import org.ballerinalang.jvm.values.api.BLink;
import org.ballerinalang.jvm.values.api.BString;

import java.util.HashMap;
Expand Down Expand Up @@ -87,10 +88,15 @@ public BString getStringValue(BString fieldName) {
}

@Override
public String stringValue() {
public String stringValue(BLink parent) {
return "object " + type.toString();
}

@Override
public String informalStringValue(BLink parent) {
return stringValue(parent);
}

@Override
public boolean getBooleanValue(BString fieldName) {
return (boolean) get(fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.ballerinalang.jvm.values;

import org.ballerinalang.jvm.BallerinaErrors;
import org.ballerinalang.jvm.CycleUtils;
import org.ballerinalang.jvm.TypeChecker;
import org.ballerinalang.jvm.commons.ArrayState;
import org.ballerinalang.jvm.types.BArrayType;
Expand All @@ -30,7 +31,9 @@
import org.ballerinalang.jvm.util.exceptions.BallerinaException;
import org.ballerinalang.jvm.util.exceptions.RuntimeErrors;
import org.ballerinalang.jvm.values.api.BArray;
import org.ballerinalang.jvm.values.api.BLink;
import org.ballerinalang.jvm.values.api.BString;
import org.ballerinalang.jvm.values.api.BValue;
import org.ballerinalang.jvm.values.utils.StringUtils;

import java.io.IOException;
Expand Down Expand Up @@ -544,8 +547,8 @@ public void unshift(BArray values) {
}

@Override
public String stringValue() {
StringJoiner sj = new StringJoiner(" ");
public String stringValue(BLink parent) {
StringJoiner sj = new StringJoiner(",");
switch (this.elementType.getTag()) {
case TypeTags.INT_TAG:
case TypeTags.SIGNED32_INT_TAG:
Expand Down Expand Up @@ -576,16 +579,36 @@ public String stringValue() {
case TypeTags.STRING_TAG:
case TypeTags.CHAR_STRING_TAG:
for (int i = 0; i < size; i++) {
sj.add(bStringValues[i].getValue());
sj.add(((BValue) (bStringValues[i])).informalStringValue(parent));
}
break;
default:
for (int i = 0; i < size; i++) {
sj.add(StringUtils.getStringValue(refValues[i]));
if (refValues[i] == null) {
sj.add("null");
} else {
BType type = TypeChecker.getType(refValues[i]);
switch (type.getTag()) {
case TypeTags.STRING_TAG:
mohanvive marked this conversation as resolved.
Show resolved Hide resolved
case TypeTags.XML_TAG:
case TypeTags.XML_ELEMENT_TAG:
case TypeTags.XML_ATTRIBUTES_TAG:
case TypeTags.XML_COMMENT_TAG:
case TypeTags.XML_PI_TAG:
case TypeTags.XMLNS_TAG:
case TypeTags.XML_TEXT_TAG:
sj.add(((BValue) (refValues[i])).informalStringValue(new CycleUtils
.Node(this, parent)));
break;
default:
sj.add(StringUtils.getStringValue(refValues[i], new CycleUtils.Node(this, parent)));
break;
}
}
}
break;
}
return sj.toString();
return "[" + sj.toString() + "]";
}

@Override
Expand Down Expand Up @@ -713,7 +736,7 @@ public ArrayValueImpl slice(long startIndex, long endIndex) {

@Override
public String toString() {
return stringValue();
return stringValue(null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.ballerinalang.jvm.values;

import org.ballerinalang.jvm.values.api.BLink;
import org.ballerinalang.jvm.values.api.BString;

/**
Expand Down Expand Up @@ -60,10 +61,15 @@ public BString concat(BString str) {
}

@Override
public String stringValue() {
public String stringValue(BLink parent) {
return value;
}

@Override
public String informalStringValue(BLink parent) {
return "\"" + toString() + "\"";
}

@Override
public int hashCode() {
return value.hashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.ballerinalang.jvm.types.BTypes;
import org.ballerinalang.jvm.util.BLangConstants;
import org.ballerinalang.jvm.values.api.BDecimal;
import org.ballerinalang.jvm.values.api.BLink;

import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -159,7 +160,8 @@ public long intValue() {
}

if (!isDecimalWithinIntRange(value)) {
throw BallerinaErrors.createNumericConversionError(this.stringValue(), BTypes.typeDecimal, BTypes.typeInt);
throw BallerinaErrors.createNumericConversionError(this.stringValue(null), BTypes.typeDecimal,
BTypes.typeInt);
}
return (long) Math.rint(value.doubleValue());
}
Expand Down Expand Up @@ -222,8 +224,9 @@ public boolean booleanValue() {
/**
* Get the string value.
* @return string value
* @param parent The link to the parent node
*/
public String stringValue() {
public String stringValue(BLink parent) {
if (this.valueKind != DecimalValueKind.OTHER) {
return this.valueKind.getValue();
}
Expand Down Expand Up @@ -519,7 +522,7 @@ public int hashCode() {
*/
@Override
public String toString() {
return this.stringValue();
return this.stringValue(null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@
package org.ballerinalang.jvm.values;

import org.ballerinalang.jvm.BallerinaErrors;
import org.ballerinalang.jvm.CycleUtils;
import org.ballerinalang.jvm.TypeChecker;
import org.ballerinalang.jvm.services.ErrorHandlerUtils;
import org.ballerinalang.jvm.types.BErrorType;
import org.ballerinalang.jvm.types.BType;
import org.ballerinalang.jvm.types.BTypes;
import org.ballerinalang.jvm.types.TypeConstants;
import org.ballerinalang.jvm.types.TypeTags;
import org.ballerinalang.jvm.values.api.BError;
import org.ballerinalang.jvm.values.api.BLink;
import org.ballerinalang.jvm.values.api.BString;
import org.ballerinalang.jvm.values.api.BValue;
import org.ballerinalang.jvm.values.utils.StringUtils;

import java.io.PrintWriter;
import java.util.HashMap;
Expand Down Expand Up @@ -73,21 +78,54 @@ public ErrorValue(BType type, BString message, ErrorValue cause, Object details)
}

@Override
public String stringValue() {
public String stringValue(BLink parent) {
CycleUtils.Node linkParent = new CycleUtils.Node(this, parent);
if (isEmptyDetail()) {
return "error " + message.getValue();
return "error" + getModuleName() + "(" + ((StringValue) message).informalStringValue(linkParent) + ")";
}
return "error " + message.getValue() + " " + getCauseToString() +
org.ballerinalang.jvm.values.utils.StringUtils.getStringValue(details);
return "error" + getModuleName() + "(" + ((StringValue) message).informalStringValue(linkParent) +
getCauseToString(linkParent) + getDetailsToString(linkParent) + ")";
}

private String getCauseToString() {
private String getCauseToString(BLink parent) {
if (cause != null) {
return org.ballerinalang.jvm.values.utils.StringUtils.getStringValue(cause) + " ";
return ", " + cause.informalStringValue(parent);
}
return "";
}

private String getDetailsToString(BLink parent) {
StringJoiner sj = new StringJoiner(", ");
for (Object key : ((MapValue) details).getKeys()) {
Object value = ((MapValue) details).get(key);
if (value == null) {
sj.add(key + "=null");
} else {
BType type = TypeChecker.getType(value);
mohanvive marked this conversation as resolved.
Show resolved Hide resolved
switch (type.getTag()) {
case TypeTags.STRING_TAG:
case TypeTags.XML_TAG:
case TypeTags.XML_ELEMENT_TAG:
case TypeTags.XML_ATTRIBUTES_TAG:
case TypeTags.XML_COMMENT_TAG:
case TypeTags.XML_PI_TAG:
case TypeTags.XMLNS_TAG:
case TypeTags.XML_TEXT_TAG:
sj.add(key + "=" + ((BValue) value).informalStringValue(parent));
break;
default:
sj.add(key + "=" + StringUtils.getStringValue(value, parent));
break;
}
}
}
return ", " + sj.toString();
}

private String getModuleName() {
return type.getPackage().name == null ? "" : " " + type.getName() + " ";
}

@Override
public BType getType() {
return type;
Expand All @@ -114,7 +152,7 @@ public void freezeDirect() {

@Override
public String toString() {
return stringValue();
return stringValue(null);
}

/**
Expand Down
Loading