2012-05-25 11:10:11 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 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-03-19 07:12:16 +01:00
|
|
|
#include "base/utility.h"
|
2013-03-18 12:55:32 +01:00
|
|
|
#include "base/array.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <sstream>
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/smart_ptr/make_shared.hpp>
|
|
|
|
#include <boost/foreach.hpp>
|
2012-05-25 11:10:11 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
2012-05-31 16:04:51 +02:00
|
|
|
|
2012-09-19 12:32:39 +02:00
|
|
|
Expression::Expression(const String& key, ExpressionOperator op,
|
|
|
|
const Value& value, const DebugInfo& debuginfo)
|
2012-06-06 14:38:28 +02:00
|
|
|
: m_Key(key), m_Operator(op), m_Value(value), m_DebugInfo(debuginfo)
|
2012-05-31 16:04:51 +02:00
|
|
|
{
|
2013-03-27 13:49:58 +01:00
|
|
|
ASSERT(op != OperatorExecute || value.IsObjectType<ExpressionList>());
|
2012-06-01 16:49:33 +02:00
|
|
|
}
|
|
|
|
|
2012-06-05 15:05:15 +02:00
|
|
|
void Expression::Execute(const Dictionary::Ptr& dictionary) const
|
2012-06-01 16:49:33 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
Value oldValue, newValue;
|
2012-06-01 16:49:33 +02:00
|
|
|
|
2012-07-06 11:22:38 +02:00
|
|
|
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;
|
2012-06-05 15:05:15 +02:00
|
|
|
|
2013-03-18 12:55:32 +01:00
|
|
|
if (m_Value.IsObjectType<Array>())
|
|
|
|
valueArray = m_Value;
|
|
|
|
|
2012-06-06 14:38:28 +02:00
|
|
|
newValue = m_Value;
|
2012-06-05 15:05:15 +02:00
|
|
|
|
2012-07-06 11:22:38 +02:00
|
|
|
Dictionary::Ptr dict;
|
2013-03-18 12:55:32 +01:00
|
|
|
Array::Ptr array;
|
2012-07-06 11:22:38 +02:00
|
|
|
|
2012-06-06 14:38:28 +02:00
|
|
|
switch (m_Operator) {
|
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;
|
|
|
|
|
2012-06-01 16:49:33 +02:00
|
|
|
case OperatorSet:
|
2012-07-06 11:22:38 +02:00
|
|
|
if (valueExprl) {
|
|
|
|
dict = boost::make_shared<Dictionary>();
|
|
|
|
valueExprl->Execute(dict);
|
2012-06-05 15:05:15 +02:00
|
|
|
newValue = dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2012-06-01 16:49:33 +02:00
|
|
|
|
2013-03-18 12:55:32 +01:00
|
|
|
if (oldValue.IsObjectType<Array>())
|
|
|
|
array = oldValue;
|
2012-07-06 11:22:38 +02:00
|
|
|
|
|
|
|
if (valueExprl) {
|
2013-03-18 12:55:32 +01:00
|
|
|
if (!dict)
|
|
|
|
dict = boost::make_shared<Dictionary>();
|
|
|
|
|
2012-07-06 11:22:38 +02:00
|
|
|
valueExprl->Execute(dict);
|
2013-03-18 12:55:32 +01:00
|
|
|
|
|
|
|
newValue = dict;
|
2012-07-06 11:22:38 +02:00
|
|
|
} else if (valueDict) {
|
2013-03-18 12:55:32 +01:00
|
|
|
if (!dict)
|
|
|
|
dict = boost::make_shared<Dictionary>();
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
ObjectLock olock(valueDict);
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String key;
|
|
|
|
Value value;
|
2013-03-15 18:21:29 +01:00
|
|
|
BOOST_FOREACH(boost::tie(key, value), valueDict) {
|
2012-07-16 22:00:50 +02:00
|
|
|
dict->Set(key, value);
|
2012-07-06 11:22:38 +02:00
|
|
|
}
|
2013-03-18 12:55:32 +01:00
|
|
|
|
|
|
|
newValue = dict;
|
|
|
|
} else if (valueArray) {
|
|
|
|
if (!array)
|
|
|
|
array = boost::make_shared<Array>();
|
|
|
|
|
|
|
|
|
|
|
|
ObjectLock olock(valueArray);
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Value& value, valueArray) {
|
|
|
|
array->Add(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
newValue = array;
|
2012-06-05 15:05:15 +02:00
|
|
|
} 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()));
|
2012-06-01 16:49:33 +02:00
|
|
|
}
|
|
|
|
|
2012-06-05 15:05:15 +02:00
|
|
|
break;
|
2012-06-01 16:49:33 +02:00
|
|
|
|
2012-06-05 15:05:15 +02:00
|
|
|
default:
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Not yet implemented."));
|
2012-06-01 16:49:33 +02:00
|
|
|
}
|
|
|
|
|
2013-03-14 12:40:02 +01:00
|
|
|
dictionary->Set(m_Key, newValue);
|
2012-05-31 16:04:51 +02:00
|
|
|
}
|
2013-01-30 23:02:46 +01:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
void Expression::DumpValue(std::ostream& fp, int indent, const Value& value, bool inlineDict)
|
2013-01-30 23:02:46 +01:00
|
|
|
{
|
|
|
|
ExpressionList::Ptr valueExprl;
|
|
|
|
Dictionary::Ptr valueDict;
|
|
|
|
if (value.IsObjectType<ExpressionList>()) {
|
|
|
|
if (!inlineDict)
|
|
|
|
fp << "{ " << "\n";
|
|
|
|
|
|
|
|
static_cast<ExpressionList::Ptr>(value)->Dump(fp, indent);
|
|
|
|
|
|
|
|
if (!inlineDict) {
|
|
|
|
for (int i = 0; i < indent - 1; i++)
|
|
|
|
fp << "\t";
|
|
|
|
|
|
|
|
fp << "}";
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value.IsObjectType<Dictionary>()) {
|
|
|
|
if (!inlineDict)
|
|
|
|
fp << "{ " << "\n";
|
|
|
|
|
|
|
|
String k;
|
|
|
|
Value v;
|
2013-03-15 18:21:29 +01:00
|
|
|
BOOST_FOREACH(boost::tie(k, v), static_cast<Dictionary::Ptr>(value)) {
|
2013-01-30 23:15:37 +01:00
|
|
|
for (int i = 0; i < indent; i++)
|
|
|
|
fp << "\t";
|
|
|
|
|
2013-01-30 23:02:46 +01:00
|
|
|
fp << "\"" << k << "\" = ";
|
|
|
|
DumpValue(fp, indent, v);
|
|
|
|
fp << "," << "\n";
|
2013-01-30 23:15:37 +01:00
|
|
|
|
|
|
|
fp << "}";
|
2013-01-30 23:02:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!inlineDict)
|
2013-01-30 23:15:37 +01:00
|
|
|
fp << "}";
|
2013-01-30 23:02:46 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value.IsScalar()) {
|
|
|
|
fp << "\"" << static_cast<String>(value) << "\"";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Encountered unknown type while dumping value."));
|
2013-01-30 23:02:46 +01:00
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
void Expression::Dump(std::ostream& fp, int indent) const
|
2013-01-30 23:02:46 +01:00
|
|
|
{
|
|
|
|
if (m_Operator == OperatorExecute) {
|
|
|
|
DumpValue(fp, indent, m_Value, true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < indent; i++)
|
|
|
|
fp << "\t";
|
|
|
|
|
|
|
|
fp << "\"" << m_Key << "\" ";
|
|
|
|
|
|
|
|
switch (m_Operator) {
|
|
|
|
case OperatorSet:
|
|
|
|
fp << "=";
|
|
|
|
break;
|
|
|
|
case OperatorPlus:
|
|
|
|
fp << "+=";
|
|
|
|
break;
|
|
|
|
default:
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error("Not yet implemented."));
|
2013-01-30 23:02:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fp << " ";
|
|
|
|
|
|
|
|
DumpValue(fp, indent + 1, m_Value);
|
|
|
|
|
|
|
|
fp << ", " << "\n";
|
|
|
|
}
|
2013-03-19 07:09:06 +01:00
|
|
|
|
2013-03-19 14:47:19 +01:00
|
|
|
void Expression::ExtractPath(const std::vector<String>& path, const ExpressionList::Ptr& result) const
|
2013-03-19 07:09:06 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2013-03-19 13:37:09 +01:00
|
|
|
if (path.size() == 1) {
|
2013-03-19 14:13:58 +01:00
|
|
|
result->AddExpression(Expression("", OperatorExecute, exprl, m_DebugInfo));
|
2013-03-19 13:37:09 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<String> sub_path(path.begin() + 1, path.end());
|
2013-03-19 14:47:19 +01:00
|
|
|
exprl->ExtractPath(sub_path, result);
|
2013-03-19 07:09:06 +01:00
|
|
|
} else if (m_Operator == OperatorExecute) {
|
|
|
|
ExpressionList::Ptr exprl = m_Value;
|
2013-03-19 14:47:19 +01:00
|
|
|
exprl->ExtractPath(path, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Expression::ExtractFiltered(const std::set<String, string_iless>& 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-03-19 07:09:06 +01:00
|
|
|
}
|
|
|
|
}
|