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

Fix let expression usage inside query actions #42068

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -2103,8 +2103,8 @@ public void visit(BLangGroupExpr groupExpr) {

@Override
public void visit(BLangLetExpression letExpr) {
letExpr.expr = rewrite(letExpr.expr);
letExpr.letVarDeclarations.forEach(var -> this.acceptNode((BLangNode) var.definitionNode));
letExpr.expr = rewrite(letExpr.expr);
result = letExpr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public Object[] dataToTestQueryActionOrExpr() {
"testQueryActionOrExpressionWithUnionRecordResultType",
"testQueryActionOrExprWithAnyOrErrResultType",
"testNestedQueryActionOrExprWithClientResourceAccessAction",
"testQueryActionWithQueryExpression"
"testQueryActionWithQueryExpression",
"testQueryActionWithLetExpression"
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -988,11 +988,7 @@ type Student record {
function calGraduationYear(int year) returns int => year + 5;

function getBestStudents() returns any|error {
Student s1 = {firstName: "Martin", lastName: "Sadler", intakeYear: 1990, gpa: 3.5};
Student s2 = {firstName: "Ranjan", lastName: "Fonseka", intakeYear: 2001, gpa: 1.9};
Student s3 = {firstName: "Michelle", lastName: "Guthrie", intakeYear: 2002, gpa: 3.7};
Student s4 = {firstName: "George", lastName: "Fernando", intakeYear: 2005, gpa: 4.0};
Student[] studentList = [s1, s2, s3];
Student[] studentList = getStudents();

return from var student in studentList
where student.gpa >= 2.0
Expand All @@ -1006,6 +1002,37 @@ function testQueryActionOrExprWithAnyOrErrResultType() {
assertTrue(getBestStudents() is record {|string name; string degree; int graduationYear;|}[]);
}

function getStudents() returns Student[] {
return [
{firstName: "Martin", lastName: "Sadler", intakeYear: 1990, gpa: 3.5},
{firstName: "Ranjan", lastName: "Fonseka", intakeYear: 2001, gpa: 1.9},
{firstName: "Michelle", lastName: "Guthrie", intakeYear: 2002, gpa: 3.7},
{firstName: "George", lastName: "Fernando", intakeYear: 2005, gpa: 4.0}
];
}

function testQueryActionWithLetExpression() {
Student[] studentList = getStudents();
float[] actualGpaList = [];
_ = from var {gpa} in studentList
do {
float gpaVal = let var sGpa = gpa in sGpa;
actualGpaList.push(gpaVal);
};
float[] expectedGpaList = [3.5, 1.9, 3.7, 4.0];
assertEquality(expectedGpaList, actualGpaList);

int[] actualValues = [];
int weight = 2;
_ = from var i in [1, 2, 3, 4]
do {
int gpaVal = let var sGpa = i in weight * i;
actualValues.push(gpaVal);
};
int[] expectedValues = [2, 4, 6, 8];
assertEquality(actualValues, expectedValues);
LakshanWeerasinghe marked this conversation as resolved.
Show resolved Hide resolved
}

const ASSERTION_ERROR_REASON = "AssertionError";

function assertEquality(anydata expected, anydata actual) {
Expand Down
Loading