Browse Source

Add !expr function to allow inserting the result of evaluating an expression

master
Adam Pippin 3 years ago
parent
commit
6485b4f5a1
  1. 29
      app/Engine/Cfnpp/Functions.php

29
app/Engine/Cfnpp/Functions.php

@ -141,6 +141,11 @@ class Functions
$expression = $parser->parse($condition->getValue()); $expression = $parser->parse($condition->getValue());
$result = $expression->evaluate($this->options->getVariables()); $result = $expression->evaluate($this->options->getVariables());
if (is_array($result))
{
throw new \Exception('!if expression must evaluate down to a single value: '.$condition->getValue());
}
if ($result) if ($result)
{ {
$if_true->setName($node->hasName() ? $node->getName() : null); $if_true->setName($node->hasName() ? $node->getName() : null);
@ -153,4 +158,28 @@ class Functions
} }
return $if_false; return $if_false;
} }
/**
* Calculate an expression.
*
* @param Node $node
* @param NodeFunction $function
* @return ?Node
*/
public function f_expr(Node $node, NodeFunction $function): ?Node
{
if (!($function instanceof NodeFunctionValue))
{
throw new \Exception('!if requires scalar argument');
}
$parser = new \App\Engine\Cfnpp\Expression\Parser();
$expression = $parser->parse($function->getValue());
$result = $expression->evaluate($this->options->getVariables());
$result = Node::fromPhp($result);
$result->setName($node->hasName() ? $node->getName() : null);
return $result;
}
} }

Loading…
Cancel
Save