Skip to content
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

[NU-1701] Introduce extension methods to SpeL. Add canCastTo and cast… #6875

Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ class ExpressionSuggesterSpec
Map("empty" -> List(StaticMethodDefinition(MethodTypeInfo(Nil, None, Typed[Boolean]), "empty", None))),
Map.empty
),
ClassDefinition(
Unknown,
Map("canCastTo" -> List(StaticMethodDefinition(MethodTypeInfo(Nil, None, Typed[Boolean]), "canCastTo", None))),
Map.empty
),
)
)

Expand Down Expand Up @@ -149,6 +154,7 @@ class ExpressionSuggesterSpec
"listOfUnions" -> Typed.genericTypeClass[java.util.List[A]](List(Typed(Typed[A], Typed[B]))),
"dictFoo" -> DictInstance("dictFoo", EmbeddedDictDefinition(Map.empty[String, String])).typingResult,
"dictBar" -> DictInstance("dictBar", EmbeddedDictDefinition(Map.empty[String, String])).typingResult,
"unknown" -> Unknown
)

private def spelSuggestionsFor(input: String, row: Int = 0, column: Int = -1): List[ExpressionSuggestion] = {
Expand Down Expand Up @@ -202,6 +208,7 @@ class ExpressionSuggesterSpec
"#other",
"#union",
"#unionOfLists",
"#unknown",
"#util"
)
}
Expand All @@ -219,6 +226,7 @@ class ExpressionSuggesterSpec
"#other",
"#union",
"#unionOfLists",
"#unknown",
"#util"
)
}
Expand Down Expand Up @@ -765,6 +773,12 @@ class ExpressionSuggesterSpec
)
}

test("should suggest methods for unknown") {
spelSuggestionsFor("#unknown.") shouldBe List(
suggestion("canCastTo", Typed[Boolean]),
mslabek marked this conversation as resolved.
Show resolved Hide resolved
)
}

}

object ExpressionSuggesterTestData {
Expand Down
1 change: 1 addition & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
* [#6925](https://github.com/TouK/nussknacker/pull/6925) Fix situation when preset labels were presented as `null` when node didn't pass the validation.
* [#6935](https://github.com/TouK/nussknacker/pull/6935) Spel: Scenario labels added to meta variable - `#meta.scenarioLabels`
* [#6952](https://github.com/TouK/nussknacker/pull/6952) Improvement: TypeInformation support for scala.Option
* [#6840](https://github.com/TouK/nussknacker/pull/6840) Introduce canCastTo, castTo and castToOrNull extension methods in SpeL.

## 1.17

Expand Down
13 changes: 13 additions & 0 deletions docs/scenarios_authoring/Spel.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,16 @@ On the other hand, formatter created using `#DATE_FORMAT.formatter()` method wil
- `#DATE_FORMAT.lenientFormatter('yyyy-MM-dd EEEE', 'PL')` - creates lenient version `DateTimeFormatter` using given pattern and locale

For full list of available format options take a look at [DateTimeFormatter api docs](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html).

## Casting.

When a type cannot be determined by parser, the type is presented as `Unknown`. When we know what the type will be on
runtime, we can cast a given type, and then we can operate on the cast type.

E.g. having a variable `obj` of a type: `List[Unknown]` and we know the elements are strings then we can cast elements
to String: `#obj.![#this.castToOrNull('java.lang.String')]`.

Available methods:
- `canCastTo` - checks if a type can be cast to a given class.
- `castTo` - casts a type to a given class or throws exception if type cannot be cast.
- `castToOrNull` - casts a type to a given class or return null if type cannot be cast.
Loading
Loading