Skip to content

Commit

Permalink
chore: fix issue with aggregate.first
Browse files Browse the repository at this point in the history
  • Loading branch information
kmruiz committed Nov 14, 2024
1 parent f9e65bd commit d4afb0f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ object JavaDriverDialectParser : DialectParser<PsiElement> {
)
.flatMap(methodCallChain()).acceptAnyError()
.firstMatching(
resolveFromMethodName.flatMap(equalsTo(IsCommand.CommandType.AGGREGATE)).matches()
resolveFromMethodName.filter { it == IsCommand.CommandType.AGGREGATE }.matches()
).flatMap(resolveFromMethodName)
.acceptAnyError()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiReturnStatement
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.parentsOfType
import com.mongodb.jbplugin.dialects.javadriver.glossary.collectTypeUntil
import com.mongodb.jbplugin.dialects.javadriver.glossary.findAllChildrenOfType
import com.mongodb.jbplugin.dialects.javadriver.glossary.fuzzyResolveMethod
Expand Down Expand Up @@ -85,7 +86,8 @@ fun methodCallChain(): Parser<PsiMethodCallExpression, Any, List<PsiMethodCallEx
// by reversing the list, we get the "deepest" (closest) levels first
val allCallExpressions = input.findAllChildrenOfType(
PsiMethodCallExpression::class.java
).reversed()
).reversed() + input.parentsOfType<PsiMethodCallExpression>()

Either.right(allCallExpressions)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public final class Repository {
// Returns the value of the entire return expression
val queryWithIterableCall = psiFile.getQueryAtMethod("Repository", "findBookById")
// Now the candidate for query attaches at the top element
assertTrue(JavaDriverDialectParser.isCandidateForQuery(queryWithIterableCall))
assertFalse(JavaDriverDialectParser.isCandidateForQuery(queryWithIterableCall))

val actualQuery = PsiTreeUtil
.findChildrenOfType(queryWithIterableCall, PsiMethodCallExpression::class.java)
Expand Down Expand Up @@ -1652,4 +1652,39 @@ public final class Repository {
val command = parsedQuery.component<IsCommand>()
assertEquals(IsCommand.CommandType.FIND_ONE, command?.type)
}

@ParsingTest(
fileName = "Repository.java",
value = """
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClient;
import org.bson.Document;
import java.util.List;
import static com.mongodb.client.model.Filters.*;
import static com.mongodb.client.model.Updates.*;
public class Repository {
private final MongoClient client;
public Repository(MongoClient client) {
this.client = client;
}
private FindIterable<Document> findFantasyBooks() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.aggregate(List.of()).first();
}
}
""",
)
fun `understands aggregate first as an AGGREGATE command`(
psiFile: PsiFile
) {
val query = psiFile.getQueryAtMethod("Repository", "findFantasyBooks")
val parsedQuery = JavaDriverDialect.parser.parse(query)

val command = parsedQuery.component<IsCommand>()
assertEquals(IsCommand.CommandType.AGGREGATE, command?.type)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ inline fun <reified II, I, E, O> Parser<I, E, O>.inputAs(): Parser<II, E, O> {
fun <I, E, O> Parser<I, E, O>.debug(message: String): Parser<I, E, O> {
return { input ->
val result = this(input)
println("$input > $result >> $message ")
println(":: $message -> $input > $result ")
result
}
}
Expand Down

0 comments on commit d4afb0f

Please sign in to comment.