Skip to content

Commit

Permalink
added TopLevelAttributeExcludeHandler convenience object
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteemann committed Dec 7, 2015
1 parent a561316 commit 2cdd281
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ install(FILES velocypack/Builder.h DESTINATION include/velocypack)
install(FILES velocypack/Collection.h DESTINATION include/velocypack)
install(FILES velocypack/Dumper.h DESTINATION include/velocypack)
install(FILES velocypack/Exception.h DESTINATION include/velocypack)
install(FILES velocypack/Helpers.h DESTINATION include/velocypack)
install(FILES velocypack/HexDump.h DESTINATION include/velocypack)
install(FILES velocypack/Iterator.h DESTINATION include/velocypack)
install(FILES velocypack/Options.h DESTINATION include/velocypack)
Expand Down
56 changes: 56 additions & 0 deletions include/velocypack/Helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
////////////////////////////////////////////////////////////////////////////////
/// @brief Library to build up VPack documents.
///
/// DISCLAIMER
///
/// Copyright 2015 ArangoDB GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Max Neunhoeffer
/// @author Jan Steemann
/// @author Copyright 2015, ArangoDB GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

#ifndef VELOCYPACK_HELPERS_H
#define VELOCYPACK_HELPERS_H 1

#include <string>
#include <cstdint>
#include <unordered_set>

#include "velocypack/velocypack-common.h"
#include "velocypack/Options.h"
#include "velocypack/Slice.h"

namespace arangodb {
namespace velocypack {

struct TopLevelAttributeExcludeHandler final : AttributeExcludeHandler {
TopLevelAttributeExcludeHandler (std::unordered_set<std::string> const& attributes)
: attributes(attributes) {
}

bool shouldExclude(Slice const& key, int nesting) override final {
return (nesting == 1 && attributes.find(key.copyString()) != attributes.end());
}

std::unordered_set<std::string> attributes;
};

} // namespace arangodb::velocypack
} // namespace arangodb

#endif
7 changes: 7 additions & 0 deletions include/velocypack/velocypack-aliases.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ using VPackAttributeTranslator = arangodb::velocypack::AttributeTranslator;
#endif
#endif

#ifdef VELOCYPACK_HELPERS_H
#ifndef VELOCYPACK_ALIAS_HELPERS
#define VELOCYPACK_ALIAS_HELPERS
using VPackTopLevelAttributeExcludeHandler = arangodb::velocypack::TopLevelAttributeExcludeHandler;
#endif
#endif

#ifdef VELOCYPACK_DUMPER_H
#ifndef VELOCYPACK_ALIAS_DUMPER
#define VELOCYPACK_ALIAS_DUMPER
Expand Down
1 change: 1 addition & 0 deletions tests/tests-common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "velocypack/Collection.h"
#include "velocypack/Dumper.h"
#include "velocypack/Exception.h"
#include "velocypack/Helpers.h"
#include "velocypack/HexDump.h"
#include "velocypack/Iterator.h"
#include "velocypack/Options.h"
Expand Down
29 changes: 29 additions & 0 deletions tests/testsParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2267,6 +2267,35 @@ TEST(ParserTest, ExcludeAttributesTopLevel) {
ASSERT_EQ(7UL, s.get("bark").getUInt());
}

TEST(ParserTest, ExcludeAttributesTopLevelUsingHelper) {
std::string const value(
"{\"foo\":1,\"bar\":2,\"baz\":3,\"qux\":4,\"quux\":5,\"bart\":6,\"bark\":"
"7}");

TopLevelAttributeExcludeHandler handler(std::unordered_set<std::string>{ "foo", "bar", "qux" });
Options options;
options.attributeExcludeHandler = &handler;

Parser parser(&options);
parser.parse(value);

std::shared_ptr<Builder> b = parser.steal();
Slice s(b->slice());

ASSERT_EQ(4UL, s.length());
ASSERT_FALSE(s.hasKey("foo"));
ASSERT_FALSE(s.hasKey("bar"));
ASSERT_FALSE(s.hasKey("qux"));
ASSERT_TRUE(s.hasKey("baz"));
ASSERT_EQ(3UL, s.get("baz").getUInt());
ASSERT_TRUE(s.hasKey("quux"));
ASSERT_EQ(5UL, s.get("quux").getUInt());
ASSERT_TRUE(s.hasKey("bart"));
ASSERT_EQ(6UL, s.get("bart").getUInt());
ASSERT_TRUE(s.hasKey("bark"));
ASSERT_EQ(7UL, s.get("bark").getUInt());
}

TEST(ParserTest, ExcludeAttributesSubLevel) {
std::string const value(
"{\"foo\":{\"bar\":{\"baz\":2,\"qux\":2,\"bart\":4},\"qux\":{\"baz\":9}},"
Expand Down

0 comments on commit 2cdd281

Please sign in to comment.