Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into upgrade-akka-depend…
Browse files Browse the repository at this point in the history
…encies
  • Loading branch information
bekiroguz committed Sep 10, 2024
2 parents 8d26fe4 + 25a10dc commit f239f82
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import com.ing.baker.types.Converters
import scala.collection.immutable.Map
import java.util.*
import java.util.concurrent.CompletableFuture
import kotlin.reflect.full.createType
import kotlin.reflect.jvm.ExperimentalReflectionOnLambdas
import kotlin.reflect.jvm.javaType
import kotlin.reflect.jvm.reflect
import kotlin.reflect.typeOf

inline fun <reified T1, R> functionInteractionInstance(
name: String,
noinline function: (T1) -> R
): InteractionInstance {

val types = listOf(
T1::class.createType().javaType
javaTypeOf<T1>()
)

val params = function.reflect()?.parameters ?: error("Cannot read parameters")
Expand Down Expand Up @@ -76,8 +76,8 @@ inline fun <reified T1, reified T2, R> functionInteractionInstance(
): InteractionInstance {

val types = listOf(
T1::class.createType().javaType,
T2::class.createType().javaType
javaTypeOf<T1>(),
javaTypeOf<T2>()
)

val params = function.reflect()?.parameters ?: error("Cannot read parameters")
Expand Down Expand Up @@ -133,9 +133,9 @@ inline fun <reified T1, reified T2, reified T3, R> functionInteractionInstance(
): InteractionInstance {

val types = listOf(
T1::class.createType().javaType,
T2::class.createType().javaType,
T3::class.createType().javaType,
javaTypeOf<T1>(),
javaTypeOf<T2>(),
javaTypeOf<T3>()
)

val params = function.reflect()?.parameters ?: error("Cannot read parameters")
Expand Down Expand Up @@ -184,4 +184,6 @@ inline fun <reified T1, reified T2, reified T3, R> functionInteractionInstance(
}
}
}
}
}

inline fun <reified T> javaTypeOf() = typeOf<T>().javaType
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import com.ing.baker.runtime.kotlindsl.functionInteractionInstance
import org.junit.Assert.assertEquals
import org.junit.Test
import java.util.*

class FunctionInteractionInstanceTest {

@Test
fun `should handle function interaction with optional`() {
val func = { test: Optional<String> -> "" }
val interaction = functionInteractionInstance("test", func)

assertEquals(interaction.name(), "\$SieveInteraction\$test")
}

@Test
fun `should handle function interaction`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import java.lang.reflect.Type
import java.util.*
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.full.createType
import kotlin.reflect.full.functions
import kotlin.reflect.full.primaryConstructor
import kotlin.reflect.jvm.ExperimentalReflectionOnLambdas
Expand Down Expand Up @@ -103,37 +102,40 @@ class RecipeBuilder(private val name: String) {
/**
* Registers a sieve [T1, T2, R] to the recipe.
*/
inline fun <reified T1,reified R> ingredient(name: String, noinline function: (T1) -> R) {
inline fun <reified T1, reified R> ingredient(name: String, noinline function: (T1) -> R) {
val parameters = function.reflect()?.parameters ?: error("Cannot read parameters")
val ingredients = listOf(T1::class)
val ingredients = listOf(javaTypeOf<T1>())
.zip(parameters)
.map { (clazz, param) -> Ingredient(param.name, clazz.createType().javaType) }
addSieve(name, ingredients, typeOf<R>().javaType, function)
.map { (clazz, param) -> Ingredient(param.name, clazz) }
addSieve(name, ingredients, javaTypeOf<R>(), function)
}

/**
* Registers a sieve [T1, T2, R] to the recipe.
*/
inline fun <reified T1, reified T2, reified R> ingredient(name: String, noinline function: (T1, T2) -> R) {
val parameters = function.reflect()?.parameters ?: error("Cannot read parameters")
val ingredients = listOf(T1::class, T2::class)
val ingredients = listOf(javaTypeOf<T1>(), javaTypeOf<T2>())
.zip(parameters)
.map { (clazz, param) -> Ingredient(param.name, clazz.createType().javaType) }
addSieve(name, ingredients, typeOf<R>().javaType, function)
.map { (clazz, param) -> Ingredient(param.name, clazz) }
addSieve(name, ingredients, javaTypeOf<R>(), function)
}

/**
* Registers a sieve [T1, T2, R] to the recipe.
*/
inline fun <reified T1, reified T2, reified T3, reified R> ingredient(name: String, noinline function: (T1, T2, T3) -> R) {
inline fun <reified T1, reified T2, reified T3, reified R> ingredient(
name: String,
noinline function: (T1, T2, T3) -> R
) {
val parameters = function.reflect()?.parameters ?: error("Cannot read parameters")
val ingredients = listOf(T1::class, T2::class, T3::class)
val ingredients = listOf(javaTypeOf<T1>(), javaTypeOf<T2>(), javaTypeOf<T3>())
.zip(parameters)
.map { (clazz, param) -> Ingredient(param.name, clazz.createType().javaType) }
addSieve(name, ingredients, typeOf<R>().javaType, function)
.map { (clazz, param) -> Ingredient(param.name, clazz) }
addSieve(name, ingredients, javaTypeOf<R>(), function)
}

fun addSieve(name:String, ingredients:List<Ingredient>, returnType:Type, function:Any){
fun addSieve(name: String, ingredients: List<Ingredient>, returnType: Type, function: Any) {
sieves.add(
Sieve(
name,
Expand Down Expand Up @@ -570,3 +572,5 @@ private fun KClass<*>.toEvent(maxFiringLimit: Int? = null): Event {
}

private fun KFunction<*>.hasFiresEventAnnotation() = annotations.any { it.annotationClass == FiresEvent::class }

inline fun <reified T> javaTypeOf() = typeOf<T>().javaType
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class KotlinDslTest {
until = deadline(20.0.seconds)
}

// Ingredient definition failed with optionals
ingredient("test", { test: Optional<String> -> "" })

sensoryEvents {
eventWithoutFiringLimit<Events.OrderPlaced>()
event<Events.PaymentInformationReceived>(maxFiringLimit = 5)
Expand Down

0 comments on commit f239f82

Please sign in to comment.