Skip to content

Commit

Permalink
[NU-1836] Move converters from CONV util to extension methods. Implem…
Browse files Browse the repository at this point in the history
…ent generic conversion extension methods: is, to and toOrNull.

# Conflicts:
#	docs/Changelog.md

# Conflicts:
#	docs/Changelog.md
  • Loading branch information
Łukasz Bigorajski committed Oct 28, 2024
1 parent 309f511 commit 4ea7c6e
Show file tree
Hide file tree
Showing 25 changed files with 4,916 additions and 1,498 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ object typing {
fields.map { case (fieldName, fieldType) =>
fieldName -> fieldType.withoutValue
},
runtimeObjType,
runtimeObjType.withoutValue,
additionalInfo
)

Expand Down Expand Up @@ -206,7 +206,7 @@ object typing {
case class TypedClass private[typing] (klass: Class[_], params: List[TypingResult]) extends SingleTypingResult {
override val valueOpt: None.type = None

override def withoutValue: TypedClass = this
override def withoutValue: TypedClass = TypedClass(klass, params.map(_.withoutValue))

override def display: String = {
val className = if (klass.isArray) "List" else ReflectUtils.simpleNameWithoutSuffix(runtimeObjType.klass)
Expand Down Expand Up @@ -353,8 +353,9 @@ object typing {
supertypeOfElementTypes(javaList.asScala.toList).withoutValue,
javaList
)
case set: java.util.Set[_] =>
genericTypeClass(classOf[java.util.Set[_]], List(supertypeOfElementTypes(set.asScala.toList)))
case typeFromInstance: TypedFromInstance => typeFromInstance.typingResult
// TODO: handle more types, for example Set
case other =>
Typed(other.getClass) match {
case typedClass: TypedClass =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,17 +780,14 @@ class ExpressionSuggesterSpec
}

test("should suggest parameters for casts methods") {
spelSuggestionsFor("#unknown.canCastTo('')", column = 20) should contain theSameElementsAs List(
suggestion("String", Typed[String]),
suggestion("Duration", Typed[Duration]),
suggestion("LocalDateTime", Typed[LocalDateTime]),
suggestion("Map", Typed[java.util.Map[_, _]]),
suggestion("A", Typed[A]),
suggestion("AA", Typed[AA]),
suggestion("B", Typed[B]),
suggestion("C", Typed[C]),
suggestion("Util", Typed[Util]),
suggestion("WithList", Typed[WithList]),
spelSuggestionsFor("#unknown.to('')", column = 13) should contain theSameElementsAs List(
suggestion("BigDecimal", Typed[java.math.BigDecimal]),
suggestion("Boolean", Typed[java.lang.Boolean]),
suggestion("Double", Typed[java.lang.Double]),
suggestion("Long", Typed[java.lang.Long]),
suggestion("String", Typed[java.lang.String]),
suggestion("List", Typed.genericTypeClass[java.util.List[_]](List(Unknown))),
suggestion("Map", Typed.genericTypeClass[java.util.Map[_, _]](List(Unknown, Unknown))),
)
}

Expand Down
17 changes: 1 addition & 16 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,7 @@
* [#6750](https://github.com/TouK/nussknacker/pull/6750) Add varargs to `#COLLECTION.concat` and `#COLLECTION.merge`.
* [#6778](https://github.com/TouK/nussknacker/pull/6778) SpeL: check for methods if a property for a given name does not exist.
* [#6769](https://github.com/TouK/nussknacker/pull/6769) Added possibility to choose presets and define lists for Long typed parameter inputs in fragments.
* [#6807](https://github.com/TouK/nussknacker/pull/6807) Add conversion functions to primitives to: `#CONV`:
* toNumberOrNull
* toString
* toBoolean
* toBooleanOrNull
* toInteger
* toIntegerOrNull
* toLong
* toLongOrNull
* toDouble
* toDoubleOrNull
* toBigInteger
* toBigIntegerOrNull
* toBigDecimal
* toBigDecimalOrNull
* [#6807](https://github.com/TouK/nussknacker/pull/6807) Add `toNumberOrNull` conversion function, to `#CONV`.
* [#6995](https://github.com/TouK/nussknacker/pull/6995) Add `toJson` and `toJsonString` conversions (in the `#CONV` helper)
* [#6995](https://github.com/TouK/nussknacker/pull/6995) Add `#BASE64` helper to decode/encode Base64 values
* [#6826](https://github.com/TouK/nussknacker/pull/6826) Security fix: added validation of expression used inside
Expand All @@ -71,7 +57,6 @@
* [#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.
* [#6974](https://github.com/TouK/nussknacker/pull/6974) Add SpeL suggestions for cast methods parameter.
* [#6958](https://github.com/TouK/nussknacker/pull/6958) Add message size limit in the "Kafka" exceptionHandler
* [#6988](https://github.com/TouK/nussknacker/pull/6988) Remove unused API classes: `MultiMap`, `TimestampedEvictableStateFunction`
Expand Down
33 changes: 21 additions & 12 deletions docs/scenarios_authoring/Spel.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,13 @@ person2 = name: "John"; age: 24
listOfPersons = {person1, person2}
```

| Expression | Result | Type |
| ------------ | -------- | -------- |
| `{1,2,3,4}.![#this * 2]` | {2, 4, 6, 8} | List[Integer] |
| `#listOfPersons.![#this.name]` | {'Alex', 'John'} | List[String] |
| `#listOfPersons.![#this.age]` | {42, 24} | List[Integer] |
| `#listOfPersons.![7]` | {7, 7} | List[Integer] |
| Expression | Result | Type |
|-----------------------------------------------------------------|----------------------|----------------------|
| `{1,2,3,4}.![#this * 2]` | {2, 4, 6, 8} | List[Integer] |
| `#listOfPersons.![#this.name]` | {'Alex', 'John'} | List[String] |
| `#listOfPersons.![#this.age]` | {42, 24} | List[Integer] |
| `#listOfPersons.![7]` | {7, 7} | List[Integer] |
| `#listOfPersons.![{key: #this.name, value: #this.age}].toMap()` | {Alex: 42, John: 24} | Map[String, Integer] |


For other operations on lists, please see the `#COLLECTION` [helper](#built-in-helpers).
Expand Down Expand Up @@ -278,12 +279,20 @@ More usage examples can be found in [this section](#conversions-between-datetime

Explicit conversions are available in utility classes and build-in java conversion mechanisms:

| Expression | Result | Type |
|-----------------------------------------------------------------|----------------------------|-----------------|
| `#NUMERIC.toNumber('42')` | 42 | Number |
| `#NUMERIC.toNumber('42').toString()` | '42' | String |
| `#DATE_FORMAT.parseOffsetDateTime('2018-10-23T12:12:13+00:00')` | 1540296720000 | OffsetDateTime |
| `#DATE_FORMAT.parseLocalDateTime('2018-10-23T12:12:13')` | 2018-10-23T12:12:13+00:00 | LocalDateTime |
| Expression | Result | Type |
|-----------------------------------------------------------------|---------------------------|----------------|
| `#NUMERIC.toNumber('42')` | 42 | Number |
| `#NUMERIC.toNumber('42').toString()` | '42' | String |
| `#DATE_FORMAT.parseOffsetDateTime('2018-10-23T12:12:13+00:00')` | 1540296720000 | OffsetDateTime |
| `#DATE_FORMAT.parseLocalDateTime('2018-10-23T12:12:13')` | 2018-10-23T12:12:13+00:00 | LocalDateTime |
| `'true'.toBoolean` | true | Boolean |
| `'11'.toLong` | 11 | Long |
| `'1.1'.toDouble` | 1.1 | Double |
| `2.1.toBigDecimal` | 2.1 | BigDecimal |

Functions which start with `to`, e.g. `toBoolean`, have their equivalents:
- to check if a given value can be converted to appropriate type, e.g. `'true'.isBoolean`.
- and to return null if conversion fails, e.g. `'false'.toBooleanOrNull'`.

### Casting

Expand Down
Loading

0 comments on commit 4ea7c6e

Please sign in to comment.