-
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.
chore: refactor aggregation parsing logic into dedicated stage based …
…parsers (#117)
- Loading branch information
1 parent
5afd7c9
commit fda709b
Showing
8 changed files
with
148 additions
and
76 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
69 changes: 69 additions & 0 deletions
69
...a/src/main/kotlin/com/mongodb/jbplugin/dialects/springcriteria/AggregationStagesParser.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,69 @@ | ||
package com.mongodb.jbplugin.dialects.springcriteria | ||
|
||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.PsiMethod | ||
import com.intellij.psi.PsiMethodCallExpression | ||
import com.mongodb.jbplugin.dialects.javadriver.glossary.fuzzyResolveMethod | ||
import com.mongodb.jbplugin.dialects.javadriver.glossary.resolveToMethodCallExpression | ||
import com.mongodb.jbplugin.dialects.springcriteria.aggregationstageparsers.MatchStageParser | ||
import com.mongodb.jbplugin.mql.Node | ||
|
||
/** | ||
* Parser for parsing supported patterns of writing an aggregation pipeline. | ||
* Supported patterns for writing aggregation calls are the following: | ||
* 1. MongoTemplate.aggregate() | ||
* 2. MongoTemplate.aggregateStream() | ||
* | ||
* The AggregationParser concerns itself only with parsing the aggregation related semantics and | ||
* leave the rest as a responsibility for the composing unit. | ||
*/ | ||
class AggregationStagesParser(private val matchStageParser: MatchStageParser) { | ||
private fun isStageCall(stageCallMethod: PsiMethod): Boolean { | ||
return matchStageParser.canParse(stageCallMethod) | ||
} | ||
|
||
private fun parseAggregationStages( | ||
newAggregationCall: PsiMethodCallExpression | ||
): List<Node<PsiElement>> { | ||
val newAggregationCallArguments = newAggregationCall.argumentList.expressions | ||
val resolvedStageCalls = newAggregationCallArguments.mapNotNull { stageCallExpression -> | ||
stageCallExpression.resolveToMethodCallExpression { _, stageCallMethod -> | ||
isStageCall(stageCallMethod) | ||
} | ||
} | ||
return resolvedStageCalls.map { stageCall -> | ||
val stageCallMethod = stageCall.fuzzyResolveMethod() ?: return@map Node( | ||
source = stageCall, | ||
components = emptyList() | ||
) | ||
|
||
if (matchStageParser.canParse(stageCallMethod)) { | ||
matchStageParser.parse(stageCall) | ||
} else { | ||
Node( | ||
source = stageCall, | ||
components = emptyList() | ||
) | ||
} | ||
} | ||
} | ||
|
||
fun parse(aggregateRootCall: PsiMethodCallExpression): List<Node<PsiElement>> { | ||
val aggregateRootCallArguments = aggregateRootCall.argumentList.expressions | ||
|
||
val newAggregationCallExpression = aggregateRootCallArguments.getOrNull(0) | ||
?: return emptyList() | ||
|
||
// This is the call to Aggregation.newAggregation method which is generally the first | ||
// argument to the root aggregate call. All the aggregation stages are to be found as | ||
// the argument to this method call. | ||
val newAggregationCall = newAggregationCallExpression.resolveToMethodCallExpression { | ||
_, | ||
method | ||
-> | ||
method.name == "newAggregation" | ||
} ?: return emptyList() | ||
|
||
return parseAggregationStages(newAggregationCall) | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
.../com/mongodb/jbplugin/dialects/springcriteria/aggregationstageparsers/MatchStageParser.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,39 @@ | ||
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.springcriteria.AGGREGATE_FQN | ||
import com.mongodb.jbplugin.mql.Node | ||
import com.mongodb.jbplugin.mql.components.HasFilter | ||
import com.mongodb.jbplugin.mql.components.Name | ||
import com.mongodb.jbplugin.mql.components.Named | ||
|
||
class MatchStageParser( | ||
private val parseFilters: (PsiElement) -> List<Node<PsiElement>> | ||
) : StageParser { | ||
private fun createMatchStageNode( | ||
source: PsiElement, | ||
filters: List<Node<PsiElement>> = emptyList() | ||
) = Node( | ||
source = source, | ||
components = listOf( | ||
Named(Name.MATCH), | ||
HasFilter(filters), | ||
) | ||
) | ||
|
||
override fun canParse(stageCallMethod: PsiMethod): Boolean { | ||
return stageCallMethod.containingClass?.qualifiedName == AGGREGATE_FQN && | ||
stageCallMethod.name == "match" | ||
} | ||
|
||
override fun parse(stageCall: PsiMethodCallExpression): Node<PsiElement> { | ||
val filterExpression = stageCall.argumentList.expressions.getOrNull(0) | ||
?: return createMatchStageNode(source = stageCall) | ||
return createMatchStageNode( | ||
source = stageCall, | ||
filters = parseFilters(filterExpression) | ||
) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...otlin/com/mongodb/jbplugin/dialects/springcriteria/aggregationstageparsers/StageParser.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,11 @@ | ||
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.mql.Node | ||
|
||
interface StageParser { | ||
fun canParse(stageCallMethod: PsiMethod): Boolean | ||
fun parse(stageCall: PsiMethodCallExpression): Node<PsiElement> | ||
} |
11 changes: 2 additions & 9 deletions
11
...ggregationparser/AggregationParserTest.kt → ...ngcriteria/AggregationStagesParserTest.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
2 changes: 1 addition & 1 deletion
2
...aggregationparser/MatchStageParserTest.kt → ...ationstageparsers/MatchStageParserTest.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