-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mbz static method validation (#1914)
* Added validation for classes passed as TypeReference based on ModelDatat.typeDefinitions
- Loading branch information
1 parent
7789665
commit 984d30b
Showing
21 changed files
with
222 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
engine/interpreter/src/main/scala/pl/touk/nussknacker/engine/TypeDefinitionSet.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package pl.touk.nussknacker.engine | ||
|
||
import cats.data.{NonEmptyList, Validated} | ||
import cats.data.Validated.{Invalid, Valid} | ||
import org.apache.commons.lang3.ClassUtils | ||
import org.springframework.expression.{EvaluationContext, EvaluationException} | ||
import org.springframework.expression.spel.ExpressionState | ||
import org.springframework.expression.spel.ast.TypeReference | ||
import pl.touk.nussknacker.engine.api.Context | ||
import pl.touk.nussknacker.engine.api.expression.ExpressionParseError | ||
import pl.touk.nussknacker.engine.api.typed.typing.{TypedClass, TypingResult} | ||
import pl.touk.nussknacker.engine.definition.{DefinitionExtractor, ProcessDefinitionExtractor, TypeInfos} | ||
import pl.touk.nussknacker.engine.spel.TypedNode | ||
import pl.touk.nussknacker.engine.spel.ast.SpelAst.RichSpelNode | ||
|
||
import scala.util.{Failure, Success, Try} | ||
|
||
|
||
|
||
object TypeDefinitionSet { | ||
|
||
def empty: TypeDefinitionSet = TypeDefinitionSet(Set.empty) | ||
|
||
} | ||
|
||
case class TypeDefinitionSet(typeDefinitions: Set[TypeInfos.ClazzDefinition]) { | ||
|
||
def validateTypeReference(typeReference: TypeReference, evaluationContext: EvaluationContext): Validated[NonEmptyList[ExpressionParseError], TypedClass] = { | ||
|
||
/** | ||
* getValue mutates TypeReference but is still safe | ||
* it adds values to fields type and exitTypeDescriptor but field type is transient and exitTypeDescriptor is of a primitive type (String) | ||
*/ | ||
val typeReferenceClazz = Try(typeReference.getValue(new ExpressionState(evaluationContext))) | ||
|
||
typeReferenceClazz match { | ||
case Success(typeReferenceClazz) => | ||
typeDefinitions.find(typeDefinition => typeDefinition.clazzName.klass.equals(typeReferenceClazz)) match { | ||
case Some(clazzDefinition: TypeInfos.ClazzDefinition) => Valid(clazzDefinition.clazzName) | ||
case None => Invalid(NonEmptyList.of(ExpressionParseError(s"${typeReferenceClazz} is not allowed to be passed as TypeReference"))) | ||
} | ||
case Failure(_: EvaluationException) => Invalid(NonEmptyList.of(ExpressionParseError(s"Class ${typeReference.toStringAST} does not exist"))) | ||
case Failure(exception) => throw exception | ||
} | ||
|
||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.