icinga2/lib/config/expression.cpp

687 lines
21 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2014-10-16 17:44:06 +02:00
#include "config/expression.hpp"
2014-05-25 16:23:35 +02:00
#include "config/configitem.hpp"
#include "config/configitembuilder.hpp"
#include "config/applyrule.hpp"
#include "config/objectrule.hpp"
#include "base/array.hpp"
2014-10-26 19:59:49 +01:00
#include "base/json.hpp"
2014-05-25 16:23:35 +02:00
#include "base/scriptfunction.hpp"
#include "base/scriptvariable.hpp"
#include "base/utility.hpp"
#include "base/objectlock.hpp"
#include "base/object.hpp"
2014-10-19 14:21:12 +02:00
#include "base/logger.hpp"
#include "base/configerror.hpp"
#include <boost/foreach.hpp>
#include <boost/exception_ptr.hpp>
2014-11-06 19:35:47 +01:00
#include <boost/lexical_cast.hpp>
#include <boost/exception/errinfo_nested_exception.hpp>
using namespace icinga;
2014-10-16 17:44:06 +02:00
Expression::Expression(OpCallback op, const Value& operand1, const DebugInfo& di)
: m_Operator(op), m_Operand1(operand1), m_Operand2(), m_DebugInfo(di)
{ }
2014-10-16 17:44:06 +02:00
Expression::Expression(OpCallback op, const Value& operand1, const Value& operand2, const DebugInfo& di)
: m_Operator(op), m_Operand1(operand1), m_Operand2(operand2), m_DebugInfo(di)
{ }
2014-11-06 19:35:47 +01:00
Value Expression::Evaluate(const Object::Ptr& context, DebugHint *dhint) const
{
try {
#ifdef _DEBUG
2014-10-16 17:44:06 +02:00
if (m_Operator != &Expression::OpLiteral) {
std::ostringstream msgbuf;
ShowCodeFragment(msgbuf, m_DebugInfo, false);
2014-10-19 17:52:17 +02:00
Log(LogDebug, "Expression")
<< "Executing:\n" << msgbuf.str();
}
#endif /* _DEBUG */
2014-11-06 19:35:47 +01:00
return m_Operator(this, context, dhint);
} catch (const std::exception& ex) {
if (boost::get_error_info<boost::errinfo_nested_exception>(ex))
throw;
else
BOOST_THROW_EXCEPTION(ConfigError("Error while evaluating expression: " + String(ex.what())) << boost::errinfo_nested_exception(boost::current_exception()) << errinfo_debuginfo(m_DebugInfo));
}
}
2014-10-16 17:44:06 +02:00
void Expression::MakeInline(void)
{
2014-10-16 17:44:06 +02:00
if (m_Operator == &Expression::OpDict)
m_Operand2 = true;
}
2014-10-16 17:44:06 +02:00
void Expression::DumpOperand(std::ostream& stream, const Value& operand, int indent) {
if (operand.IsObjectType<Array>()) {
Array::Ptr arr = operand;
stream << String(indent, ' ') << "Array:\n";
2014-03-24 09:15:45 +01:00
ObjectLock olock(arr);
BOOST_FOREACH(const Value& elem, arr) {
DumpOperand(stream, elem, indent + 1);
}
2014-10-16 17:44:06 +02:00
} else if (operand.IsObjectType<Expression>()) {
Expression::Ptr left = operand;
left->Dump(stream, indent);
} else {
2014-10-26 19:59:49 +01:00
stream << String(indent, ' ') << JsonEncode(operand) << "\n";
}
}
2014-10-16 17:44:06 +02:00
void Expression::Dump(std::ostream& stream, int indent) const
{
String sym = Utility::GetSymbolName(reinterpret_cast<const void *>(m_Operator));
stream << String(indent, ' ') << "op: " << Utility::DemangleSymbolName(sym) << "\n";
stream << String(indent, ' ') << "left:\n";
DumpOperand(stream, m_Operand1, indent + 1);
stream << String(indent, ' ') << "right:\n";
DumpOperand(stream, m_Operand2, indent + 1);
}
2014-11-06 19:35:47 +01:00
Value Expression::EvaluateOperand1(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-06 19:35:47 +01:00
return static_cast<Expression::Ptr>(m_Operand1)->Evaluate(context, dhint);
}
2014-11-06 19:35:47 +01:00
Value Expression::EvaluateOperand2(const Object::Ptr& context, DebugHint *dhint) const
{
2014-11-06 19:35:47 +01:00
return static_cast<Expression::Ptr>(m_Operand2)->Evaluate(context, dhint);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpLiteral(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
return expr->m_Operand1;
}
2014-11-06 19:35:47 +01:00
Value Expression::OpVariable(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
Object::Ptr scope = context;
2014-03-24 09:10:37 +01:00
while (scope) {
2014-11-06 19:35:47 +01:00
if (HasField(scope, expr->m_Operand1))
return GetField(scope, expr->m_Operand1);
2014-03-24 09:10:37 +01:00
2014-11-06 19:35:47 +01:00
scope = GetField(scope, "__parent");
2014-03-24 09:10:37 +01:00
}
return ScriptVariable::Get(expr->m_Operand1);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpNegate(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return ~(long)expr->EvaluateOperand1(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpLogicalNegate(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return !expr->EvaluateOperand1(context).ToBool();
}
2014-11-06 19:35:47 +01:00
Value Expression::OpAdd(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context) + expr->EvaluateOperand2(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpSubtract(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context) - expr->EvaluateOperand2(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpMultiply(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context) * expr->EvaluateOperand2(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpDivide(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context) / expr->EvaluateOperand2(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpBinaryAnd(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context) & expr->EvaluateOperand2(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpBinaryOr(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context) | expr->EvaluateOperand2(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpShiftLeft(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context) << expr->EvaluateOperand2(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpShiftRight(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context) >> expr->EvaluateOperand2(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpEqual(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context) == expr->EvaluateOperand2(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpNotEqual(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context) != expr->EvaluateOperand2(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpLessThan(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context) < expr->EvaluateOperand2(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpGreaterThan(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context) > expr->EvaluateOperand2(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpLessThanOrEqual(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context) <= expr->EvaluateOperand2(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpGreaterThanOrEqual(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context) >= expr->EvaluateOperand2(context);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpIn(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
Value right = expr->EvaluateOperand2(context);
2014-03-28 12:17:22 +01:00
if (right.IsEmpty())
return false;
else if (!right.IsObjectType<Array>())
2014-10-26 19:59:49 +01:00
BOOST_THROW_EXCEPTION(ConfigError("Invalid right side argument for 'in' operator: " + JsonEncode(right)));
2014-11-06 19:35:47 +01:00
Value left = expr->EvaluateOperand1(context);
Array::Ptr arr = right;
bool found = false;
2014-03-24 09:15:45 +01:00
ObjectLock olock(arr);
BOOST_FOREACH(const Value& value, arr) {
if (value == left) {
found = true;
break;
}
}
return found;
}
2014-11-06 19:35:47 +01:00
Value Expression::OpNotIn(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return !OpIn(expr, context, dhint);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpLogicalAnd(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context).ToBool() && expr->EvaluateOperand2(context).ToBool();
}
2014-11-06 19:35:47 +01:00
Value Expression::OpLogicalOr(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
return expr->EvaluateOperand1(context).ToBool() || expr->EvaluateOperand2(context).ToBool();
}
2014-11-06 19:35:47 +01:00
Value Expression::OpFunctionCall(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
Value funcName = expr->EvaluateOperand1(context);
ScriptFunction::Ptr func;
if (funcName.IsObjectType<ScriptFunction>())
func = funcName;
else
func = ScriptFunction::GetByName(funcName);
if (!func)
BOOST_THROW_EXCEPTION(ConfigError("Function '" + funcName + "' does not exist."));
2014-11-06 19:35:47 +01:00
Array::Ptr arr = expr->EvaluateOperand2(context);
std::vector<Value> arguments;
2014-05-11 06:00:34 +02:00
for (Array::SizeType index = 0; index < arr->GetLength(); index++) {
2014-10-16 17:44:06 +02:00
const Expression::Ptr& aexpr = arr->Get(index);
2014-11-06 19:35:47 +01:00
arguments.push_back(aexpr->Evaluate(context));
}
return func->Invoke(arguments);
}
2014-11-06 19:35:47 +01:00
Value Expression::OpArray(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
Array::Ptr arr = expr->m_Operand1;
Array::Ptr result = make_shared<Array>();
if (arr) {
2014-05-11 06:00:34 +02:00
for (Array::SizeType index = 0; index < arr->GetLength(); index++) {
2014-10-16 17:44:06 +02:00
const Expression::Ptr& aexpr = arr->Get(index);
2014-11-06 19:35:47 +01:00
result->Add(aexpr->Evaluate(context));
}
}
return result;
2013-11-03 10:41:30 +01:00
}
2014-11-06 19:35:47 +01:00
Value Expression::OpDict(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
Array::Ptr arr = expr->m_Operand1;
bool in_place = expr->m_Operand2;
Dictionary::Ptr result = make_shared<Dictionary>();
2014-11-06 19:35:47 +01:00
result->Set("__parent", context);
2014-03-24 09:10:37 +01:00
if (arr) {
2014-05-11 06:00:34 +02:00
for (Array::SizeType index = 0; index < arr->GetLength(); index++) {
2014-10-16 17:44:06 +02:00
const Expression::Ptr& aexpr = arr->Get(index);
2014-11-06 19:35:47 +01:00
Object::Ptr acontext = in_place ? context : result;
aexpr->Evaluate(acontext, dhint);
2014-11-06 19:35:47 +01:00
if (HasField(acontext, "__result"))
break;
}
}
Dictionary::Ptr xresult = result->ShallowClone();
xresult->Remove("__parent");
return xresult;
}
2014-11-06 19:35:47 +01:00
Value Expression::OpSet(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
Array::Ptr left = expr->m_Operand1;
Array::Ptr indexer = left->Get(0);
int csop = left->Get(1);
DebugHint *sdhint = dhint;
Value parent, object;
String index;
for (Array::SizeType i = 0; i < indexer->GetLength(); i++) {
Expression::Ptr indexExpr = indexer->Get(i);
2014-11-06 19:35:47 +01:00
String tempindex = indexExpr->Evaluate(context, dhint);
if (i == indexer->GetLength() - 1)
index = tempindex;
if (i == 0) {
2014-11-06 19:35:47 +01:00
parent = context;
object = GetField(context, tempindex);
} else {
parent = object;
Expression::Ptr eparent = make_shared<Expression>(&Expression::OpLiteral, parent, expr->m_DebugInfo);
Expression::Ptr eindex = make_shared<Expression>(&Expression::OpLiteral, tempindex, expr->m_DebugInfo);
Expression::Ptr eip = make_shared<Expression>(&Expression::OpIndexer, eparent, eindex, expr->m_DebugInfo);
2014-11-06 19:35:47 +01:00
object = eip->Evaluate(context, dhint);
}
if (sdhint)
sdhint = sdhint->GetChild(index);
if (i != indexer->GetLength() - 1 && object.IsEmpty()) {
object = make_shared<Dictionary>();
2014-11-06 19:35:47 +01:00
SetField(parent, tempindex, object);
}
}
2014-11-06 19:35:47 +01:00
Value right = expr->EvaluateOperand2(context, dhint);
if (csop != OpSetLiteral) {
Expression::OpCallback op;
switch (csop) {
case OpSetAdd:
op = &Expression::OpAdd;
break;
case OpSetSubtract:
op = &Expression::OpSubtract;
break;
case OpSetMultiply:
op = &Expression::OpMultiply;
break;
case OpSetDivide:
op = &Expression::OpDivide;
break;
default:
VERIFY(!"Invalid opcode.");
}
Expression::Ptr ecp = make_shared<Expression>(op,
make_shared<Expression>(&Expression::OpLiteral, object, expr->m_DebugInfo),
make_shared<Expression>(&Expression::OpLiteral, right, expr->m_DebugInfo),
expr->m_DebugInfo);
2014-11-06 19:35:47 +01:00
right = ecp->Evaluate(context, dhint);
}
2014-11-06 19:35:47 +01:00
SetField(parent, index, right);
if (sdhint)
sdhint->AddMessage("=", expr->m_DebugInfo);
return right;
}
2014-11-06 19:35:47 +01:00
Value Expression::OpIndexer(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
Value value = expr->EvaluateOperand1(context);
Value index = expr->EvaluateOperand2(context);
2014-03-28 12:17:22 +01:00
if (value.IsObjectType<Dictionary>()) {
Dictionary::Ptr dict = value;
return dict->Get(index);
} else if (value.IsObjectType<Array>()) {
Array::Ptr arr = value;
return arr->Get(index);
2014-03-28 12:17:22 +01:00
} else if (value.IsObjectType<Object>()) {
Object::Ptr object = value;
2014-11-03 00:44:04 +01:00
Type::Ptr type = object->GetReflectionType();
2014-03-28 12:17:22 +01:00
if (!type)
BOOST_THROW_EXCEPTION(ConfigError("Dot operator applied to object which does not support reflection"));
int field = type->GetFieldId(index);
if (field == -1)
BOOST_THROW_EXCEPTION(ConfigError("Tried to access invalid property '" + index + "'"));
return object->GetField(field);
2014-03-28 16:32:15 +01:00
} else if (value.IsEmpty()) {
return Empty;
2014-03-28 12:17:22 +01:00
} else {
BOOST_THROW_EXCEPTION(ConfigError("Dot operator cannot be applied to type '" + value.GetTypeName() + "'"));
}
}
2014-11-06 19:35:47 +01:00
Value Expression::OpImport(const Expression *expr, const Object::Ptr& context, DebugHint *dhint)
{
2014-11-06 19:35:47 +01:00
Value type = expr->EvaluateOperand1(context);
Value name = expr->EvaluateOperand2(context);
ConfigItem::Ptr item = ConfigItem::GetObject(type, name);
if (!item)
BOOST_THROW_EXCEPTION(ConfigError("Import references unknown template: '" + name + "'"));
2014-11-06 19:35:47 +01:00
item->GetExpressionList()->Evaluate(context, dhint);
return Empty;
}
2014-11-06 19:35:47 +01:00
Value Expression::FunctionWrapper(const std::vector<Value>& arguments, const Array::Ptr& funcargs, const Expression::Ptr& expr, const Object::Ptr& scope)
{
if (arguments.size() < funcargs->GetLength())
BOOST_THROW_EXCEPTION(ConfigError("Too few arguments for function"));
2014-11-06 19:35:47 +01:00
Dictionary::Ptr context = make_shared<Dictionary>();
context->Set("__parent", scope);
2014-05-11 06:00:34 +02:00
for (std::vector<Value>::size_type i = 0; i < std::min(arguments.size(), funcargs->GetLength()); i++)
2014-11-06 19:35:47 +01:00
context->Set(funcargs->Get(i), arguments[i]);
2014-11-06 19:35:47 +01:00
expr->Evaluate(context);
return context->Get("__result");
}
2014-11-06 19:35:47 +01:00
Value Expression::OpFunction(const Expression* expr, const Object::Ptr& context, DebugHint *dhint)
{
Array::Ptr left = expr->m_Operand1;
2014-10-16 17:44:06 +02:00
Expression::Ptr aexpr = left->Get(1);
String name = left->Get(0);
Array::Ptr funcargs = expr->m_Operand2;
2014-11-06 19:35:47 +01:00
ScriptFunction::Ptr func = make_shared<ScriptFunction>(boost::bind(&Expression::FunctionWrapper, _1, funcargs, aexpr, context));
if (!name.IsEmpty())
ScriptFunction::Register(name, func);
return func;
}
2014-11-06 19:35:47 +01:00
Value Expression::OpApply(const Expression* expr, const Object::Ptr& context, DebugHint *dhint)
{
Array::Ptr left = expr->m_Operand1;
2014-10-16 17:44:06 +02:00
Expression::Ptr exprl = expr->m_Operand2;
String type = left->Get(0);
String target = left->Get(1);
2014-10-16 17:44:06 +02:00
Expression::Ptr aname = left->Get(2);
Expression::Ptr filter = left->Get(3);
String fkvar = left->Get(4);
String fvvar = left->Get(5);
Expression::Ptr fterm = left->Get(6);
2014-11-06 19:35:47 +01:00
String name = aname->Evaluate(context, dhint);
2014-11-06 19:35:47 +01:00
ApplyRule::AddRule(type, target, name, exprl, filter, fkvar, fvvar, fterm, expr->m_DebugInfo, context);
return Empty;
}
2014-11-06 19:35:47 +01:00
Value Expression::OpObject(const Expression* expr, const Object::Ptr& context, DebugHint *dhint)
{
Array::Ptr left = expr->m_Operand1;
2014-10-16 17:44:06 +02:00
Expression::Ptr exprl = expr->m_Operand2;
bool abstract = left->Get(0);
String type = left->Get(1);
2014-10-16 17:44:06 +02:00
Expression::Ptr aname = left->Get(2);
Expression::Ptr filter = left->Get(3);
String zone = left->Get(4);
2014-11-06 19:35:47 +01:00
String name = aname->Evaluate(context, dhint);
ConfigItemBuilder::Ptr item = make_shared<ConfigItemBuilder>(expr->m_DebugInfo);
String checkName = name;
if (!abstract) {
2014-11-03 00:44:04 +01:00
shared_ptr<NameComposer> nc = dynamic_pointer_cast<NameComposer>(Type::GetByName(type));
if (nc)
checkName = nc->MakeName(name, Dictionary::Ptr());
}
if (!checkName.IsEmpty()) {
ConfigItem::Ptr oldItem = ConfigItem::GetObject(type, checkName);
if (oldItem) {
std::ostringstream msgbuf;
msgbuf << "Object '" << name << "' of type '" << type << "' re-defined: " << expr->m_DebugInfo << "; previous definition: " << oldItem->GetDebugInfo();
BOOST_THROW_EXCEPTION(ConfigError(msgbuf.str()) << errinfo_debuginfo(expr->m_DebugInfo));
}
}
item->SetType(type);
if (name.FindFirstOf("!") != String::NPos) {
std::ostringstream msgbuf;
msgbuf << "Name for object '" << name << "' of type '" << type << "' is invalid: Object names may not contain '!'";
BOOST_THROW_EXCEPTION(ConfigError(msgbuf.str()) << errinfo_debuginfo(expr->m_DebugInfo));
}
item->SetName(name);
item->AddExpression(exprl);
item->SetAbstract(abstract);
2014-11-06 19:35:47 +01:00
item->SetScope(context);
item->SetZone(zone);
item->Compile()->Register();
2014-11-06 19:35:47 +01:00
ObjectRule::AddRule(type, name, exprl, filter, expr->m_DebugInfo, context);
return Empty;
}
2014-05-10 11:26:56 +02:00
2014-11-06 19:35:47 +01:00
Value Expression::OpFor(const Expression* expr, const Object::Ptr& context, DebugHint *dhint)
2014-05-10 11:26:56 +02:00
{
Array::Ptr left = expr->m_Operand1;
String kvar = left->Get(0);
String vvar = left->Get(1);
Expression::Ptr aexpr = left->Get(2);
2014-10-16 17:44:06 +02:00
Expression::Ptr ascope = expr->m_Operand2;
2014-05-10 11:26:56 +02:00
2014-11-06 19:35:47 +01:00
Value value = aexpr->Evaluate(context, dhint);
2014-05-10 11:26:56 +02:00
if (value.IsObjectType<Array>()) {
if (!vvar.IsEmpty())
BOOST_THROW_EXCEPTION(ConfigError("Cannot use dictionary iterator for array.") << errinfo_debuginfo(expr->m_DebugInfo));
2014-05-10 11:26:56 +02:00
Array::Ptr arr = value;
ObjectLock olock(arr);
BOOST_FOREACH(const Value& value, arr) {
2014-11-06 19:35:47 +01:00
Dictionary::Ptr xcontext = make_shared<Dictionary>();
xcontext->Set("__parent", context);
xcontext->Set(kvar, value);
2014-11-06 19:35:47 +01:00
ascope->Evaluate(xcontext, dhint);
}
} else if (value.IsObjectType<Dictionary>()) {
if (vvar.IsEmpty())
BOOST_THROW_EXCEPTION(ConfigError("Cannot use array iterator for dictionary.") << errinfo_debuginfo(expr->m_DebugInfo));
Dictionary::Ptr dict = value;
ObjectLock olock(dict);
BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
2014-11-06 19:35:47 +01:00
Dictionary::Ptr xcontext = make_shared<Dictionary>();
xcontext->Set("__parent", context);
xcontext->Set(kvar, kv.first);
xcontext->Set(vvar, kv.second);
2014-11-06 19:35:47 +01:00
ascope->Evaluate(xcontext, dhint);
}
} else
BOOST_THROW_EXCEPTION(ConfigError("Invalid type in __for expression: " + value.GetTypeName()) << errinfo_debuginfo(expr->m_DebugInfo));
2014-05-10 11:26:56 +02:00
return Empty;
}
Dictionary::Ptr DebugHint::ToDictionary(void) const
{
Dictionary::Ptr result = make_shared<Dictionary>();
Array::Ptr messages = make_shared<Array>();
typedef std::pair<String, DebugInfo> MessageType;
BOOST_FOREACH(const MessageType& message, Messages) {
Array::Ptr amsg = make_shared<Array>();
amsg->Add(message.first);
amsg->Add(message.second.Path);
amsg->Add(message.second.FirstLine);
amsg->Add(message.second.FirstColumn);
amsg->Add(message.second.LastLine);
amsg->Add(message.second.LastColumn);
messages->Add(amsg);
}
result->Set("messages", messages);
Dictionary::Ptr properties = make_shared<Dictionary>();
typedef std::map<String, DebugHint>::value_type ChildType;
BOOST_FOREACH(const ChildType& kv, Children) {
properties->Set(kv.first, kv.second.ToDictionary());
}
result->Set("properties", properties);
return result;
}
Expression::Ptr icinga::MakeLiteral(const Value& lit)
{
return make_shared<Expression>(&Expression::OpLiteral, lit, DebugInfo());
}
2014-11-06 19:35:47 +01:00
bool Expression::HasField(const Object::Ptr& context, const String& field)
{
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
if (dict)
return dict->Contains(field);
else {
Type::Ptr type = context->GetReflectionType();
if (!type)
return false;
return type->GetFieldId(field) != -1;
}
}
Value Expression::GetField(const Object::Ptr& context, const String& field)
{
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
if (dict)
return dict->Get(field);
else {
Type::Ptr type = context->GetReflectionType();
if (!type)
return Empty;
int fid = type->GetFieldId(field);
if (fid == -1)
return Empty;
return context->GetField(fid);
}
}
void Expression::SetField(const Object::Ptr& context, const String& field, const Value& value)
{
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(context);
if (dict)
dict->Set(field, value);
else {
Type::Ptr type = context->GetReflectionType();
if (!type)
BOOST_THROW_EXCEPTION(ConfigError("Cannot set field on object."));
int fid = type->GetFieldId(field);
if (fid == -1)
BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' does not exist."));
try {
context->SetField(fid, value);
} catch (const boost::bad_lexical_cast&) {
BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'"));
} catch (const std::bad_cast&) {
BOOST_THROW_EXCEPTION(ConfigError("Attribute '" + field + "' cannot be set to value of type '" + value.GetTypeName() + "'"));
}
}
}