This is an automated email from the ASF dual-hosted git repository.

fokko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/main by this push:
     new 71366295d AVRO-4121: [C++] Add Node::getCustomAttributes (#3325)
71366295d is described below

commit 71366295dd84dc6dd190c3f8151548b8b53fe031
Author: Gang Wu <[email protected]>
AuthorDate: Sun Mar 2 04:01:26 2025 +0800

    AVRO-4121: [C++] Add Node::getCustomAttributes (#3325)
---
 lang/c++/include/avro/Node.hh     |  2 ++
 lang/c++/include/avro/NodeImpl.hh | 11 +++++++++++
 lang/c++/test/unittest.cc         | 11 +++++++++++
 3 files changed, 24 insertions(+)

diff --git a/lang/c++/include/avro/Node.hh b/lang/c++/include/avro/Node.hh
index 8664a9ab6..ad01dddb4 100644
--- a/lang/c++/include/avro/Node.hh
+++ b/lang/c++/include/avro/Node.hh
@@ -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;
diff --git a/lang/c++/include/avro/NodeImpl.hh 
b/lang/c++/include/avro/NodeImpl.hh
index d8a1da3c3..4a35d6fb1 100644
--- a/lang/c++/include/avro/NodeImpl.hh
+++ b/lang/c++/include/avro/NodeImpl.hh
@@ -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;
 
diff --git a/lang/c++/test/unittest.cc b/lang/c++/test/unittest.cc
index b0cb44c5b..feafa31a4 100644
--- a/lang/c++/test/unittest.cc
+++ b/lang/c++/test/unittest.cc
@@ -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;
@@ -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;
 }

Reply via email to