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

Provide completions after a variable resource path parameter in a resource access action #41628

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -3823,22 +3823,22 @@ public void visit(BLangInvocation.BLangResourceAccessInvocation resourceAccessIn
}

if (resourceFunctions.size() == 0) {
dlog.error(resourceAccessInvocation.resourceAccessPathSegments.pos, DiagnosticErrorCode.UNDEFINED_RESOURCE,
lhsExprType);
data.resultType = symTable.semanticError;
handleResourceAccessError(resourceAccessInvocation.resourceAccessPathSegments,
resourceAccessInvocation.resourceAccessPathSegments.pos, DiagnosticErrorCode.UNDEFINED_RESOURCE,
data, lhsExprType);
return;
}

// Filter the resource methods in the list by resource access method name
resourceFunctions.removeIf(func -> !func.accessor.value.equals(resourceAccessInvocation.name.value));
int targetResourceFuncCount = resourceFunctions.size();
if (targetResourceFuncCount == 0) {
dlog.error(resourceAccessInvocation.name.pos,
DiagnosticErrorCode.UNDEFINED_RESOURCE_METHOD, resourceAccessInvocation.name, lhsExprType);
data.resultType = symTable.semanticError;
handleResourceAccessError(resourceAccessInvocation.resourceAccessPathSegments,
resourceAccessInvocation.name.pos, DiagnosticErrorCode.UNDEFINED_RESOURCE_METHOD, data,
resourceAccessInvocation.name, lhsExprType);
} else if (targetResourceFuncCount > 1) {
dlog.error(resourceAccessInvocation.pos, DiagnosticErrorCode.AMBIGUOUS_RESOURCE_ACCESS_NOT_YET_SUPPORTED);
data.resultType = symTable.semanticError;
handleResourceAccessError(resourceAccessInvocation.resourceAccessPathSegments, resourceAccessInvocation.pos,
DiagnosticErrorCode.AMBIGUOUS_RESOURCE_ACCESS_NOT_YET_SUPPORTED, data, lhsExprType);
} else {
BResourceFunction targetResourceFunc = resourceFunctions.get(0);
checkExpr(resourceAccessInvocation.resourceAccessPathSegments,
Expand All @@ -3849,6 +3849,14 @@ public void visit(BLangInvocation.BLangResourceAccessInvocation resourceAccessIn
}
}

private void handleResourceAccessError(BLangListConstructorExpr resourceAccessPathSegments,
Location diagnosticLocation, DiagnosticErrorCode errorCode,
AnalyzerData data, Object... dlogArgs) {
checkExpr(resourceAccessPathSegments, data);
dlog.error(diagnosticLocation, errorCode, dlogArgs);
data.resultType = symTable.semanticError;
}

private BTupleType getResourcePathType(List<BResourcePathSegmentSymbol> pathSegmentSymbols) {
BType restType = null;
int pathSegmentCount = pathSegmentSymbols.size();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"position": {
"line": 7,
"character": 25
},
"source": "action_node_context/source/client_resource_access_action_source37.bal",
"description": "Test completions after a variable resource path",
"items": [
{
"label": "/third",
"kind": "Function",
"detail": "()",
"documentation": {
"right": {
"kind": "markdown",
"value": "**Package:** _._ \n \n \n**Params** \n- `string` second"
}
},
"sortText": "C",
"filterText": "third|get",
"insertText": "/third;",
"insertTextFormat": "Snippet",
"additionalTextEdits": [
{
"range": {
"start": {
"line": 7,
"character": 24
},
"end": {
"line": 7,
"character": 25
}
},
"newText": ""
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
client class MyClient {
resource function get first/[string second]/third() {}
}

public function main() {
string someVar = "check";
MyClient cl = new;
cl->/first/[someVar]/;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

import java.util.Optional;

import static io.ballerina.compiler.api.symbols.TypeDescKind.INT;
import static io.ballerina.compiler.api.symbols.TypeDescKind.STRING;
import static io.ballerina.compiler.api.symbols.TypeDescKind.TYPE_REFERENCE;
import static io.ballerina.compiler.api.symbols.TypeDescKind.UNION;
import static org.testng.Assert.assertEquals;
Expand Down Expand Up @@ -60,6 +62,14 @@ public Object[][] getPos() {
return new Object[][]{
{42, 23, 42, 54, UNION},
{42, 23, 42, 32, TYPE_REFERENCE},
{110, 9, 110, 12, STRING},
{111, 9, 111, 14, STRING},
{111, 17, 111, 25, STRING},
{112, 17, 111, 19, STRING},
{113, 15, 113, 19, INT},
{114, 15, 114, 16, INT},
{115, 9, 115, 14, STRING},
{115, 17, 115, 24, STRING},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,27 @@ public function testPathSegmentOfAmbiguousResourceFunction() {
Bam bam = new;
bam->/path1/path2.post();
}

client class Resc {
resource function post user() {}

resource function post [string name]() {}

resource function get sports/[string name]() {}

resource function get pets/[int id]() {}

resource function get sports/[string name]/info () {}
}

function testTypeOfResourceSegments() {
Resc cl = new;
string myString = "";
int myInt = 0;
cl->/user.post();
cl->/sports/[myString]();
cl->/sports/["A"]();
cl->/pets/[myInt]();
cl->/pets/[1]();
cl->/sports/[myString]/
}
Loading