Skip to content

Commit

Permalink
Merge pull request #41799 from Nadeeshan96/mas-frombalstring-41700
Browse files Browse the repository at this point in the history
Fix parsing map keys in `fromBalString` method
  • Loading branch information
HindujaB authored Dec 7, 2023
2 parents a2d992e + 985e312 commit d1fbfb9
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.ballerina.runtime.internal;

import io.ballerina.runtime.api.TypeTags;
import io.ballerina.runtime.api.creators.ErrorCreator;
import io.ballerina.runtime.api.creators.TypeCreator;
import io.ballerina.runtime.api.creators.ValueCreator;
import io.ballerina.runtime.api.types.MapType;
Expand All @@ -41,6 +42,7 @@
import java.util.Set;

import static io.ballerina.runtime.api.PredefinedTypes.TYPE_ANYDATA;
import static io.ballerina.runtime.api.utils.StringUtils.fromString;
import static io.ballerina.runtime.internal.util.StringUtils.parseExpressionStringVal;

/**
Expand Down Expand Up @@ -162,7 +164,7 @@ public static Object parseMapExpressionStringValue(String exprValue, BLink paren
break;
}
}
String key = e.substring(1, colonIndex - 1);
String key = getMapKey(e, colonIndex);
String value = e.substring(colonIndex + 1);
Object val = parseExpressionStringVal(value, node);
eleMap.put(StringUtils.fromString(key), val);
Expand All @@ -185,6 +187,17 @@ public static Object parseMapExpressionStringValue(String exprValue, BLink paren
}
}

private static String getMapKey(String e, int colonIndex) {
String key = e.substring(0, colonIndex).trim();
if (key.startsWith("\"") && key.endsWith("\"")) {
key = key.substring(1, key.length() - 1);
} else {
throw ErrorCreator.createError(fromString("invalid expression style string value: " +
"the map keys are not enclosed with '\"'."));
}
return key;
}

/**
* Create a table from string literal.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,107 @@ function testMapFromBalString() {
}

assert(result3, mapVal3);
}

string mapExpString = string `{"name":"John","city":"London"}`;
anydata|error output = mapExpString.fromBalString();
assert(output is error, false);
if (output is anydata) {
assert(output, {"name": "John", "city": "London"});
}

string mapExpString1 = string `{"key1": [ 1, 2, "three", ["",4,{"key1":"val1", "key2": 2}]], "key2" : `;
string mapExpString2 = string `{"key1": [1, 2, "three", ["",4,{"key1":"val1", "key2": 2}]], "key2": "London"}}`;
mapExpString = mapExpString1 + mapExpString2;
output = mapExpString.fromBalString();
assert(output is error, false);
if (output is anydata) {
assert(output, {"key1":[1,2,"three",["",4,{"key1":"val1","key2":2}]],
"key2":{"key1":[1,2,"three",["",4,{"key1":"val1","key2":2}]],"key2":"London"}});
}

mapExpString = string `{"name":"John",city:"London"}`;
output = mapExpString.fromBalString();
assert(output is error, true);
if (output is error) {
assert("invalid expression style string value: the map keys are not enclosed with '\"'.",
<string>checkpanic output.detail()["message"]);
}

mapExpString = string `{name:"John",city:"London"}`;
output = mapExpString.fromBalString();
assert(output is error, true);
if (output is error) {
assert("invalid expression style string value: the map keys are not enclosed with '\"'.",
<string>checkpanic output.detail()["message"]);
}

mapExpString = string `{" name ":" John"," city ":" London "}`;
output = mapExpString.fromBalString();
assert(output is error, false);
if (output is anydata) {
assert(output, {" name ": " John", " city ": " London "});
}

mapExpString = string ` {"name" : "John" , "city" : "London"} `;
output = mapExpString.fromBalString();
assert(output is error, false);
if (output is anydata) {
assert(output, {"name": "John", "city": "London"});
}

mapExpString = string `
{ "name" :
"John" ,
"city" : "London" }`;
output = mapExpString.fromBalString();
assert(output is error, false);
if (output is anydata) {
assert(output, {"name": "John", "city": "London"});
}

mapExpString = string `
{ "intval" :
3 ,
"floatval" : 12.55 }`;
output = mapExpString.fromBalString();
assert(output is error, false);
if (output is anydata) {
assert(output, {intval: 3, floatval: 12.55});
}

mapExpString = string `
{ "name" :
"John" ,
city : "London" }`;
output = mapExpString.fromBalString();
assert(output is error, true);
if (output is error) {
assert("invalid expression style string value: the map keys are not enclosed with '\"'.",
<string>checkpanic output.detail()["message"]);
}

mapExpString = string `
{ "name" :
3 ,
city" : 12.55 }`;
output = mapExpString.fromBalString();
assert(output is error, true);
if (output is error) {
assert("invalid expression style string value: the map keys are not enclosed with '\"'.",
<string>checkpanic output.detail()["message"]);
}

mapExpString = string `
{ name" :
3 ,
city" : 12.55 }`;
output = mapExpString.fromBalString();
assert(output is error, true);
if (output is error) {
assert("invalid expression style string value: the map keys are not enclosed with '\"'.",
<string>checkpanic output.detail()["message"]);
}
}

function testTableFromBalString() {
string s1 = "table key(name) [{\"id\":1,\"name\":\"Mary\",\"grade\":12}," +
Expand Down

0 comments on commit d1fbfb9

Please sign in to comment.