From 97ed912ab66d5d75b406cbdff00a09ec4dd95d32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20Pintari=C4=87?= <99442742+mpintaric55334@users.noreply.github.com> Date: Mon, 23 Oct 2023 15:29:41 +0200 Subject: [PATCH] Implement map key exists in mgp (#1336) --- include/_mgp.hpp | 2 ++ include/mg_procedure.h | 3 +++ include/mgp.hpp | 5 +++++ src/query/procedure/mg_procedure_impl.cpp | 12 ++++++++++++ tests/unit/cpp_api.cpp | 9 +++++++++ 5 files changed, 31 insertions(+) diff --git a/include/_mgp.hpp b/include/_mgp.hpp index 85c0ebf4c..e9c85e24c 100644 --- a/include/_mgp.hpp +++ b/include/_mgp.hpp @@ -355,6 +355,8 @@ inline size_t map_size(mgp_map *map) { return MgInvoke(mgp_map_size, map inline mgp_value *map_at(mgp_map *map, const char *key) { return MgInvoke(mgp_map_at, map, key); } +inline bool key_exists(mgp_map *map, const char *key) { return MgInvoke(mgp_key_exists, map, key); } + inline const char *map_item_key(mgp_map_item *item) { return MgInvoke(mgp_map_item_key, item); } inline mgp_value *map_item_value(mgp_map_item *item) { return MgInvoke(mgp_map_item_value, item); } diff --git a/include/mg_procedure.h b/include/mg_procedure.h index cf908ddce..cda319727 100644 --- a/include/mg_procedure.h +++ b/include/mg_procedure.h @@ -482,6 +482,9 @@ enum mgp_error mgp_map_size(struct mgp_map *map, size_t *result); /// Result is NULL if no mapping exists. enum mgp_error mgp_map_at(struct mgp_map *map, const char *key, struct mgp_value **result); +/// Returns true if key in map. +enum mgp_error mgp_key_exists(struct mgp_map *map, const char *key, int *result); + /// An item in the mgp_map. struct mgp_map_item; diff --git a/include/mgp.hpp b/include/mgp.hpp index 701b1e98d..8d6aa5bb0 100644 --- a/include/mgp.hpp +++ b/include/mgp.hpp @@ -630,6 +630,9 @@ class Map { /// @brief Returns the value at the given `key`. Value const At(std::string_view key) const; + /// @brief Returns true if the given `key` exists. + bool KeyExists(std::string_view key) const; + class Iterator { public: friend class Map; @@ -2573,6 +2576,8 @@ inline const Value Map::At(std::string_view key) const { return Value(); } +inline bool Map::KeyExists(std::string_view key) const { return mgp::key_exists(ptr_, key.data()); } + inline Map::Iterator::Iterator(mgp_map_items_iterator *map_items_iterator) : map_items_iterator_(map_items_iterator) { if (map_items_iterator_ == nullptr) return; if (mgp::map_items_iterator_get(map_items_iterator_) == nullptr) { diff --git a/src/query/procedure/mg_procedure_impl.cpp b/src/query/procedure/mg_procedure_impl.cpp index 7e72341a3..43e1a3232 100644 --- a/src/query/procedure/mg_procedure_impl.cpp +++ b/src/query/procedure/mg_procedure_impl.cpp @@ -1096,6 +1096,18 @@ mgp_error mgp_map_at(mgp_map *map, const char *key, mgp_value **result) { result); } +mgp_error mgp_key_exists(mgp_map *map, const char *key, int *result) { + return WrapExceptions( + [&map, &key]() -> int { + auto found_it = map->items.find(key); + if (found_it == map->items.end()) { + return 0; + }; + return 1; + }, + result); +} + mgp_error mgp_map_item_key(mgp_map_item *item, const char **result) { return WrapExceptions([&item] { return item->key; }, result); } diff --git a/tests/unit/cpp_api.cpp b/tests/unit/cpp_api.cpp index f425086f5..8e7957c1f 100644 --- a/tests/unit/cpp_api.cpp +++ b/tests/unit/cpp_api.cpp @@ -758,3 +758,12 @@ TYPED_TEST(CppApiTestFixture, TestInAndOutDegrees) { ASSERT_EQ(node_2.OutDegree(), 0); ASSERT_EQ(node_3.OutDegree(), 0); } + +TYPED_TEST(CppApiTestFixture, TestMapKeyExist) { + mgp::Map map = mgp::Map(); + map.Insert("key", mgp::Value("string")); + ASSERT_EQ(true, map.KeyExists("key")); + ASSERT_EQ(false, map.KeyExists("no_existo")); + map.Insert("null_key", mgp::Value()); + ASSERT_EQ(true, map.KeyExists("null_key")); +}