From 0915b3ef56dfac3113cce55a59a5765dc94976be Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 14:34:54 +0200 Subject: Adding upstream version 2.13.6. Signed-off-by: Daniel Baumann --- lib/config/configitembuilder.cpp | 120 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 lib/config/configitembuilder.cpp (limited to 'lib/config/configitembuilder.cpp') diff --git a/lib/config/configitembuilder.cpp b/lib/config/configitembuilder.cpp new file mode 100644 index 0000000..f7a3ead --- /dev/null +++ b/lib/config/configitembuilder.cpp @@ -0,0 +1,120 @@ +/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */ + +#include "config/configitembuilder.hpp" +#include "base/configtype.hpp" +#include + +using namespace icinga; + +ConfigItemBuilder::ConfigItemBuilder(const DebugInfo& debugInfo) + : m_Abstract(false), m_DefaultTmpl(false), m_IgnoreOnError(false) +{ + m_DebugInfo = debugInfo; +} + +void ConfigItemBuilder::SetType(const Type::Ptr& type) +{ + ASSERT(type); + m_Type = type; +} + +void ConfigItemBuilder::SetName(const String& name) +{ + m_Name = name; +} + +void ConfigItemBuilder::SetAbstract(bool abstract) +{ + m_Abstract = abstract; +} + +void ConfigItemBuilder::SetScope(const Dictionary::Ptr& scope) +{ + m_Scope = scope; +} + +void ConfigItemBuilder::SetZone(const String& zone) +{ + m_Zone = zone; +} + +void ConfigItemBuilder::SetPackage(const String& package) +{ + m_Package = package; +} + +void ConfigItemBuilder::AddExpression(Expression *expr) +{ + m_Expressions.emplace_back(expr); +} + +void ConfigItemBuilder::SetFilter(const Expression::Ptr& filter) +{ + m_Filter = filter; +} + +void ConfigItemBuilder::SetDefaultTemplate(bool defaultTmpl) +{ + m_DefaultTmpl = defaultTmpl; +} + +void ConfigItemBuilder::SetIgnoreOnError(bool ignoreOnError) +{ + m_IgnoreOnError = ignoreOnError; +} + +ConfigItem::Ptr ConfigItemBuilder::Compile() +{ + if (!m_Type) { + std::ostringstream msgbuf; + msgbuf << "The type of an object must be specified"; + BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), m_DebugInfo)); + } + + auto *ctype = dynamic_cast(m_Type.get()); + + if (!ctype) { + std::ostringstream msgbuf; + msgbuf << "The type '" + m_Type->GetName() + "' cannot be used for config objects"; + BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), m_DebugInfo)); + } + + if (m_Name.FindFirstOf("!") != String::NPos) { + std::ostringstream msgbuf; + msgbuf << "Name for object '" << m_Name << "' of type '" << m_Type->GetName() << "' is invalid: Object names may not contain '!'"; + BOOST_THROW_EXCEPTION(ScriptError(msgbuf.str(), m_DebugInfo)); + } + + std::vector > exprs; + + Array::Ptr templateArray = new Array({ m_Name }); + + exprs.emplace_back(new SetExpression(MakeIndexer(ScopeThis, "templates"), OpSetAdd, + std::unique_ptr(new LiteralExpression(templateArray)), m_DebugInfo)); + +#ifdef I2_DEBUG + if (!m_Abstract) { + bool foundDefaultImport = false; + + for (const std::unique_ptr& expr : m_Expressions) { + if (dynamic_cast(expr.get())) { + foundDefaultImport = true; + break; + } + } + + ASSERT(foundDefaultImport); + } +#endif /* I2_DEBUG */ + + auto *dexpr = new DictExpression(std::move(m_Expressions), m_DebugInfo); + dexpr->MakeInline(); + exprs.emplace_back(dexpr); + + auto exprl = new DictExpression(std::move(exprs), m_DebugInfo); + exprl->MakeInline(); + + return new ConfigItem(m_Type, m_Name, m_Abstract, exprl, m_Filter, + m_DefaultTmpl, m_IgnoreOnError, m_DebugInfo, m_Scope, m_Zone, m_Package); +} + -- cgit v1.2.3