Skip to content

Commit

Permalink
feat: add recursive method for get value with path
Browse files Browse the repository at this point in the history
  • Loading branch information
koevskinikola committed Mar 14, 2023
1 parent c1edeb4 commit 9dc728b
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,22 @@ object ContextBuiltinFunctions {
case List(ValContext(context), ValList(_)) if context.variableProvider.keys.isEmpty => ValNull
case List(ValContext(context), ValList(ValString(head)::Nil)) =>
context.variableProvider.getVariable(head).getOrElse(ValNull)
case List(ValContext(context), ValList(ValString(head)::tail)) => ???
case List(ValContext(context), ValList(ValString(head)::tail)) if isListOfStrings(tail) =>
getValueRecursive(context, head, tail.asInstanceOf[List[ValString]].map(_.value))
case List(ValContext(_), ValList(_)) => ValNull
}
)

@tailrec
private def getValueRecursive(context: Context, head: String, tail: List[String]): Val = {
val value = context.variableProvider.getVariable(head).asInstanceOf[Option[Val]].getOrElse(ValNull)
value match {
case _ if tail.isEmpty => value
case value:ValContext => getValueRecursive(value.context, tail.head, tail.tail)
case _ => ValNull
}
}

private def contextPutFunction = builtinFunction(
params = List("context", "key", "value"),
invoke = {
Expand Down

0 comments on commit 9dc728b

Please sign in to comment.