Skip to content

Commit

Permalink
chore: keep parsing also the unknown value references
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshusinghs committed Nov 22, 2024
1 parent 9f986dd commit 6068a46
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -577,34 +577,29 @@ object JavaDriverDialectParser : DialectParser<PsiElement> {
private fun parseAddFieldsArgument(fieldExpression: PsiNewExpression): Node<PsiElement>? {
val expressionArguments = fieldExpression.argumentList?.expressions
val fieldNameExpression = expressionArguments?.getOrNull(0)
?: return null
val fieldName = fieldNameExpression.tryToResolveAsConstantString()
?: return null

val valueExpression = expressionArguments.getOrNull(1)
?: return null
// This needs to be changed when we start parsing also the expression
// driven values
val constantValue = valueExpression.tryToResolveAsConstant().takeIf {
it.first
}?.second ?: return null
val fieldReference = fieldNameExpression?.tryToResolveAsConstantString()?.let {
HasFieldReference.Computed(
source = fieldNameExpression,
fieldName = it,
)
} ?: HasFieldReference.Unknown

val valueExpression = expressionArguments?.getOrNull(1)
val valueReference = valueExpression?.tryToResolveAsConstantString()?.let {
// We currently only support parsing a constant value which is why we will encode
// anything else, not parseable, as an Unknown.
HasValueReference.Constant(
source = valueExpression,
value = it,
type = it.javaClass.toBsonType(),
)
} ?: HasValueReference.Unknown

return Node(
source = fieldExpression,
components = listOf(
HasFieldReference(
reference = HasFieldReference.Computed(
source = fieldNameExpression,
fieldName = fieldName,
)
),
HasValueReference(
reference = HasValueReference.Constant(
source = valueExpression,
value = constantValue,
type = constantValue.javaClass.toBsonType()
)
)
HasFieldReference(fieldReference),
HasValueReference(valueReference)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.mongodb.jbplugin.mql.components.HasValueReference
import com.mongodb.jbplugin.mql.components.Name
import com.mongodb.jbplugin.mql.components.Named
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull

@IntegrationTest
class AddFieldsParserTest {
Expand Down Expand Up @@ -602,7 +603,7 @@ public final class Aggregation {
}
"""
)
fun `should skip parsing fields where value is an expression`(psiFile: PsiFile) {
fun `should parse Fields call with an expression as an Unknown value`(psiFile: PsiFile) {
val aggregate = psiFile.getQueryAtMethod("Aggregation", "getAllBookTitles")
val parsedAggregate = JavaDriverDialect.parser.parse(aggregate)
val hasAggregation = parsedAggregate.component<HasAggregation<PsiElement>>()
Expand All @@ -614,7 +615,7 @@ public final class Aggregation {
assertEquals(Name.ADD_FIELDS, named.name)

val addedFields = addFieldsStageNode.component<HasAddedFields<PsiElement>>()!!
assertEquals(2, addedFields.children.size)
assertEquals(3, addedFields.children.size)

val field1Node = addedFields.children[0]
val field1FieldReference = (field1Node.component<HasFieldReference<PsiElement>>()!!.reference as HasFieldReference.Computed)
Expand All @@ -625,10 +626,16 @@ public final class Aggregation {

val field2Node = addedFields.children[1]
val field2FieldReference = (field2Node.component<HasFieldReference<PsiElement>>()!!.reference as HasFieldReference.Computed)
assertEquals("field3", field2FieldReference.fieldName)
val field2ValueReference = (field2Node.component<HasValueReference<PsiElement>>()!!.reference as HasValueReference.Constant)
assertEquals("Value3", field2ValueReference.value)
assertEquals(BsonAnyOf(BsonString, BsonNull), field2ValueReference.type)
assertEquals("field2", field2FieldReference.fieldName)
val field2ValueReference = field2Node.component<HasValueReference<PsiElement>>()!!.reference
assertNotNull(field2ValueReference as? HasValueReference.Unknown)

val field3Node = addedFields.children[2]
val field3FieldReference = (field3Node.component<HasFieldReference<PsiElement>>()!!.reference as HasFieldReference.Computed)
assertEquals("field3", field3FieldReference.fieldName)
val field3ValueReference = (field3Node.component<HasValueReference<PsiElement>>()!!.reference as HasValueReference.Constant)
assertEquals("Value3", field3ValueReference.value)
assertEquals(BsonAnyOf(BsonString, BsonNull), field3ValueReference.type)
}

companion object {
Expand Down

0 comments on commit 6068a46

Please sign in to comment.