icinga2/lib/config/expression.cpp

239 lines
6.5 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
2014-01-09 00:32:11 +01:00
* Copyright (C) 2012-present 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. *
******************************************************************************/
2013-03-17 20:19:29 +01:00
#include "config/expression.h"
#include "config/expressionlist.h"
2013-03-16 21:18:53 +01:00
#include "base/objectlock.h"
2013-08-28 08:18:58 +02:00
#include "base/debug.h"
2013-03-18 12:55:32 +01:00
#include "base/array.h"
2013-03-16 21:18:53 +01:00
#include <sstream>
#include <boost/foreach.hpp>
using namespace icinga;
2012-09-19 12:32:39 +02:00
Expression::Expression(const String& key, ExpressionOperator op,
const Value& value, const DebugInfo& debuginfo)
: m_Key(key), m_Operator(op), m_Value(value), m_DebugInfo(debuginfo)
{
ASSERT(op != OperatorExecute || value.IsObjectType<ExpressionList>());
}
Value Expression::DeepClone(const Value& value)
{
if (value.IsObjectType<Array>()) {
Array::Ptr array = value;
Array::Ptr result = make_shared<Array>();
ObjectLock olock(array);
BOOST_FOREACH(const Value& item, array) {
result->Add(DeepClone(item));
}
return result;
} else if (value.IsObjectType<Dictionary>()) {
Dictionary::Ptr dict = value;
Dictionary::Ptr result = make_shared<Dictionary>();
ObjectLock olock(dict);
BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
result->Set(kv.first, DeepClone(kv.second));
}
return result;
}
return value;
}
void Expression::Execute(const Dictionary::Ptr& dictionary) const
{
Value oldValue, newValue;
ExpressionList::Ptr valueExprl;
Dictionary::Ptr valueDict;
2013-03-18 12:55:32 +01:00
Array::Ptr valueArray;
2012-07-11 20:55:46 +02:00
if (m_Value.IsObjectType<ExpressionList>())
valueExprl = m_Value;
if (m_Value.IsObjectType<Dictionary>())
valueDict = m_Value;
2013-03-18 12:55:32 +01:00
if (m_Value.IsObjectType<Array>())
valueArray = m_Value;
newValue = m_Value;
Dictionary::Ptr dict;
2013-03-18 12:55:32 +01:00
Array::Ptr array;
switch (m_Operator) {
2013-09-24 13:13:14 +02:00
case OperatorNop:
/* Nothing to do here. */
return;
2012-07-06 14:33:10 +02:00
case OperatorExecute:
if (!valueExprl)
2013-03-16 21:18:53 +01:00
BOOST_THROW_EXCEPTION(std::invalid_argument("Operand for OperatorExecute must be an ExpressionList."));
2012-07-06 14:33:10 +02:00
valueExprl->Execute(dictionary);
return;
case OperatorSet:
if (valueExprl) {
dict = make_shared<Dictionary>();
valueExprl->Execute(dict);
newValue = dict;
} else {
newValue = DeepClone(newValue);
}
break;
case OperatorPlus:
2013-03-04 15:52:42 +01:00
oldValue = dictionary->Get(m_Key);
2012-06-11 08:21:47 +02:00
2012-07-11 20:55:46 +02:00
if (oldValue.IsObjectType<Dictionary>())
dict = oldValue;
2013-03-18 12:55:32 +01:00
if (oldValue.IsObjectType<Array>())
array = oldValue;
if (valueExprl) {
2013-03-18 12:55:32 +01:00
if (!dict)
dict = make_shared<Dictionary>();
2013-03-18 12:55:32 +01:00
valueExprl->Execute(dict);
2013-03-18 12:55:32 +01:00
newValue = dict;
} else if (valueDict) {
2013-03-18 12:55:32 +01:00
if (!dict)
dict = make_shared<Dictionary>();
2013-03-18 12:55:32 +01:00
2013-03-01 12:07:52 +01:00
ObjectLock olock(valueDict);
String key;
Value value;
BOOST_FOREACH(const Dictionary::Pair& kv, valueDict) {
dict->Set(kv.first, DeepClone(kv.second));
}
2013-03-18 12:55:32 +01:00
newValue = dict;
} else if (valueArray) {
if (!array)
array = make_shared<Array>();
2013-03-18 12:55:32 +01:00
ObjectLock olock(valueArray);
BOOST_FOREACH(const Value& value, valueArray) {
array->Add(DeepClone(value));
2013-03-18 12:55:32 +01:00
}
newValue = array;
} else {
2013-03-16 21:18:53 +01:00
std::ostringstream message;
2013-03-18 12:55:32 +01:00
message << "+= only works for dictionaries and arrays ("
2012-09-19 12:32:39 +02:00
<< m_DebugInfo << ")";
2013-03-16 21:18:53 +01:00
BOOST_THROW_EXCEPTION(std::invalid_argument(message.str()));
}
break;
default:
2013-03-16 21:18:53 +01:00
BOOST_THROW_EXCEPTION(std::runtime_error("Not yet implemented."));
}
dictionary->Set(m_Key, newValue);
}
void Expression::ExtractPath(const std::vector<String>& path, const ExpressionList::Ptr& result) const
{
ASSERT(!path.empty());
if (path[0] == m_Key) {
2013-03-19 14:13:58 +01:00
if (!m_Value.IsObjectType<ExpressionList>())
BOOST_THROW_EXCEPTION(std::invalid_argument("Specified path does not exist."));
ExpressionList::Ptr exprl = m_Value;
if (path.size() == 1) {
2013-03-19 14:13:58 +01:00
result->AddExpression(Expression("", OperatorExecute, exprl, m_DebugInfo));
return;
}
std::vector<String> sub_path(path.begin() + 1, path.end());
exprl->ExtractPath(sub_path, result);
} else if (m_Operator == OperatorExecute) {
ExpressionList::Ptr exprl = m_Value;
exprl->ExtractPath(path, result);
}
}
void Expression::ExtractFiltered(const std::set<String>& keys, const shared_ptr<ExpressionList>& result) const
{
if (keys.find(m_Key) != keys.end()) {
result->AddExpression(*this);
} else if (m_Operator == OperatorExecute) {
ExpressionList::Ptr exprl = m_Value;
exprl->ExtractFiltered(keys, result);
}
}
2013-09-24 13:13:14 +02:00
void Expression::ErasePath(const std::vector<String>& path)
{
ASSERT(!path.empty());
if (path[0] == m_Key) {
if (path.size() == 1) {
m_Operator = OperatorNop;
} else if (m_Value.IsObjectType<ExpressionList>()) {
ExpressionList::Ptr exprl = m_Value;
std::vector<String> sub_path(path.begin() + 1, path.end());
exprl->ErasePath(sub_path);
}
} else if (m_Operator == OperatorExecute) {
ExpressionList::Ptr exprl = m_Value;
exprl->ErasePath(path);
}
}
void Expression::FindDebugInfoPath(const std::vector<String>& path, DebugInfo& result) const
{
ASSERT(!path.empty());
if (path[0] == m_Key) {
if (path.size() == 1) {
result = m_DebugInfo;
} else if (m_Value.IsObjectType<ExpressionList>()) {
ExpressionList::Ptr exprl = m_Value;
std::vector<String> sub_path(path.begin() + 1, path.end());
exprl->FindDebugInfoPath(sub_path, result);
}
} else if (m_Operator == OperatorExecute) {
ExpressionList::Ptr exprl = m_Value;
exprl->FindDebugInfoPath(path, result);
}
}