Skip to content

Commit

Permalink
fix: and/or/nor not working with iterable args
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshusinghs committed Jan 7, 2025
1 parent 11050fe commit a058e10
Show file tree
Hide file tree
Showing 3 changed files with 591 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import static com.mongodb.client.model.Filters.*;
Expand All @@ -158,7 +159,8 @@ public class Repository {
this.client = client;
}
public FindIterable<Document> exampleFind() {
// and queries
public FindIterable<Document> inlineAndQuery() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(
Expand All @@ -167,10 +169,102 @@ public class Repository {
)
);
}
public FindIterable<Document> andQueryFromAVariableWithVariableFieldName() {
var fieldName = "nonExistingField";
var andQuery = and(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">fieldName</warning>, "123")
);
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(andQuery);
}
public FindIterable<Document> andQueryFromAMethodCallWithFieldNameFromMethodCall() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(getAndQueryWithFieldNameFromMethodCall());
}
private Bson getAndQueryWithFieldNameFromMethodCall() {
return and(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">getFieldName()</warning>, "123")
);
}
private String getFieldName() {
return "nonExistingField";
}
// or queries
public FindIterable<Document> inlineOrQuery() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(
or(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">"nonExistingField"</warning>, "123")
)
);
}
public FindIterable<Document> orQueryFromAVariableWithVariableFieldName() {
var fieldName = "nonExistingField";
var orQuery = or(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">fieldName</warning>, "123")
);
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(orQuery);
}
public FindIterable<Document> orQueryFromAMethodCallWithFieldNameFromMethodCall() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(getOrQueryWithFieldNameFromMethodCall());
}
private Bson getOrQueryWithFieldNameFromMethodCall() {
return or(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">getFieldName()</warning>, "123")
);
}
// nor queries
public FindIterable<Document> inlineNorQuery() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(
nor(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">"nonExistingField"</warning>, "123")
)
);
}
public FindIterable<Document> norQueryFromAVariableWithVariableFieldName() {
var fieldName = "nonExistingField";
var norQuery = nor(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">fieldName</warning>, "123")
);
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(norQuery);
}
public FindIterable<Document> norQueryFromAMethodCallWithFieldNameFromMethodCall() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.find(getNorQueryWithFieldNameFromMethodCall());
}
private Bson getNorQueryWithFieldNameFromMethodCall() {
return nor(
eq(<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">getFieldName()</warning>, "123")
);
}
}
""",
)
fun `shows an inspection when a field, referenced in a nested $and query, does not exists in the current namespace`(
fun `shows an inspection when a field, referenced in different forms of a nested $and, $or and $nor query, does not exists in the current namespace`(
fixture: CodeInsightTestFixture,
) {
val (dataSource, readModelProvider) = fixture.setupConnection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,19 @@ object JavaDriverDialectParser : DialectParser<PsiElement> {
HasValueReference(valueReference)
),
)
} else if (method.isVarArgs || method.name == "not") {
// Filters.and, Filters.or... are varargs
} else if (method.name == "and" || method.name == "or" || method.name == "nor") {
return Node(
filter,
listOf(
Named(Name.from(method.name)),
HasFilter(
filter.getVarArgsOrIterableArgs()
.mapNotNull { resolveBsonBuilderCall(it, FILTERS_FQN) }
.mapNotNull { parseFilterExpression(it) },
),
),
)
} else if (method.name == "not") {
return Node(
filter,
listOf(
Expand Down
Loading

0 comments on commit a058e10

Please sign in to comment.