Skip to content

Commit 7136629

Browse files
authored
AVRO-4121: [C++] Add Node::getCustomAttributes (#3325)
1 parent 3b0c0f4 commit 7136629

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

lang/c++/include/avro/Node.hh

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ public:
170170
doAddCustomAttribute(customAttributes);
171171
}
172172

173+
virtual CustomAttributes getCustomAttributes() const = 0;
174+
173175
virtual bool isValid() const = 0;
174176

175177
virtual SchemaResolution resolve(const Node &reader) const = 0;

lang/c++/include/avro/NodeImpl.hh

+11
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ protected:
164164
customAttributes_.add(customAttributes);
165165
}
166166

167+
CustomAttributes getCustomAttributes() const override {
168+
CustomAttributes mergedCustomAttributes;
169+
for (size_t i = 0; i < customAttributes_.size(); i++) {
170+
const auto &customAttribute = customAttributes_.get(i);
171+
for (const auto &[key, value] : customAttribute.attributes()) {
172+
mergedCustomAttributes.addAttribute(key, value);
173+
}
174+
}
175+
return mergedCustomAttributes;
176+
}
177+
167178
SchemaResolution furtherResolution(const Node &reader) const {
168179
SchemaResolution match = RESOLVE_NO_MATCH;
169180

lang/c++/test/unittest.cc

+11
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,16 @@ void testNestedMapSchema() {
10661066
BOOST_CHECK_EQUAL(expected, actual.str());
10671067
}
10681068

1069+
void testCustomAttributes() {
1070+
std::string schema = R"({"type":"array","items":"long","extra":"1","extra2":"2"})";
1071+
avro::ValidSchema compiledSchema =
1072+
compileJsonSchemaFromString(schema);
1073+
auto customAttributes = compiledSchema.root()->getCustomAttributes();
1074+
BOOST_CHECK_EQUAL(customAttributes.attributes().size(), 2);
1075+
BOOST_CHECK_EQUAL(customAttributes.getAttribute("extra").value(), "1");
1076+
BOOST_CHECK_EQUAL(customAttributes.getAttribute("extra2").value(), "2");
1077+
}
1078+
10691079
boost::unit_test::test_suite *
10701080
init_unit_test_suite(int /*argc*/, char * /*argv*/[]) {
10711081
using namespace boost::unit_test;
@@ -1086,6 +1096,7 @@ init_unit_test_suite(int /*argc*/, char * /*argv*/[]) {
10861096
boost::make_shared<TestResolution>()));
10871097
test->add(BOOST_TEST_CASE(&testNestedArraySchema));
10881098
test->add(BOOST_TEST_CASE(&testNestedMapSchema));
1099+
test->add(BOOST_TEST_CASE(&testCustomAttributes));
10891100

10901101
return test;
10911102
}

0 commit comments

Comments
 (0)