Implement namespace support for the keys() function
authorGunnar Beutner <gunnar.beutner@icinga.com>
Tue, 7 Aug 2018 11:55:41 +0000 (13:55 +0200)
committerGunnar Beutner <gunnar.beutner@icinga.com>
Mon, 13 Aug 2018 11:44:31 +0000 (13:44 +0200)
lib/base/scriptutils.cpp
lib/base/scriptutils.hpp

index befccde5f4ac78ac22391b6d9dbb78a2845e771c..4aa0b3bcdef30dfbb77f6fc708736f4b1d68eb27 100644 (file)
@@ -30,6 +30,7 @@
 #include "base/application.hpp"
 #include "base/dependencygraph.hpp"
 #include "base/initialize.hpp"
+#include "base/namespace.hpp"
 #include <boost/regex.hpp>
 #include <algorithm>
 #include <set>
@@ -83,11 +84,11 @@ enum MatchType
 
 void ScriptUtils::StaticInitialize()
 {
-       ScriptGlobal::Set("MatchAll", MatchAll, true);
-       ScriptGlobal::Set("MatchAny", MatchAny, true);
+       ScriptGlobal::Set("System.MatchAll", MatchAll, true);
+       ScriptGlobal::Set("System.MatchAny", MatchAny, true);
 
-       ScriptGlobal::Set("GlobFile", GlobFile, true);
-       ScriptGlobal::Set("GlobDirectory", GlobDirectory, true);
+       ScriptGlobal::Set("System.GlobFile", GlobFile, true);
+       ScriptGlobal::Set("System.GlobDirectory", GlobDirectory, true);
 }
 
 String ScriptUtils::CastString(const Value& value)
@@ -397,10 +398,12 @@ Type::Ptr ScriptUtils::TypeOf(const Value& value)
        return value.GetReflectionType();
 }
 
-Array::Ptr ScriptUtils::Keys(const Dictionary::Ptr& obj)
+Array::Ptr ScriptUtils::Keys(const Object::Ptr& obj)
 {
        ArrayData result;
 
+       Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(obj);
+
        if (dict) {
                ObjectLock olock(dict);
                for (const Dictionary::Pair& kv : dict) {
@@ -408,6 +411,15 @@ Array::Ptr ScriptUtils::Keys(const Dictionary::Ptr& obj)
                }
        }
 
+       Namespace::Ptr ns = dynamic_pointer_cast<Namespace>(obj);
+
+       if (ns) {
+               ObjectLock olock(ns);
+               for (const Namespace::Pair& kv : ns) {
+                       result.push_back(kv.first);
+               }
+       }
+
        return new Array(std::move(result));
 }
 
index 9515b381e44ed7397bec497313b4e19925c41c7a..fcb80f6e43cf6bd9d715b0a8d7f9115c96f9e7f3 100644 (file)
@@ -49,7 +49,7 @@ public:
        static void Log(const std::vector<Value>& arguments);
        static Array::Ptr Range(const std::vector<Value>& arguments);
        static Type::Ptr TypeOf(const Value& value);
-       static Array::Ptr Keys(const Dictionary::Ptr& dict);
+       static Array::Ptr Keys(const Object::Ptr& obj);
        static ConfigObject::Ptr GetObject(const Value& type, const String& name);
        static Array::Ptr GetObjects(const Type::Ptr& type);
        static void Assert(const Value& arg);