]> granicus.if.org Git - icinga2/commitdiff
Implement the keys() function
authorGunnar Beutner <gunnar.beutner@netways.de>
Mon, 3 Nov 2014 12:05:14 +0000 (13:05 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Mon, 3 Nov 2014 12:05:14 +0000 (13:05 +0100)
fixes #7557

doc/7-configuring-icinga-2.md
lib/base/scriptutils.cpp
lib/base/scriptutils.hpp

index a0c2e3d221fdb52fef49a00912a95a1f5fcaf6ab..4412e324de9247155706184b28b6cabd271de9ec 100644 (file)
@@ -287,6 +287,7 @@ match(pattern, text)            | Returns true if the wildcard pattern matches t
 len(value)                      | Returns the length of the value, i.e. the number of elements for an array or dictionary, or the length of the string in bytes.
 union(array, array, ...)        | Returns an array containing all unique elements from the specified arrays.
 intersection(array, array, ...) | Returns an array containing all unique elements which are common to all specified arrays.
+keys(dict)                      | Returns an array containing the dictionary's keys.
 string(value)                   | Converts the value to a string.
 number(value)                   | Converts the value to a number.
 bool(value)                     | Converts the value to a bool.
index c7496640ed39bfd152619692ed6d4685293e39b5..cf654e542506f17f5f9ab2c0d5e62cff3c03ad34 100644 (file)
 #include "base/scriptfunction.hpp"
 #include "base/utility.hpp"
 #include "base/convert.hpp"
-#include "base/array.hpp"
-#include "base/dictionary.hpp"
 #include "base/json.hpp"
 #include "base/logger.hpp"
+#include "base/objectlock.hpp"
 #include <boost/foreach.hpp>
 #include <boost/regex.hpp>
 #include <algorithm>
@@ -41,6 +40,7 @@ REGISTER_SCRIPTFUNCTION(log, &ScriptUtils::Log);
 REGISTER_SCRIPTFUNCTION(range, &ScriptUtils::Range);
 REGISTER_SCRIPTFUNCTION(exit, &ScriptUtils::Exit);
 REGISTER_SCRIPTFUNCTION(typeof, &ScriptUtils::TypeOf);
+REGISTER_SCRIPTFUNCTION(keys, &ScriptUtils::Keys);
 
 bool ScriptUtils::Regex(const String& pattern, const String& text)
 {
@@ -196,3 +196,16 @@ Type::Ptr ScriptUtils::TypeOf(const Value& value)
                        VERIFY(!"Invalid value type.");
        }
 }
+
+Array::Ptr ScriptUtils::Keys(const Dictionary::Ptr& dict)
+{
+       Array::Ptr result = make_shared<Array>();
+
+       ObjectLock olock(dict);
+       BOOST_FOREACH(const Dictionary::Pair& kv, dict) {
+               result->Add(kv.first);
+       }
+
+       return result;
+}
+
index 2a4bb549f273c49b38c030726ad2814613488f7a..1ba25e0a48484669c4a8453079aa2f3bf6ed3f0f 100644 (file)
@@ -23,6 +23,7 @@
 #include "base/i2-base.hpp"
 #include "base/string.hpp"
 #include "base/array.hpp"
+#include "base/dictionary.hpp"
 #include "base/type.hpp"
 
 namespace icinga
@@ -42,6 +43,7 @@ public:
        static Array::Ptr Range(const std::vector<Value>& arguments);
        static void Exit(int code);
        static Type::Ptr TypeOf(const Value& value);
+       static Array::Ptr Keys(const Dictionary::Ptr& dict);
 
 private:
        ScriptUtils(void);