From c098ed823cf2011384a7c59717fcc912cb0a5db7 Mon Sep 17 00:00:00 2001 From: Giorgio Garofalo Date: Sat, 10 Aug 2024 00:27:42 +0200 Subject: [PATCH] Allow argument injection of function call and function call node --- .../eu/iamgio/quarkdown/context/BaseContext.kt | 7 ++++++- .../iamgio/quarkdown/function/call/FunctionCall.kt | 4 ++++ .../quarkdown/function/reflect/InjectedValue.kt | 12 +++++++++++- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/context/BaseContext.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/context/BaseContext.kt index 8e4479a2..1d430fbf 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/context/BaseContext.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/context/BaseContext.kt @@ -47,7 +47,12 @@ open class BaseContext( val function = getFunctionByName(call.name) return function?.let { - FunctionCall(it, call.arguments, context = this) + FunctionCall( + it, + call.arguments, + context = this, + sourceNode = call, + ) } } diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/function/call/FunctionCall.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/function/call/FunctionCall.kt index 67d8c17e..32cf6441 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/function/call/FunctionCall.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/function/call/FunctionCall.kt @@ -1,5 +1,6 @@ package eu.iamgio.quarkdown.function.call +import eu.iamgio.quarkdown.ast.quarkdown.FunctionCallNode import eu.iamgio.quarkdown.context.Context import eu.iamgio.quarkdown.function.Function import eu.iamgio.quarkdown.function.call.binding.AllArgumentsBinder @@ -14,12 +15,15 @@ import eu.iamgio.quarkdown.function.value.OutputValue * @param arguments arguments of the call * @param context optional context this call lies in. * This value can be injected to library functions that demand it via the `@Injected` annotation + * @param sourceNode [FunctionCallNode] that generated this call, if there is any. Like [context], this value can be injected. + * It is `null` if the call is standalone and was not generated by a node. * @param T expected output type of the function */ data class FunctionCall>( val function: Function, val arguments: List, val context: Context? = null, + val sourceNode: FunctionCallNode? = null, ) : Expression { /** * Checks the call validity and calls the function. diff --git a/core/src/main/kotlin/eu/iamgio/quarkdown/function/reflect/InjectedValue.kt b/core/src/main/kotlin/eu/iamgio/quarkdown/function/reflect/InjectedValue.kt index 2f94116f..aa78d3bd 100644 --- a/core/src/main/kotlin/eu/iamgio/quarkdown/function/reflect/InjectedValue.kt +++ b/core/src/main/kotlin/eu/iamgio/quarkdown/function/reflect/InjectedValue.kt @@ -1,5 +1,6 @@ package eu.iamgio.quarkdown.function.reflect +import eu.iamgio.quarkdown.ast.quarkdown.FunctionCallNode import eu.iamgio.quarkdown.context.Context import eu.iamgio.quarkdown.function.FunctionParameter import eu.iamgio.quarkdown.function.call.FunctionCall @@ -15,7 +16,14 @@ import kotlin.reflect.full.isSubclassOf */ object InjectedValue { /** - * @param type type of the target parameter to inject value to + * Generates a value to inject to a function parameter that expects a type of [type]. + * + * Supported types: + * - [Context]: injects the context of the function call + * - [FunctionCallNode]: injects the source node of the function call + * - [FunctionCall]: injects the function call itself + * + * @param type type of the target parameter to inject value to. * @param call function call to extract injectable data from * @return the function-call-ready value to inject * @throws IllegalArgumentException if the target type is not injectable @@ -26,6 +34,8 @@ object InjectedValue { ): InputValue<*> = when { type.isSubclassOf(Context::class) -> ObjectValue(call.context) + type.isSubclassOf(FunctionCallNode::class) -> ObjectValue(call.sourceNode) + type.isSubclassOf(FunctionCall::class) -> ObjectValue(call) else -> throw IllegalArgumentException("Cannot inject a value to type $type") } }