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

feat: add support for parsing Aggregates.unwind stage INTELLIJ-151 #100

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -805,4 +805,67 @@ public class Repository {
},
)
}

@ParsingTest(
fileName = "Repository.java",
value = """
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Sorts;
import org.bson.Document;
import org.bson.types.ObjectId;
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;
}

public void exampleFind() {
client.getDatabase("myDatabase").getCollection("myCollection")
.aggregate(List.of(
Aggregates.unwind("<caret>")
));
}
}
""",
)
fun `should autocomplete fields from the current namespace in an Aggregates#unwind stage`(
fixture: CodeInsightTestFixture,
) {
fixture.specifyDialect(JavaDriverDialect)

val (dataSource, readModelProvider) = fixture.setupConnection()
val namespace = Namespace("myDatabase", "myCollection")

`when`(
readModelProvider.slice(eq(dataSource), eq(GetCollectionSchema.Slice(namespace)))
).thenReturn(
GetCollectionSchema(
CollectionSchema(
namespace,
BsonObject(
mapOf(
"myField" to BsonString,
),
),
),
),
)

val elements = fixture.completeBasic()

assertTrue(
elements.containsElements {
it.lookupString == "myField"
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -818,4 +818,68 @@ public class Repository {
},
)
}

@ParsingTest(
fileName = "Repository.java",
value = """
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Sorts;
import org.bson.Document;
import org.bson.types.ObjectId;
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;
}

public void exampleFind() {
client.getDatabase("myDatabase").getCollection("myCollection")
.aggregate(List.of(
Aggregates.unwind(<caret>)
));
}
}
""",
)
fun `should autocomplete fields from the current namespace in an Aggregates#unwind stage`(
fixture: CodeInsightTestFixture,
) {
fixture.specifyDialect(JavaDriverDialect)

val (dataSource, readModelProvider) = fixture.setupConnection()
val namespace = Namespace("myDatabase", "myCollection")

`when`(
readModelProvider.slice(eq(dataSource), eq(GetCollectionSchema.Slice(namespace)))
).thenReturn(
GetCollectionSchema(
CollectionSchema(
namespace,
BsonObject(
mapOf(
"myField" to BsonString,
),
),
),
),
)

fixture.type('"')
val elements = fixture.completeBasic()

assertTrue(
elements.containsElements {
it.lookupString == "myField"
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.mongodb.jbplugin.fixtures.setupConnection
import com.mongodb.jbplugin.fixtures.specifyDialect
import com.mongodb.jbplugin.mql.BsonDouble
import com.mongodb.jbplugin.mql.BsonObject
import com.mongodb.jbplugin.mql.BsonString
import com.mongodb.jbplugin.mql.CollectionSchema
import com.mongodb.jbplugin.mql.Namespace
import org.mockito.Mockito.`when`
Expand Down Expand Up @@ -501,4 +502,98 @@ public class Repository {
fixture.enableInspections(FieldCheckInspectionBridge::class.java)
fixture.testHighlighting()
}

@ParsingTest(
fileName = "Repository.java",
value = """
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Sorts;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import java.util.List;
import static com.mongodb.client.model.Filters.*;

public class Repository {
private final MongoClient client;

public Repository(MongoClient client) {
this.client = client;
}

public AggregateIterable<Document> exampleGoodUnwind() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.aggregate(List.of(
Aggregates.unwind(
"${'$'}existingField"
)
));
}

public AggregateIterable<Document> exampleUnwind1() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.aggregate(List.of(
Aggregates.unwind(
<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">"${'$'}nonExistingField"</warning>
)
));
}

public AggregateIterable<Document> exampleUnwind2() {
String fieldName = "${'$'}nonExistingField";
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.aggregate(List.of(
Aggregates.unwind(
<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">fieldName</warning>
)
));
}

private String getField() {
return "${'$'}nonExistingField";
}

public AggregateIterable<Document> exampleUnwind3() {
return client.getDatabase("myDatabase")
.getCollection("myCollection")
.aggregate(List.of(
Aggregates.unwind(
<warning descr="Field \"nonExistingField\" does not exist in collection \"myDatabase.myCollection\"">getField()</warning>
)
));
}
}
""",
)
fun `shows an inspection for Aggregates#unwind call when the field does not exist in the current namespace`(
fixture: CodeInsightTestFixture,
) {
val (dataSource, readModelProvider) = fixture.setupConnection()
fixture.specifyDialect(JavaDriverDialect)

`when`(
readModelProvider.slice(eq(dataSource), any<GetCollectionSchema.Slice>())
).thenReturn(
GetCollectionSchema(
CollectionSchema(
Namespace("myDatabase", "myCollection"),
BsonObject(
mapOf(
"existingField" to BsonString
)
)
)
),
)

fixture.enableInspections(FieldCheckInspectionBridge::class.java)
fixture.testHighlighting()
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package alt.mongodb.javadriver;

import com.mongodb.client.MongoClient;
import com.mongodb.client.model.Aggregates;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Sorts;
import com.mongodb.client.model.*;
import org.bson.Document;

import java.util.ArrayList;
Expand Down Expand Up @@ -64,6 +61,10 @@ public List<Document> queryMoviesByYear(String year) {
Sorts.orderBy(
Sorts.ascending("asd", "qwe")
)
),
Aggregates.unwind(
"asd",
new UnwindOptions()
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,41 @@ object JavaDriverDialectParser : DialectParser<PsiElement> {
)
}

"unwind" -> {
val fieldExpression = stageCall.argumentList.expressions.getOrNull(0)
?: return Node(
source = stageCall,
components = listOf(
Named(Name.UNWIND)
)
)

val fieldName = fieldExpression.tryToResolveAsConstantString()
?: return Node(
source = stageCall,
components = listOf(
Named(Name.UNWIND),
HasFieldReference(
HasFieldReference.Unknown
)
)
)

return Node(
source = stageCall,
components = listOf(
Named(Name.UNWIND),
HasFieldReference(
HasFieldReference.FromSchema(
source = fieldExpression,
fieldName = fieldName.trim('$'),
displayName = fieldName,
)
),
)
)
}

else -> return null
}
}
Expand Down
Loading
Loading