Skip to content

Commit aa7c5d4

Browse files
wip
1 parent 436e32e commit aa7c5d4

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

scenario-compiler/src/main/scala/pl/touk/nussknacker/engine/Interpreter.scala

+4-6
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,10 @@ private class InterpreterInternal[F[_]: Monad](
144144
interpretationResult(node, FragmentEndReference(id, fields), newCtx)
145145
}
146146
)
147-
case Enricher(_, ref, outName, next, mockedOutput) =>
148-
if (runtimeMode == Test && mockedOutput.isDefined) {
149-
//todo: refactor
150-
val valueWithModifiedContext = expressionEvaluator.evaluate[Any](mockedOutput.get, outName, node.id, ctx)
151-
interpretNext(next, ctx.withVariable(outName, valueWithModifiedContext.value))
152-
}
147+
case Enricher(_, _, outName, next, Some(mockedOutput)) if runtimeMode == Test =>
148+
val valueWithModifiedContext = expressionEvaluator.evaluate[Any](mockedOutput, outName, node.id, ctx)
149+
interpretNext(next, ctx.withVariable(outName, valueWithModifiedContext.value))
150+
case Enricher(_, ref, outName, next, _) =>
153151
invokeWrappedInInterpreterShape(ref, ctx).flatMap {
154152
case Left(ValueWithContext(out, newCtx)) => interpretNext(node, next, newCtx.withVariable(outName, out))
155153
case Right(exInfo) => Monad[F].pure(List(Right(exInfo)))

scenario-compiler/src/main/scala/pl/touk/nussknacker/engine/compile/PartSubGraphCompiler.scala

+12-8
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,23 @@ class PartSubGraphCompiler(nodeCompiler: NodeCompiler) {
210210
(ref, next) => compiledgraph.node.Processor(id, ref, next, isDisabled.contains(true))
211211
)
212212

213-
case enricher @ Enricher(id, _, output, _, mockExpression) =>
213+
case enricher @ Enricher(id, _, output, _, _) =>
214214
val NodeCompilationResult(typingInfo, parameters, newCtx, validatedServiceRef, _) =
215215
nodeCompiler.compileEnricher(enricher, ctx, outputVar = OutputVar.enricher(output))
216-
//tddo: compile expression
217216

218-
//todo: type expression here and check if it match output variable type
219-
220-
CompilationResult.map4(
217+
CompilationResult.map3(
221218
toCompilationResult(validatedServiceRef, typingInfo, parameters),
222219
CompilationResult(newCtx),
223-
compile(next, newCtx.getOrElse(ctx)),
224-
225-
)((ref, _, next) => compiledgraph.node.Enricher(id, ref, output, next, ))
220+
compile(next, newCtx.getOrElse(ctx))
221+
)((serviceCompilationResult, _, next) =>
222+
compiledgraph.node.Enricher(
223+
id,
224+
serviceCompilationResult.serviceRef,
225+
output,
226+
next,
227+
serviceCompilationResult.mockOutputExpression
228+
)
229+
)
226230

227231
// here we don't do anything, in subgraphcompiler it's just pass through, we can't add input context here because it contains output variable context (not input)
228232
case CustomNode(id, _, nodeType, _, _) =>

scenario-compiler/src/main/scala/pl/touk/nussknacker/engine/compile/nodecompilation/NodeCompiler.scala

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package pl.touk.nussknacker.engine.compile.nodecompilation
22

3-
import cats.data.{NonEmptyList, ValidatedNel, Writer}
3+
import cats.data.{NonEmptyList, Validated, ValidatedNel, Writer}
44
import cats.data.Validated.{invalid, valid, Invalid, Valid}
55
import cats.implicits._
66
import pl.touk.nussknacker.engine.{api, compiledgraph, RuntimeMode, ScenarioCompilationDependencies}
@@ -21,7 +21,10 @@ import pl.touk.nussknacker.engine.compile.{
2121
FragmentSourceWithTestWithParametersSupportFactory,
2222
NodeValidationExceptionHandler
2323
}
24-
import pl.touk.nussknacker.engine.compile.nodecompilation.NodeCompiler.NodeCompilationResult
24+
import pl.touk.nussknacker.engine.compile.nodecompilation.NodeCompiler.{
25+
EnricherCompilationResult,
26+
NodeCompilationResult
27+
}
2528
import pl.touk.nussknacker.engine.compiledgraph.{CompiledParameter, TypedParameter}
2629
import pl.touk.nussknacker.engine.definition.component.ComponentDefinitionWithImplementation
2730
import pl.touk.nussknacker.engine.definition.component.dynamic.{
@@ -60,6 +63,11 @@ object NodeCompiler {
6063

6164
}
6265

66+
final case class EnricherCompilationResult(
67+
serviceRef: compiledgraph.service.ServiceRef,
68+
mockOutputExpression: Option[CompiledExpression]
69+
)
70+
6371
}
6472

6573
class NodeCompiler(
@@ -374,8 +382,29 @@ class NodeCompiler(
374382
def compileEnricher(n: Enricher, ctx: ValidationContext, outputVar: OutputVar)(
375383
implicit nodeId: NodeId,
376384
scenarioCompilationDependencies: ScenarioCompilationDependencies
377-
): NodeCompilationResult[compiledgraph.service.ServiceRef] = {
378-
compileService(n.service, ctx, Some(outputVar))
385+
): NodeCompilationResult[EnricherCompilationResult] = {
386+
val serviceCompilationResult = compileService(n.service, ctx, Some(outputVar))
387+
388+
val expressionCompilationResult = n.mockExpression match {
389+
case Some(expression) =>
390+
compileEnricherMockExpression(expression, serviceCompilationResult.expressionType.getOrElse(Unknown), ctx)
391+
.map(Some(_))
392+
case None => Validated.validNel(None)
393+
}
394+
serviceCompilationResult.copy(
395+
compiledObject = serviceCompilationResult.compiledObject.product(expressionCompilationResult).map {
396+
case (service, mockedExpression) =>
397+
EnricherCompilationResult(service, mockedExpression)
398+
}
399+
)
400+
}
401+
402+
private def compileEnricherMockExpression(expression: Expression, expectedType: TypingResult, ctx: ValidationContext)(
403+
implicit nodeId: NodeId
404+
): ValidatedNel[ProcessCompilationError, CompiledExpression] = {
405+
expressionCompiler
406+
.compile(expression, None, ctx, expectedType)
407+
.map(_.expression)
379408
}
380409

381410
private def compileService(n: ServiceRef, validationContext: ValidationContext, outputVar: Option[OutputVar])(

0 commit comments

Comments
 (0)