Skip to content

Commit

Permalink
[NU-1836] Add toList and toMap extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Łukasz Bigorajski committed Oct 21, 2024
1 parent 8502620 commit 3d52e8c
Show file tree
Hide file tree
Showing 11 changed files with 762 additions and 18 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
3 changes: 3 additions & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
* [#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`
* [#7000](https://github.com/TouK/nussknacker/pull/7000) Show all possible options for dictionary editor on open.
* [#7042](https://github.com/TouK/nussknacker/pull/7042) SpeL: added extension methods:
* toList/toListOrNull - a collection or unknown collection can be converted to a list.
* toMap/toMapOrNull - the list of key-value pairs or unknown map can be converted to a map.

## 1.17

Expand Down
13 changes: 7 additions & 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,54 @@
}
}
],
"toList": [
{
"description": "Convert to a list or throw exception in case of failure",
"name": "toList",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toListOrNull": [
{
"description": "Convert to a list or null in case of failure",
"name": "toListOrNull",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toMap": [
{
"description": "Convert to a map or throw exception in case of failure",
"name": "toMap",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toMapOrNull": [
{
"description": "Convert to a map or null in case of failure",
"name": "toMapOrNull",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toString": [
{
"name": "toString",
Expand Down Expand Up @@ -3106,6 +3154,54 @@
]
}
],
"toList": [
{
"description": "Convert to a list or throw exception in case of failure",
"name": "toList",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toListOrNull": [
{
"description": "Convert to a list or null in case of failure",
"name": "toListOrNull",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toMap": [
{
"description": "Convert to a map or throw exception in case of failure",
"name": "toMap",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toMapOrNull": [
{
"description": "Convert to a map or null in case of failure",
"name": "toMapOrNull",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toString": [
{
"name": "toString",
Expand Down Expand Up @@ -11573,6 +11669,54 @@
}
}
],
"toList": [
{
"description": "Convert to a list or throw exception in case of failure",
"name": "toList",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toListOrNull": [
{
"description": "Convert to a list or null in case of failure",
"name": "toListOrNull",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toMap": [
{
"description": "Convert to a map or throw exception in case of failure",
"name": "toMap",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toMapOrNull": [
{
"description": "Convert to a map or null in case of failure",
"name": "toMapOrNull",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toString": [
{
"name": "toString",
Expand Down Expand Up @@ -11720,6 +11864,54 @@
}
}
],
"toList": [
{
"description": "Convert to a list or throw exception in case of failure",
"name": "toList",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toListOrNull": [
{
"description": "Convert to a list or null in case of failure",
"name": "toListOrNull",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toMap": [
{
"description": "Convert to a map or throw exception in case of failure",
"name": "toMap",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toMapOrNull": [
{
"description": "Convert to a map or null in case of failure",
"name": "toMapOrNull",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toString": [
{
"name": "toString",
Expand Down Expand Up @@ -11956,6 +12148,54 @@
}
}
],
"toList": [
{
"description": "Convert to a list or throw exception in case of failure",
"name": "toList",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toListOrNull": [
{
"description": "Convert to a list or null in case of failure",
"name": "toListOrNull",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toMap": [
{
"description": "Convert to a map or throw exception in case of failure",
"name": "toMap",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toMapOrNull": [
{
"description": "Convert to a map or null in case of failure",
"name": "toMapOrNull",
"signatures": [
{
"noVarArgs": [],
"result": {"type": "Unknown"}
}
]
}
],
"toString": [
{
"name": "toString",
Expand Down
Loading

0 comments on commit 3d52e8c

Please sign in to comment.