You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/08/12 08:42:51 UTC

[avro] 01/01: AVRO-3601: CustomAttributes#getAttribute() now returns boost::optional

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

mgrigorov pushed a commit to branch avro-3601-c++-simplify-custom-attributes-2
in repository https://gitbox.apache.org/repos/asf/avro.git

commit 49bed31807e14cbb4b040f4000af96a991101c3d
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
AuthorDate: Fri Aug 12 11:41:57 2022 +0300

    AVRO-3601: CustomAttributes#getAttribute() now returns boost::optional
    
    Add unit tests for CustomAttributes#getAttribute(string)
    
    Signed-off-by: Martin Tzvetanov Grigorov <mg...@apache.org>
---
 lang/c++/api/CustomAttributes.hh  |  3 ++-
 lang/c++/impl/CustomAttributes.cc |  8 +++++---
 lang/c++/test/unittest.cc         | 12 ++++++++++--
 3 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/lang/c++/api/CustomAttributes.hh b/lang/c++/api/CustomAttributes.hh
index 2bd572c57..adad6f6de 100644
--- a/lang/c++/api/CustomAttributes.hh
+++ b/lang/c++/api/CustomAttributes.hh
@@ -19,6 +19,7 @@
 #ifndef avro_CustomAttributes_hh__
 #define avro_CustomAttributes_hh__
 
+#include <boost/optional.hpp>
 #include <iostream>
 #include <map>
 #include <string>
@@ -33,7 +34,7 @@ class AVRO_DECL CustomAttributes {
   public:
     // Retrieves the custom attribute json entity for that attributeName, returns an
     // null if the attribute doesn't exist.
-    std::string getAttribute(const std::string &name) const;
+    boost::optional<std::string> getAttribute(const std::string &name) const;
 
     // Adds a custom attribute. If the attribute already exists, throw an exception.
     void addAttribute(const std::string &name, const std::string &value);
diff --git a/lang/c++/impl/CustomAttributes.cc b/lang/c++/impl/CustomAttributes.cc
index bb5643856..1186d9e26 100644
--- a/lang/c++/impl/CustomAttributes.cc
+++ b/lang/c++/impl/CustomAttributes.cc
@@ -23,13 +23,15 @@
 
 namespace avro {
 
-std::string CustomAttributes::getAttribute(const std::string &name) const {
+boost::optional<std::string> CustomAttributes::getAttribute(const std::string &name) const {
+    boost::optional<std::string> result;
     std::map<std::string, std::string>::const_iterator iter =
         attributes_.find(name);
     if (iter == attributes_.end()) {
-      return NULL;
+      return result;
     }
-    return iter->second;
+    result = iter->second;
+    return result;
 }
 
 void CustomAttributes::addAttribute(const std::string& name,
diff --git a/lang/c++/test/unittest.cc b/lang/c++/test/unittest.cc
index 2a5c51786..0057e8287 100644
--- a/lang/c++/test/unittest.cc
+++ b/lang/c++/test/unittest.cc
@@ -467,8 +467,6 @@ struct TestSchema {
         concepts::MultiAttribute<NodePtr> fieldValues;
         std::vector<GenericDatum> defaultValues;
 
-        CustomAttributes cf;
-        cf.addAttribute("extra field", std::string("1"));
         fieldNames.add("f1");
         fieldValues.add(NodePtr( new NodePrimitive(Type::AVRO_LONG)));
 
@@ -481,6 +479,15 @@ struct TestSchema {
                     expectedJsonWithoutCustomAttribute);
     }
 
+    void checkCustomAttributes_getAttribute()
+    {
+        CustomAttributes cf;
+        cf.addAttribute("field1", std::string("1"));
+
+        BOOST_CHECK_EQUAL(std::string("1"), *cf.getAttribute("field1"));
+        BOOST_CHECK_EQUAL(false, cf.getAttribute("not_existing").is_initialized());
+    }
+
     void test() {
         std::cout << "Before\n";
         schema_.toJson(std::cout);
@@ -505,6 +512,7 @@ struct TestSchema {
 
         checkNodeRecordWithoutCustomAttribute();
         checkNodeRecordWithCustomAttribute();
+        checkCustomAttributes_getAttribute();
     }
 
     ValidSchema schema_;