Implement the keys() function

fixes #7557
This commit is contained in:
Gunnar Beutner 2014-11-03 13:05:14 +01:00
parent e4a1572c9b
commit aa94563eb5
3 changed files with 18 additions and 2 deletions

View 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.

View File

@ -21,10 +21,9 @@
#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;
}

View 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);