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

Clarification on value:fromBalString behaviour #1279

Closed
Nadeeshan96 opened this issue Nov 17, 2023 · 1 comment
Closed

Clarification on value:fromBalString behaviour #1279

Nadeeshan96 opened this issue Nov 17, 2023 · 1 comment

Comments

@Nadeeshan96
Copy link
Contributor

Description:

The spec says that

# The subset of Ballerina expression syntax supported is that produced
# by toBalString when applied to an anydata value.
public isolated function fromBalString(string s) returns anydata|error = external;

and #135 (comment) also says

fromBalString only works on what toBalString produces for anydata.

Does that mean we have to be restrictive and give errors for all the strings which would not be produced by value:toBalString function when value:fromBalString function is called on them?

E.g.

import ballerina/io;

public function main() {
    map<string> m = {name:"John", city:"London"};
    io:println(m.toBalString()); // {"name":"John","city":"London"}

    string str = string `{"name":"John","city":"London"}`;
    anydata|error ans = str.fromBalString(); // okay

    str = string `{"name":"John", "city":"London"}`;
    ans = str.fromBalString(); // error, because there is a space after the comma

    str = string `{name:"John",city:"London"}`;
    ans = str.fromBalString(); // error, because the keys are not quoted
}

Suggested Labels:

Code sample that shows issue:

Related Issues:

ballerina-platform/ballerina-lang#41700

@Nadeeshan96
Copy link
Contributor Author

In the current implementation, value:fromBalString does not give an error when only whitespace characters are in between list/map elements. Since giving an error in those cases are too restrictive for the user and affect user-friendliness, the implementation is continued as it is. Hence the above snippet gives result as follows.

import ballerina/io;

public function main() {
    map<string> m = {name:"John", city:"London"};
    io:println(m.toBalString()); // {"name":"John","city":"London"}

    string str = string `{"name":"John","city":"London"}`;
    anydata|error ans = str.fromBalString(); // okay

    str = string `{"name":"John", "city":"London"}`;
    ans = str.fromBalString(); // okay

    str = string `{name:"John",city:"London"}`;
    ans = str.fromBalString(); // error, because the keys are not quoted
}

Closing the issue because further clarification is not needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant