Skip to content

Commit

Permalink
TypedObjectTypingResult - use regular Map in place of ListMap, order …
Browse files Browse the repository at this point in the history
…no longer matters - step #1

The order of fields was important for frontend forms that were generated based on TypingResult.
Because this is no longer the case, we can replace ListMap with a regular Map.
  • Loading branch information
rafa-minimal committed Jun 12, 2023
1 parent e545064 commit e369ee9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import pl.touk.nussknacker.engine.api.util.{NotNothing, ReflectUtils}
import pl.touk.nussknacker.engine.util.Implicits.RichScalaMap

import scala.jdk.CollectionConverters._
import scala.collection.immutable.ListMap
import scala.language.implicitConversions
import scala.reflect.ClassTag
import scala.reflect.runtime.universe._
Expand Down Expand Up @@ -45,28 +44,20 @@ object typing {
object TypedObjectTypingResult {

def apply(definition: TypedObjectDefinition): TypedObjectTypingResult =
TypedObjectTypingResult(definition.fields.map { case (k, v) => (k, Typed(v))})
TypedObjectTypingResult(definition.fields)

def apply(fields: List[(String, TypingResult)]): TypedObjectTypingResult =
TypedObjectTypingResult(ListMap(fields: _*))

def apply(fields: List[(String, TypingResult)], objType: TypedClass): TypedObjectTypingResult =
TypedObjectTypingResult(ListMap(fields: _*), objType)

def apply(fields: ListMap[String, TypingResult]): TypedObjectTypingResult = {
TypedObjectTypingResult(fields, stringMapWithValues[java.util.Map[_, _]](fields.toList))
}
def apply(fields: Map[String, TypingResult]): TypedObjectTypingResult =
TypedObjectTypingResult(fields, stringMapWithValues[java.util.Map[_, _]](fields))
}

// Warning, fields are kept in list-like map: 1) order is important 2) lookup has O(n) complexity
case class TypedObjectTypingResult(fields: ListMap[String, TypingResult],
case class TypedObjectTypingResult(fields: Map[String, TypingResult],
objType: TypedClass,
additionalInfo: Map[String, AdditionalDataValue] = Map.empty) extends SingleTypingResult {
override def valueOpt: Option[ListMap[String, Any]] =
fields.map{ case (k, v) => v.valueOpt.map((k, _))}.toList.sequence.map(ListMap(_: _*))
override def valueOpt: Option[Map[String, Any]] =
fields.map{ case (k, v) => v.valueOpt.map((k, _))}.toList.sequence.map(Map(_: _*))

override def withoutValue: TypedObjectTypingResult =
TypedObjectTypingResult(ListMap(fields.mapValuesNow(_.withoutValue).toList: _*), objType, additionalInfo)
TypedObjectTypingResult(fields.mapValuesNow(_.withoutValue), objType, additionalInfo)

override def display: String = fields.map { case (name, typ) => s"$name: ${typ.display}"}.mkString("{", ", ", "}")
}
Expand Down Expand Up @@ -311,7 +302,7 @@ object typing {

private def typeMapFields(map: Map[String, Any]) = map.map {
case (k, v) => k -> fromInstance(v)
}.toList
}

def apply(possibleTypes: TypingResult*): TypingResult = {
apply(possibleTypes.toSet)
Expand All @@ -338,16 +329,16 @@ object typing {

}

private def stringMapWithValues[T: ClassTag](fields: List[(String, TypingResult)]): TypedClass = {
val valueType = superTypeOfTypes(fields.map(_._2))
private def stringMapWithValues[T: ClassTag](fields: Map[String, TypingResult]): TypedClass = {
val valueType = superTypeOfTypes(fields.values)
Typed.genericTypeClass[T](List(Typed[String], valueType))
}

private def supertypeOfElementTypes(list: List[_]): TypingResult = {
superTypeOfTypes(list.map(fromInstance))
}

private def superTypeOfTypes(list: List[TypingResult]) = {
private def superTypeOfTypes(list: Iterable[TypingResult]) = {
val superTypeFinder = new CommonSupertypeFinder(SupertypeClassResolutionStrategy.AnySuperclass, true)
list.reduceOption(superTypeFinder.commonSupertype(_, _)(NumberTypesPromotionStrategy.ToSupertype)).getOrElse(Unknown)
}
Expand Down
1 change: 1 addition & 0 deletions docs/MigrationGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ To see the biggest differences please consult the [changelog](Changelog.md).
## In version 1.10.0

### Code API changes
* [#4352](https://github.com/TouK/nussknacker/pull/4352) `TypedObjectTypingResult#fields` are no longer ordered, fields will be sorted for presentation (see `TypedObjectTypingResult#display`)
* [#4294](https://github.com/TouK/nussknacker/pull/4294) `HttpRemoteEnvironmentConfig` allows you to pass flag `passUsernameInMigration` - (default true).
When set to true, migration attaches username in the form of `Remote[userName]` while migrating to secondary environment. To use the old migration endpoint, set to false.
* [#4278](https://github.com/TouK/nussknacker/pull/4278) Now expression compiler and code suggestions mechanism are reusing the same
Expand Down

0 comments on commit e369ee9

Please sign in to comment.