libchromeos: add GetKeys() method to KeyValueStore
GetKeys returns the keys currently set in the key value store.
BUG=none
TEST=ran unit tests
Change-Id: Ifd1c1f7678df6fd9b68900e2573d5e71802aa004
Reviewed-on: https://chromium-review.googlesource.com/253430
Trybot-Ready: Aaron Kemp <kemp@google.com>
Tested-by: Aaron Kemp <kemp@google.com>
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Aaron Kemp <kemp@google.com>
(cherry picked from commit 4b93f8e27b4d611a0fa106716527231b1fe73db5)
Reviewed-on: https://chromium-review.googlesource.com/255785
Reviewed-by: Aaron Kemp <kemp@google.com>
Commit-Queue: Patrick Sosinski <sosinski@google.com>
Tested-by: Patrick Sosinski <sosinski@google.com>
diff --git a/libchromeos/chromeos/key_value_store.cc b/libchromeos/chromeos/key_value_store.cc
index 77da87c..8104d59 100644
--- a/libchromeos/chromeos/key_value_store.cc
+++ b/libchromeos/chromeos/key_value_store.cc
@@ -13,6 +13,7 @@
#include <base/strings/string_split.h>
#include <base/strings/string_util.h>
#include <chromeos/strings/string_utils.h>
+#include <chromeos/map_utils.h>
using std::map;
using std::string;
@@ -113,4 +114,8 @@
SetString(key, value ? kTrueValue : kFalseValue);
}
+std::vector<std::string> KeyValueStore::GetKeys() const {
+ return GetMapKeysAsVector(store_);
+}
+
} // namespace chromeos
diff --git a/libchromeos/chromeos/key_value_store.h b/libchromeos/chromeos/key_value_store.h
index bbc9963..a3c4fdf 100644
--- a/libchromeos/chromeos/key_value_store.h
+++ b/libchromeos/chromeos/key_value_store.h
@@ -11,6 +11,7 @@
#include <map>
#include <string>
+#include <vector>
#include <base/files/file_path.h>
#include <chromeos/chromeos_export.h>
@@ -49,6 +50,9 @@
// Boolean setter. Sets the value as "true" or "false".
void SetBoolean(const std::string& key, bool value);
+ // Retrieves the keys for all values currently stored in the map.
+ std::vector<std::string> GetKeys() const;
+
private:
// The map storing all the key-value pairs.
std::map<std::string, std::string> store_;
diff --git a/libchromeos/chromeos/key_value_store_unittest.cc b/libchromeos/chromeos/key_value_store_unittest.cc
index 702953d..c1a99d9 100644
--- a/libchromeos/chromeos/key_value_store_unittest.cc
+++ b/libchromeos/chromeos/key_value_store_unittest.cc
@@ -6,17 +6,20 @@
#include <map>
#include <string>
+#include <vector>
#include <base/files/file_util.h>
#include <base/files/scoped_temp_dir.h>
#include <base/logging.h>
#include <base/strings/string_util.h>
+#include <chromeos/map_utils.h>
#include <gtest/gtest.h>
using base::FilePath;
using base::ReadFileToString;
using std::map;
using std::string;
+using std::vector;
namespace chromeos {
@@ -196,4 +199,16 @@
EXPECT_FALSE(store_.Load(temp_file_));
}
+TEST_F(KeyValueStoreTest, GetKeys) {
+ map<string, string> entries = {
+ {"1", "apple"}, {"2", "banana"}, {"3", "cherry"}
+ };
+ for (const auto& it : entries) {
+ store_.SetString(it.first, it.second);
+ }
+
+ vector<string> keys = GetMapKeysAsVector(entries);
+ EXPECT_EQ(keys, store_.GetKeys());
+}
+
} // namespace chromeos