Skip to content

Commit

Permalink
AVRO-4121: [C++] Add Node::getCustomAttributes (#3325)
Browse files Browse the repository at this point in the history
  • Loading branch information
wgtmac authored Mar 1, 2025
1 parent 3b0c0f4 commit 7136629
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lang/c++/include/avro/Node.hh
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ public:
doAddCustomAttribute(customAttributes);
}

virtual CustomAttributes getCustomAttributes() const = 0;

virtual bool isValid() const = 0;

virtual SchemaResolution resolve(const Node &reader) const = 0;
Expand Down
11 changes: 11 additions & 0 deletions lang/c++/include/avro/NodeImpl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ protected:
customAttributes_.add(customAttributes);
}

CustomAttributes getCustomAttributes() const override {
CustomAttributes mergedCustomAttributes;
for (size_t i = 0; i < customAttributes_.size(); i++) {
const auto &customAttribute = customAttributes_.get(i);
for (const auto &[key, value] : customAttribute.attributes()) {
mergedCustomAttributes.addAttribute(key, value);
}
}
return mergedCustomAttributes;
}

SchemaResolution furtherResolution(const Node &reader) const {
SchemaResolution match = RESOLVE_NO_MATCH;

Expand Down
11 changes: 11 additions & 0 deletions lang/c++/test/unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,16 @@ void testNestedMapSchema() {
BOOST_CHECK_EQUAL(expected, actual.str());
}

void testCustomAttributes() {
std::string schema = R"({"type":"array","items":"long","extra":"1","extra2":"2"})";
avro::ValidSchema compiledSchema =
compileJsonSchemaFromString(schema);
auto customAttributes = compiledSchema.root()->getCustomAttributes();
BOOST_CHECK_EQUAL(customAttributes.attributes().size(), 2);
BOOST_CHECK_EQUAL(customAttributes.getAttribute("extra").value(), "1");
BOOST_CHECK_EQUAL(customAttributes.getAttribute("extra2").value(), "2");
}

boost::unit_test::test_suite *
init_unit_test_suite(int /*argc*/, char * /*argv*/[]) {
using namespace boost::unit_test;
Expand All @@ -1086,6 +1096,7 @@ init_unit_test_suite(int /*argc*/, char * /*argv*/[]) {
boost::make_shared<TestResolution>()));
test->add(BOOST_TEST_CASE(&testNestedArraySchema));
test->add(BOOST_TEST_CASE(&testNestedMapSchema));
test->add(BOOST_TEST_CASE(&testCustomAttributes));

return test;
}

0 comments on commit 7136629

Please sign in to comment.