Skip to content

Commit

Permalink
Merge pull request #41075 from HindujaB/fix-param-default-value-encoding
Browse files Browse the repository at this point in the history
Fix parameter default value function getting null
  • Loading branch information
warunalakshitha authored Aug 10, 2023
2 parents 2713401 + 846c39c commit 21e8035
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,8 @@ private void populateFunctionParameters(MethodVisitor mv, BInvokableTypeSymbol i
} else {
mv.visitInsn(ICONST_0);
}
BInvokableSymbol bInvokableSymbol = invokableSymbol.defaultValues.get(paramSymbol.originalName.value);
BInvokableSymbol bInvokableSymbol = invokableSymbol.defaultValues.get(
Utils.decodeIdentifier(paramSymbol.name.value));
if (bInvokableSymbol == null) {
mv.visitInsn(ACONST_NULL);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.wso2.ballerinalang.compiler.desugar;

import io.ballerina.identifier.Utils;
import io.ballerina.tools.diagnostics.Location;
import org.ballerinalang.model.TreeBuilder;
import org.ballerinalang.model.elements.Flag;
Expand Down Expand Up @@ -555,7 +556,7 @@ private BLangExpression createClosureForDefaultValue(String closureName, String
env.enclPkg.symbol.scope.define(function.symbol.name, function.symbol);
env.enclPkg.functions.add(function);
env.enclPkg.topLevelNodes.add(function);
symbol.defaultValues.put(paramName, varSymbol);
symbol.defaultValues.put(Utils.unescapeBallerina(paramName), varSymbol);
return returnStmt.expr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.wso2.ballerinalang.compiler.desugar;

import io.ballerina.identifier.Utils;
import io.ballerina.runtime.api.constants.RuntimeConstants;
import io.ballerina.tools.diagnostics.Location;
import org.apache.commons.lang3.StringEscapeUtils;
Expand Down Expand Up @@ -6475,7 +6476,7 @@ private BLangStatementExpression createStmtExpr(BLangInvocation invocation) {
continue;
}

BInvokableSymbol invokableSymbol = defaultValues.get(paramName);
BInvokableSymbol invokableSymbol = defaultValues.get(Utils.unescapeBallerina(paramName));
BLangInvocation closureInvocation = getInvocation(invokableSymbol);
for (int m = 0; m < invokableSymbol.params.size(); m++) {
String langLibFuncParam = invokableSymbol.params.get(m).name.value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,13 @@ public static BString getParameterNameFromResource(BTypedesc type) {
return StringUtils.fromString(parameter.name);
}

public static BString getParameterDefaultFunctionNameFromResource(BTypedesc type) {
BServiceType serviceType = (BServiceType) type.getDescribingType();
ResourceMethodType resourceMethod = serviceType.getResourceMethods()[1];
Parameter parameter = resourceMethod.getParameters()[0];
return StringUtils.fromString(parameter.defaultFunctionName);
}

public static BArray getParamNamesFromObjectInit(BObject object) {
ObjectType objectType = object.getType();
MethodType initMethodtype = objectType.getInitMethod();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ service class AccountService {
email: "john.doe@email.com",
phone: "1234567890"
};
resource function get packages(int? 'limit = ()) returns json => {
'limit: 'limit,
body: "hello"
};
}

public function main() {
Expand All @@ -43,6 +47,8 @@ public function main() {
function testFunctionParameters() {
test:assertEquals(getParameterName(function(string account\-id) {}), "account-id");
test:assertEquals(getParameterNameFromResource(AccountService), "account-id");
test:assertEquals(getParameterDefaultFunctionNameFromResource(AccountService),
"$AccountService_$get$packages_limit");
}

function testIdentifierDecoding() {
Expand Down Expand Up @@ -91,3 +97,7 @@ function getParameterName(function f) returns string = @java:Method {
function getParameterNameFromResource(typedesc<any> serviceObj) returns string = @java:Method {
'class: "org.ballerinalang.nativeimpl.jvm.runtime.api.tests.Values"
} external;

function getParameterDefaultFunctionNameFromResource(typedesc<any> serviceObj) returns string = @java:Method {
'class: "org.ballerinalang.nativeimpl.jvm.runtime.api.tests.Values"
} external;

0 comments on commit 21e8035

Please sign in to comment.