-
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 into dedicated aggregation parsers
This PR splits the current aggregation parsing logic into dedicated stage based parsers.
- Loading branch information
1 parent
5afd7c9
commit 49b2578
Showing
6 changed files
with
134 additions
and
67 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.aggregationparsers.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.isMatchStageCall(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.isMatchStageCall(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
38 changes: 38 additions & 0 deletions
38
...otlin/com/mongodb/jbplugin/dialects/springcriteria/aggregationparsers/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,38 @@ | ||
package com.mongodb.jbplugin.dialects.springcriteria.aggregationparsers | ||
|
||
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>>) { | ||
private fun createMatchStageNode( | ||
source: PsiElement, | ||
filters: List<Node<PsiElement>> = emptyList() | ||
) = Node( | ||
source = source, | ||
components = listOf( | ||
Named(Name.MATCH), | ||
HasFilter(filters), | ||
) | ||
) | ||
|
||
fun parse(matchStageCall: PsiMethodCallExpression): Node<PsiElement> { | ||
val filterExpression = matchStageCall.argumentList.expressions.getOrNull(0) | ||
?: return createMatchStageNode(source = matchStageCall) | ||
return createMatchStageNode( | ||
source = matchStageCall, | ||
filters = parseFilters(filterExpression) | ||
) | ||
} | ||
|
||
companion object { | ||
fun isMatchStageCall(method: PsiMethod): Boolean { | ||
return method.containingClass?.qualifiedName != AGGREGATE_FQN && method.name == "match" | ||
} | ||
} | ||
} |
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