Skip to content

Commit

Permalink
Feat/from value pattern matching (#1588)
Browse files Browse the repository at this point in the history
* Adde FromValue extractor for pattern matching
  • Loading branch information
w8mr authored Nov 8, 2023
1 parent dfbb19a commit 4f342b5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ import com.ing.baker.runtime.akka.internal.{FatalInteractionException, RecipeRun
import com.ing.baker.runtime.model.BakerLogging
import com.ing.baker.runtime.scaladsl.{EventInstance, IngredientInstance, RecipeInstanceState}
import com.ing.baker.runtime.serialization.Encryption
import com.ing.baker.types.{Converters, PrimitiveValue, Value}
import com.ing.baker.types.{FromValue, PrimitiveValue, Value}

import java.time.Duration
import scala.collection.immutable
import scala.concurrent.ExecutionContext
import scala.concurrent.duration.Duration.Zero
import scala.concurrent.duration._
import scala.language.existentials
import scala.reflect.runtime.universe
import scala.util.Try

object ProcessInstance {
Expand Down Expand Up @@ -108,15 +107,15 @@ object ProcessInstance {
}
}

private val finiteDurationType = Converters.readJavaType[FiniteDuration]
private val durationType = Converters.readJavaType[Duration]
private val javaDuration = FromValue[java.time.Duration]()
private val finiteDuration = FromValue[FiniteDuration]()

def getWaitTimeInMillis(interactionTransition: InteractionTransition, state: RecipeInstanceState): Long = {
val input: immutable.Seq[IngredientInstance] = RecipeRuntime.createInteractionInput(interactionTransition, state)
if (input.size != 1) throw new FatalInteractionException(s"Delayed transitions can only have 1 input ingredient")
input.head.value match {
case value if value.isInstanceOf(finiteDurationType) => value.as[FiniteDuration].toMillis
case value if value.isInstanceOf(durationType) => value.as[java.time.Duration].toMillis
input.head.value match {
case javaDuration(d) => d.toMillis
case finiteDuration(fd) => fd.toMillis
case _ => throw new FatalInteractionException(s"Delayed transition ingredient not of type scala.concurrent.duration.FiniteDuration or java.time.Duration")
}
}
Expand Down Expand Up @@ -265,7 +264,7 @@ class ProcessInstance[S, E](

case IdleStop(n) =>
if (n == instance.sequenceNr && instance.activeJobs.isEmpty) {
log.idleStop(recipeInstanceId, settings.idleTTL.getOrElse(Zero))
log.idleStop(recipeInstanceId, settings.idleTTL.getOrElse(Duration.Zero))
stopMe()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.ing.baker.runtime.akka.namedCachedThreadPool
import com.ing.baker.runtime.scaladsl.RecipeInstanceState
import com.ing.baker.runtime.serialization.Encryption.NoEncryption
import com.ing.baker.types
import com.ing.baker.types.{Converters, Int32, Int64, PrimitiveValue, RecordField, Value}
import com.ing.baker.types.{Converters, Value}
import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito._
import org.mockito.invocation.InvocationOnMock
Expand Down
11 changes: 11 additions & 0 deletions core/baker-types/src/main/scala/com/ing/baker/types/Value.scala
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,14 @@ case class ListValue(entries: List[Value]) extends Value {

override def toString: String = entries.mkString("[", ",", "]")
}

object FromValue {
class FromValueExtractor[T: universe.TypeTag]() {
def unapply(v: Value) = Try {
v.as[T]
}.toOption
}

def apply[T: universe.TypeTag]() = new FromValueExtractor[T]
}

0 comments on commit 4f342b5

Please sign in to comment.