From 949a8deb8f730ea5e2421628837fc8ac4572d471 Mon Sep 17 00:00:00 2001 From: Adam Pippin Date: Fri, 19 Feb 2021 12:33:10 -0800 Subject: [PATCH] Re-add !expr function --- app/Cfnpp/Functions.php | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/app/Cfnpp/Functions.php b/app/Cfnpp/Functions.php index 6321ce1..a4469e5 100644 --- a/app/Cfnpp/Functions.php +++ b/app/Cfnpp/Functions.php @@ -215,23 +215,38 @@ class Functions * @param NodeFunction $function * @return ?Node */ - /* - * TODO: Reimplement public function f_expr(Node $node, NodeFunction $function): ?Node { if (!($function instanceof NodeFunctionValue)) { - throw new \Exception('!if requires scalar argument'); + throw new \Exception('!expr requires scalar argument'); } - $parser = new \App\Cfnpp\Expression\Parser(); - $expression = $parser->parse($function->getValue()); - $result = $expression->evaluate($this->options->getVariables()); + $expression = new \App\Cfnpp\Expression\Expression($function->getValue(), $this->options); - $result = Node::fromPhp($result); - $result->setName($node->hasName() ? $node->getName() : null); + if ($expression->isComplete()) + { + // If we computed a final value/set of values, we can just insert those + // directly. + if ($expression->count() == 1) + { + $solution_node = Node::fromPhp($expression->getValue()); + } + else + { + $solution_node = Node::fromPhp($expression->toArray()); + } + } + else + { + // Otherwise let's convert it to cfn intrinsics + $solution_node = Node::fromPhp($expression->toCloudformation()); + } - return $result; + if ($node->hasName()) + { + $solution_node->setName($node->getName()); + } + return $solution_node; } - */ }