-
Notifications
You must be signed in to change notification settings - Fork 0
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(spring): add support for the unwind aggregation stage INTELLIJ-176 #120
Merged
+252
−9
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the method name is a little confusing but the idea of this method
isSuitableForFieldAutoComplete
is for parsers to check and confirm, based on their understanding, if the provided method call is a good target for autocompleter to add field autocomplete.So it will most likely have the same logic as canParse but might also differ (for example it does differ in project parser). In this case, I think we can safely defer to canParse.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah cool, I'll fix it then! I think eventually we will need to add more information to this method then, because not all arguments in a method call expression are suitable for autocomplete. But this is something we can do later.