ResolverSpec: add option not to resolve "$name$"

but only "$host.name$".
This commit is contained in:
Alexander A. Klimov 2020-09-30 16:35:47 +02:00
parent 91901eafd8
commit a309b4a415
2 changed files with 23 additions and 6 deletions

View File

@ -89,11 +89,14 @@ bool MacroProcessor::ResolveMacro(const String& macro, const ResolverList& resol
} }
for (const ResolverSpec& resolver : resolvers) { for (const ResolverSpec& resolver : resolvers) {
if (!objName.IsEmpty() && objName != resolver.first) if (!objName.IsEmpty() && objName != resolver.Name)
continue; continue;
if (objName.IsEmpty()) { if (objName.IsEmpty()) {
CustomVarObject::Ptr dobj = dynamic_pointer_cast<CustomVarObject>(resolver.second); if (!resolver.ResolveShortMacros)
continue;
CustomVarObject::Ptr dobj = dynamic_pointer_cast<CustomVarObject>(resolver.Obj);
if (dobj) { if (dobj) {
Dictionary::Ptr vars = dobj->GetVars(); Dictionary::Ptr vars = dobj->GetVars();
@ -106,12 +109,12 @@ bool MacroProcessor::ResolveMacro(const String& macro, const ResolverList& resol
} }
} }
auto *mresolver = dynamic_cast<MacroResolver *>(resolver.second.get()); auto *mresolver = dynamic_cast<MacroResolver *>(resolver.Obj.get());
if (mresolver && mresolver->ResolveMacro(boost::algorithm::join(tokens, "."), cr, result)) if (mresolver && mresolver->ResolveMacro(boost::algorithm::join(tokens, "."), cr, result))
return true; return true;
Value ref = resolver.second; Value ref = resolver.Obj;
bool valid = true; bool valid = true;
for (const String& token : tokens) { for (const String& token : tokens) {
@ -172,7 +175,7 @@ Value MacroProcessor::EvaluateFunction(const Function::Ptr& func, const Resolver
Dictionary::Ptr resolvers_this = new Dictionary(); Dictionary::Ptr resolvers_this = new Dictionary();
for (const ResolverSpec& resolver : resolvers) { for (const ResolverSpec& resolver : resolvers) {
resolvers_this->Set(resolver.first, resolver.second); resolvers_this->Set(resolver.Name, resolver.Obj);
} }
auto internalResolveMacrosShim = [resolvers, cr, resolvedMacros, useResolvedMacros, recursionLevel](const std::vector<Value>& args) { auto internalResolveMacrosShim = [resolvers, cr, resolvedMacros, useResolvedMacros, recursionLevel](const std::vector<Value>& args) {

View File

@ -7,6 +7,7 @@
#include "icinga/checkable.hpp" #include "icinga/checkable.hpp"
#include "base/value.hpp" #include "base/value.hpp"
#include <vector> #include <vector>
#include <utility>
namespace icinga namespace icinga
{ {
@ -19,8 +20,21 @@ namespace icinga
class MacroProcessor class MacroProcessor
{ {
public: public:
struct ResolverSpec
{
String Name;
Object::Ptr Obj;
// Whether to resolve not only e.g. $host.address$, but also just $address$
bool ResolveShortMacros;
inline ResolverSpec(String name, Object::Ptr obj, bool resolveShortMacros = true)
: Name(std::move(name)), Obj(std::move(obj)), ResolveShortMacros(resolveShortMacros)
{
}
};
typedef std::function<Value (const Value&)> EscapeCallback; typedef std::function<Value (const Value&)> EscapeCallback;
typedef std::pair<String, Object::Ptr> ResolverSpec;
typedef std::vector<ResolverSpec> ResolverList; typedef std::vector<ResolverSpec> ResolverList;
static Value ResolveMacros(const Value& str, const ResolverList& resolvers, static Value ResolveMacros(const Value& str, const ResolverList& resolvers,