Skip to content

Commit

Permalink
[NU-7142] Lift TypingResult for dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Rudnicki committed Nov 14, 2024
1 parent 15a4f9d commit 10ee2df
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

### 1.18.0 (Not released yet)

* [7145](https://github.com/TouK/nussknacker/pull/7145) Lift TypingResult information for dictionaries
* [6944](https://github.com/TouK/nussknacker/pull/6944) Changes around adhoc testing feature
* `test-with-form` button was renamed to `adhoc-testing`
* Improved form validators inside adhoc tests (validation was moved to backend)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,31 @@ import pl.touk.nussknacker.engine.api.definition.{AdditionalVariable => _}
import pl.touk.nussknacker.engine.api.expression.ExpressionTypingInfo
import pl.touk.nussknacker.engine.api.generics.ExpressionParseError
import pl.touk.nussknacker.engine.api.typed.typing
import pl.touk.nussknacker.engine.api.typed.typing.{Typed, TypingResult}
import pl.touk.nussknacker.engine.api.typed.typing.{Typed, TypedClass, TypedObjectWithValue, TypingResult}
import pl.touk.nussknacker.engine.expression.NullExpression
import pl.touk.nussknacker.engine.expression.parse.{CompiledExpression, ExpressionParser, TypedExpression}
import pl.touk.nussknacker.engine.graph.expression.Expression.Language
import pl.touk.nussknacker.engine.graph.expression.{DictKeyWithLabelExpression, Expression}
import pl.touk.nussknacker.engine.spel.SpelExpressionParseError.KeyWithLabelExpressionParsingError
import pl.touk.nussknacker.engine.spel.SpelExpressionParser

case class DictKeyWithLabelExpressionTypingInfo(key: String, label: Option[String], typingResult: TypingResult)
extends ExpressionTypingInfo
import scala.util.Try

case class DictKeyWithLabelExpressionTypingInfo(key: String, label: Option[String], expectedType: TypingResult)
extends ExpressionTypingInfo {

// We should support at least types defined in FragmentParameterValidator#permittedTypesForEditors
override def typingResult: TypingResult = expectedType match {
case clazz: TypedClass if clazz.canBeSubclassOf(Typed[Long]) && Try(key.toLong).toOption.isDefined =>
TypedObjectWithValue(clazz.runtimeObjType, key.toLong)
case clazz: TypedClass if clazz.canBeSubclassOf(Typed[Boolean]) && Try(key.toBoolean).toOption.isDefined =>
TypedObjectWithValue(clazz.runtimeObjType, key.toBoolean)
case clazz: TypedClass if clazz.canBeSubclassOf(Typed[String]) =>
TypedObjectWithValue(clazz.runtimeObjType, key)
case _ => expectedType
}

}

object DictKeyWithLabelExpressionParser extends ExpressionParser {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package pl.touk.nussknacker.engine.language.dictWithLabel

import org.scalatest.Inside.inside
import org.scalatest.OptionValues
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import pl.touk.nussknacker.engine.api.context.ValidationContext
import pl.touk.nussknacker.engine.api.typed.typing.{Typed, TypedClass, TypedObjectWithValue}

import scala.reflect.ClassTag

class DictKeyWithLabelExpressionParserTest extends AnyFunSuite with Matchers with OptionValues {
private val dictKeyWithLabelExpressionParser = DictKeyWithLabelExpressionParser

test("should parse dict key with label String expression and lift typing information") {
checkForDictKeyWithLabelExpressionTypingInfo[String]("'pizza'", "pizza")
}

test("should parse dict key with label Long expression and lift typing information") {
checkForDictKeyWithLabelExpressionTypingInfo[Long](42, "number")
}

test("should parse dict key with label Boolean expression and lift typing information") {
checkForDictKeyWithLabelExpressionTypingInfo[Boolean](true, "judge")
}

private def checkForDictKeyWithLabelExpressionTypingInfo[T: ClassTag](key: T, label: String) = {
val jsonString = s"""{"key":"$key","label":"$label"}"""
val parsedTypingResult = dictKeyWithLabelExpressionParser
.parse(jsonString, ValidationContext.empty, Typed.typedClass[T])
.toOption
.value
.typingInfo
.typingResult

inside(parsedTypingResult) { case typedObjectWithValue: TypedObjectWithValue =>
typedObjectWithValue.underlying shouldBe Typed.typedClass[T]
typedObjectWithValue.valueOpt shouldBe Some(key)
}
}

}

0 comments on commit 10ee2df

Please sign in to comment.