Skip to content

Commit

Permalink
review fixes 8
Browse files Browse the repository at this point in the history
  • Loading branch information
Łukasz Bigorajski committed Oct 30, 2024
1 parent 7aaab63 commit 956f6be
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package pl.touk.nussknacker.engine.extension
import cats.data.ValidatedNel
import cats.implicits.catsSyntaxValidatedId
import org.springframework.util.NumberUtils
import pl.touk.nussknacker.engine.api.generics.GenericFunctionTypingError
import pl.touk.nussknacker.engine.api.generics.{GenericFunctionTypingError, MethodTypeInfo}
import pl.touk.nussknacker.engine.api.typed.typing.{Typed, TypingResult}
import pl.touk.nussknacker.engine.definition.clazz.{ClassDefinitionSet, MethodDefinition, StaticMethodDefinition}
import pl.touk.nussknacker.engine.util.classes.Extensions.ClassExtensions

import java.lang.{
Expand Down Expand Up @@ -68,6 +69,29 @@ object Conversion {

}

trait ConversionExt extends ExtensionMethodsHandler with Conversion {
val definitions: List[MethodDefinition]

// Conversion extension should be available for every class in a runtime
override def appliesToClassInRuntime(clazz: Class[_]): Boolean = true

override def extractDefinitions(clazz: Class[_], set: ClassDefinitionSet): Map[String, List[MethodDefinition]] = {
if (appliesToConversion(clazz)) {
definitions.groupBy(_.name)
} else {
Map.empty
}
}

private[extension] def definition(result: TypingResult, methodName: String, desc: Option[String]) =
StaticMethodDefinition(
signature = MethodTypeInfo.noArgTypeInfo(result),
name = methodName,
description = desc
)

}

object ToStringConversion extends Conversion {
override type ResultType = String
override val resultTypeClass: Class[ResultType] = classOf[String]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class ToBigDecimalConversionExt(target: Any) {
def toBigDecimalOrNull(): JBigDecimal = ToBigDecimalConversionExt.convertOrNull(target)
}

object ToBigDecimalConversionExt extends ToNumericConversionExt {
object ToBigDecimalConversionExt extends ConversionExt with ToNumericConversion {
override type ExtensionMethodInvocationTarget = ToBigDecimalConversionExt
override val invocationTargetClass: Class[ToBigDecimalConversionExt] = classOf[ToBigDecimalConversionExt]
override type ResultType = JBigDecimal
override val resultTypeClass: Class[JBigDecimal] = classOf[JBigDecimal]

override val definitions: Map[String, List[MethodDefinition]] = List(
override val definitions: List[MethodDefinition] = List(
definition(Typed.typedClass[JBoolean], "isBigDecimal", Some("Check whether can be convert to a BigDecimal")),
definition(
Typed.typedClass[JBigDecimal],
Expand All @@ -33,7 +33,7 @@ object ToBigDecimalConversionExt extends ToNumericConversionExt {
"toBigDecimalOrNull",
Some("Convert to BigDecimal or null in case of failure")
),
).groupBy(_.name)
)

override def createConverter(
set: ClassDefinitionSet
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package pl.touk.nussknacker.engine.extension

import pl.touk.nussknacker.engine.api.generics.MethodTypeInfo
import pl.touk.nussknacker.engine.api.typed.typing
import pl.touk.nussknacker.engine.api.typed.typing.Typed
import pl.touk.nussknacker.engine.definition.clazz.{ClassDefinitionSet, MethodDefinition, StaticMethodDefinition}
import pl.touk.nussknacker.engine.definition.clazz.{ClassDefinitionSet, StaticMethodDefinition}

import java.lang.{Boolean => JBoolean}

Expand All @@ -12,23 +12,18 @@ class ToBooleanConversionExt(target: Any) {
def toBooleanOrNull(): JBoolean = ToBooleanConversionExt.convertOrNull(target)
}

object ToBooleanConversionExt extends ExtensionMethodsHandler with Conversion {
object ToBooleanConversionExt extends ConversionExt {
private val cannotConvertException = (value: Any) =>
new IllegalArgumentException(s"Cannot convert: $value to Boolean")
private val allowedClassesForConversion: Set[Class[_]] = Set(classOf[String], classOf[Object])
private val booleanTyping: typing.TypedClass = Typed.typedClass[JBoolean]

private val definition = StaticMethodDefinition(
signature = MethodTypeInfo.noArgTypeInfo(Typed.typedClass[JBoolean]),
name = "",
description = None
override val definitions: List[StaticMethodDefinition] = List(
definition(booleanTyping, "isBoolean", Some("Check whether can be convert to a Boolean")),
definition(booleanTyping, "toBoolean", Some("Convert to Boolean or throw exception in case of failure")),
definition(booleanTyping, "toBooleanOrNull", Some("Convert to Boolean or null in case of failure")),
)

private val definitions = List(
definition.copy(name = "isBoolean", description = Some("Check whether can be convert to a Boolean")),
definition.copy(name = "toBoolean", description = Some("Convert to Boolean or throw exception in case of failure")),
definition.copy(name = "toBooleanOrNull", description = Some("Convert to Boolean or null in case of failure")),
).groupBy(_.name)

override type ExtensionMethodInvocationTarget = ToBooleanConversionExt
override val invocationTargetClass: Class[ToBooleanConversionExt] = classOf[ToBooleanConversionExt]

Expand All @@ -37,12 +32,6 @@ object ToBooleanConversionExt extends ExtensionMethodsHandler with Conversion {
): ToExtensionMethodInvocationTargetConverter[ToBooleanConversionExt] =
(target: Any) => new ToBooleanConversionExt(target)

override def extractDefinitions(clazz: Class[_], set: ClassDefinitionSet): Map[String, List[MethodDefinition]] =
if (appliesToConversion(clazz)) definitions
else Map.empty

override def appliesToClassInRuntime(clazz: Class[_]): Boolean = true

override type ResultType = JBoolean
override val resultTypeClass: Class[JBoolean] = classOf[JBoolean]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ class ToDoubleConversionExt(target: Any) {
def toDoubleOrNull(): JDouble = ToDoubleConversionExt.convertOrNull(target)
}

object ToDoubleConversionExt extends ExtensionMethodsHandler with ToNumericConversionExt {
object ToDoubleConversionExt extends ConversionExt with ToNumericConversion {
override type ExtensionMethodInvocationTarget = ToDoubleConversionExt
override val invocationTargetClass: Class[ToDoubleConversionExt] = classOf[ToDoubleConversionExt]
override type ResultType = JDouble
override val resultTypeClass: Class[JDouble] = classOf[JDouble]

override val definitions: Map[String, List[MethodDefinition]] = List(
override val definitions: List[MethodDefinition] = List(
definition(Typed.typedClass[JBoolean], "isDouble", Some("Check whether can be convert to a Double")),
definition(Typed.typedClass[JDouble], "toDouble", Some("Convert to Double or throw exception in case of failure")),
definition(Typed.typedClass[JDouble], "toDoubleOrNull", Some("Convert to Double or null in case of failure")),
).groupBy(_.name)
)

override def createConverter(
set: ClassDefinitionSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ToListConversionExt(target: Any) {

}

object ToListConversionExt extends ExtensionMethodsHandler with ToCollectionConversion {
object ToListConversionExt extends ConversionExt with ToCollectionConversion {
private val booleanTyping = Typed.typedClass[Boolean]
private val listTyping = Typed.genericTypeClass[JList[_]](List(Unknown))
private val collectionClass = classOf[JCollection[_]]
Expand All @@ -45,11 +45,11 @@ object ToListConversionExt extends ExtensionMethodsHandler with ToCollectionConv
description = Option("Convert to a list or null in case of failure")
)

private val definitions = List(
override val definitions: List[MethodDefinition] = List(
isListMethodDefinition,
toListDefinition,
toListOrNullDefinition,
).groupBy(_.name)
)

override type ExtensionMethodInvocationTarget = ToListConversionExt
override val invocationTargetClass: Class[ToListConversionExt] = classOf[ToListConversionExt]
Expand All @@ -59,14 +59,6 @@ object ToListConversionExt extends ExtensionMethodsHandler with ToCollectionConv
): ToExtensionMethodInvocationTargetConverter[ToListConversionExt] =
(target: Any) => new ToListConversionExt(target)

override def extractDefinitions(clazz: Class[_], set: ClassDefinitionSet): Map[String, List[MethodDefinition]] =
if (appliesToConversion(clazz)) definitions
else Map.empty

// Conversion extension should be available for every class in a runtime
override def appliesToClassInRuntime(clazz: Class[_]): Boolean =
true

override type ResultType = JList[_]
override val resultTypeClass: Class[JList[_]] = classOf[JList[_]]
override def typingResult: TypingResult = Typed.genericTypeClass(resultTypeClass, List(Unknown))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ class ToLongConversionExt(target: Any) {
def toLongOrNull(): JLong = ToLongConversionExt.convertOrNull(target)
}

object ToLongConversionExt extends ToNumericConversionExt {
object ToLongConversionExt extends ConversionExt with ToNumericConversion {
override type ExtensionMethodInvocationTarget = ToLongConversionExt
override val invocationTargetClass: Class[ToLongConversionExt] = classOf[ToLongConversionExt]
override type ResultType = JLong
override val resultTypeClass: Class[JLong] = classOf[JLong]

override val definitions: Map[String, List[MethodDefinition]] = List(
override val definitions: List[MethodDefinition] = List(
definition(Typed.typedClass[JBoolean], "isLong", Some("Check whether can be convert to a Long")),
definition(Typed.typedClass[JLong], "toLong", Some("Convert to Long or throw exception in case of failure")),
definition(Typed.typedClass[JLong], "toLongOrNull", Some("Convert to Long or null in case of failure")),
).groupBy(_.name)
)

override def createConverter(
set: ClassDefinitionSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ToMapConversionExt(target: Any) {

}

object ToMapConversionExt extends ExtensionMethodsHandler with ToCollectionConversion {
object ToMapConversionExt extends ConversionExt with ToCollectionConversion {
private val booleanTyping = Typed.typedClass[Boolean]
private val mapTyping = Typed.genericTypeClass[JMap[_, _]](List(Unknown, Unknown))
private val keyName = "key"
Expand Down Expand Up @@ -46,11 +46,11 @@ object ToMapConversionExt extends ExtensionMethodsHandler with ToCollectionConve
description = Option("Convert to a map or null in case of failure")
)

private val definitions = List(
override val definitions: List[MethodDefinition] = List(
isMapMethodDefinition,
toMapDefinition,
toMapOrNullDefinition,
).groupBy(_.name)
)

override type ExtensionMethodInvocationTarget = ToMapConversionExt
override val invocationTargetClass: Class[ToMapConversionExt] = classOf[ToMapConversionExt]
Expand All @@ -60,14 +60,6 @@ object ToMapConversionExt extends ExtensionMethodsHandler with ToCollectionConve
): ToExtensionMethodInvocationTargetConverter[ToMapConversionExt] =
(target: Any) => new ToMapConversionExt(target)

override def extractDefinitions(clazz: Class[_], set: ClassDefinitionSet): Map[String, List[MethodDefinition]] =
if (appliesToConversion(clazz)) definitions
else Map.empty

// Conversion extension should be available for every class in a runtime
override def appliesToClassInRuntime(clazz: Class[_]): Boolean =
true

override type ResultType = JMap[_, _]
override val resultTypeClass: Class[JMap[_, _]] = classOf[ResultType]
override def typingResult: TypingResult = Typed.genericTypeClass(resultTypeClass, List(Unknown, Unknown))
Expand Down

This file was deleted.

0 comments on commit 956f6be

Please sign in to comment.