-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
175 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...com/mongodb/jbplugin/dialects/springcriteria/aggregationstageparsers/UnwindStageParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.mongodb.jbplugin.dialects.springcriteria.aggregationstageparsers | ||
|
||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiMethod | ||
import com.intellij.psi.PsiMethodCallExpression | ||
import com.mongodb.jbplugin.dialects.javadriver.glossary.tryToResolveAsConstantString | ||
import com.mongodb.jbplugin.dialects.springcriteria.AGGREGATE_FQN | ||
import com.mongodb.jbplugin.mql.Component | ||
import com.mongodb.jbplugin.mql.Node | ||
import com.mongodb.jbplugin.mql.components.HasFieldReference | ||
import com.mongodb.jbplugin.mql.components.Name | ||
import com.mongodb.jbplugin.mql.components.Named | ||
|
||
class UnwindStageParser : StageParser { | ||
override fun isSuitableForFieldAutoComplete( | ||
methodCall: PsiMethodCallExpression, | ||
method: PsiMethod | ||
) = true | ||
|
||
override fun canParse(stageCallMethod: PsiMethod): Boolean { | ||
val owningClassFqn = stageCallMethod.containingClass?.qualifiedName ?: return false | ||
return owningClassFqn == AGGREGATE_FQN && stageCallMethod.name == "unwind" | ||
} | ||
|
||
override fun parse(stageCall: PsiMethodCallExpression): Node<PsiElement> { | ||
val psiField = | ||
stageCall.argumentList.expressions.getOrNull(0) ?: return unwindNode(stageCall) | ||
|
||
val referencedField = psiField.tryToResolveAsConstantString() | ||
?: return unwindNode(stageCall) | ||
|
||
return unwindNode( | ||
stageCall, | ||
HasFieldReference(HasFieldReference.FromSchema(psiField, referencedField)) | ||
) | ||
} | ||
|
||
private fun unwindNode(stageCall: PsiMethodCallExpression, vararg additionalComponents: Component): Node<PsiElement> = | ||
Node(stageCall, listOf(Named(Name.UNWIND)) + additionalComponents) | ||
} |
123 changes: 123 additions & 0 deletions
123
...mongodb/jbplugin/dialects/springcriteria/aggregationstageparsers/UnwindStageParserTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package com.mongodb.jbplugin.dialects.springcriteria.aggregationstageparsers | ||
|
||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiFile | ||
import com.mongodb.jbplugin.dialects.springcriteria.IntegrationTest | ||
import com.mongodb.jbplugin.dialects.springcriteria.ParsingTest | ||
import com.mongodb.jbplugin.dialects.springcriteria.SpringCriteriaDialectParser | ||
import com.mongodb.jbplugin.dialects.springcriteria.assert | ||
import com.mongodb.jbplugin.dialects.springcriteria.collection | ||
import com.mongodb.jbplugin.dialects.springcriteria.component | ||
import com.mongodb.jbplugin.dialects.springcriteria.field | ||
import com.mongodb.jbplugin.dialects.springcriteria.getQueryAtMethod | ||
import com.mongodb.jbplugin.dialects.springcriteria.stageN | ||
import com.mongodb.jbplugin.mql.components.HasCollectionReference | ||
import com.mongodb.jbplugin.mql.components.HasFieldReference | ||
import com.mongodb.jbplugin.mql.components.HasSourceDialect | ||
import com.mongodb.jbplugin.mql.components.IsCommand | ||
import com.mongodb.jbplugin.mql.components.Name | ||
import org.junit.jupiter.api.Assertions.assertEquals | ||
|
||
@IntegrationTest | ||
class UnwindStageParserTest { | ||
@ParsingTest( | ||
fileName = "Book.java", | ||
""" | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.aggregation.Aggregation; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
import java.util.List; | ||
@Document | ||
record Book() {} | ||
class Repository { | ||
private final MongoTemplate template; | ||
public Repository(MongoTemplate template) { | ||
this.template = template; | ||
} | ||
public AggregationResults<Book> allReleasedBooks() { | ||
return template.aggregate( | ||
Aggregation.newAggregation( | ||
Aggregation.unwind("author") | ||
), | ||
Book.class, | ||
Book.class | ||
); | ||
} | ||
} | ||
""" | ||
) | ||
fun `should be able to parse an unwind stage with a literal field name`(psiFile: PsiFile) { | ||
val query = psiFile.getQueryAtMethod("Repository", "allReleasedBooks") | ||
SpringCriteriaDialectParser.parse(query).assert(IsCommand.CommandType.AGGREGATE) { | ||
component<HasSourceDialect> { | ||
assertEquals(HasSourceDialect.DialectName.SPRING_CRITERIA, name) | ||
} | ||
|
||
collection<HasCollectionReference.OnlyCollection<PsiElement>> { | ||
assertEquals("book", collection) | ||
} | ||
|
||
stageN(0, Name.UNWIND) { | ||
field<HasFieldReference.FromSchema<PsiElement>> { | ||
assertEquals("author", fieldName) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@ParsingTest( | ||
fileName = "Book.java", | ||
""" | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.aggregation.Aggregation; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
import java.util.List; | ||
@Document | ||
record Book() {} | ||
class Repository { | ||
private static final String AUTHOR = "author"; | ||
private final MongoTemplate template; | ||
public Repository(MongoTemplate template) { | ||
this.template = template; | ||
} | ||
public AggregationResults<Book> allReleasedBooks() { | ||
return template.aggregate( | ||
Aggregation.newAggregation( | ||
Aggregation.unwind(AUTHOR) | ||
), | ||
Book.class, | ||
Book.class | ||
); | ||
} | ||
} | ||
""" | ||
) | ||
fun `should be able to parse an unwind stage with a constant field name`(psiFile: PsiFile) { | ||
val query = psiFile.getQueryAtMethod("Repository", "allReleasedBooks") | ||
SpringCriteriaDialectParser.parse(query).assert(IsCommand.CommandType.AGGREGATE) { | ||
component<HasSourceDialect> { | ||
assertEquals(HasSourceDialect.DialectName.SPRING_CRITERIA, name) | ||
} | ||
|
||
collection<HasCollectionReference.OnlyCollection<PsiElement>> { | ||
assertEquals("book", collection) | ||
} | ||
|
||
stageN(0, Name.UNWIND) { | ||
field<HasFieldReference.FromSchema<PsiElement>> { | ||
assertEquals("author", fieldName) | ||
} | ||
} | ||
} | ||
} | ||
} |