Skip to content

Commit

Permalink
Allow argument injection of function call and function call node
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgio committed Aug 9, 2024
1 parent 4195218 commit c098ed8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<T : OutputValue<*>>(
val function: Function<T>,
val arguments: List<FunctionCallArgument>,
val context: Context? = null,
val sourceNode: FunctionCallNode? = null,
) : Expression {
/**
* Checks the call validity and calls the function.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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")
}
}

0 comments on commit c098ed8

Please sign in to comment.