Skip to content

Commit

Permalink
AVRO-3601: CustomAttributes#getAttribute() now returns boost::optional (
Browse files Browse the repository at this point in the history
#1826)

* AVRO-3601: CustomAttributes#getAttribute() now returns boost::optional

Add unit tests for CustomAttributes#getAttribute(string)

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>

* AVRO-3601: Add unit tests for writing CustomAttributes's values as JSON strings

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
(cherry picked from commit d70b847)
  • Loading branch information
martin-g committed Aug 15, 2022
1 parent 32aa94d commit 734222e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
3 changes: 2 additions & 1 deletion lang/c++/api/CustomAttributes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef avro_CustomAttributes_hh__
#define avro_CustomAttributes_hh__

#include <boost/optional.hpp>
#include <iostream>
#include <map>
#include <string>
Expand All @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions lang/c++/impl/CustomAttributes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
28 changes: 24 additions & 4 deletions lang/c++/test/unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,12 @@ struct TestSchema {
concepts::MultiAttribute<CustomAttributes> customAttributes;

CustomAttributes cf;
cf.addAttribute("extra field", std::string("1"));
cf.addAttribute("stringField", std::string("\\\"field value with \\\"double quotes\\\"\\\""));
cf.addAttribute("booleanField", std::string("true"));
cf.addAttribute("numberField", std::string("1.23"));
cf.addAttribute("nullField", std::string("null"));
cf.addAttribute("arrayField", std::string("[1]"));
cf.addAttribute("mapField", std::string("{\\\"key1\\\":\\\"value1\\\", \\\"key2\\\":\\\"value2\\\"}"));
fieldNames.add("f1");
fieldValues.add(NodePtr( new NodePrimitive(Type::AVRO_LONG)));
customAttributes.add(cf);
Expand All @@ -452,7 +457,14 @@ struct TestSchema {
customAttributes);
std::string expectedJsonWithCustomAttribute =
"{\"type\": \"record\", \"name\": \"Test\",\"fields\": "
"[{\"name\": \"f1\", \"type\": \"long\",\"extra field\": \"1\"}]}";
"[{\"name\": \"f1\", \"type\": \"long\", "
"\"arrayField\": \"[1]\", "
"\"booleanField\": \"true\", "
"\"mapField\": \"{\\\"key1\\\":\\\"value1\\\", \\\"key2\\\":\\\"value2\\\"}\", "
"\"nullField\": \"null\", "
"\"numberField\": \"1.23\", "
"\"stringField\": \"\\\"field value with \\\"double quotes\\\"\\\"\""
"}]}";
testNodeRecord(nodeRecordWithCustomAttribute,
expectedJsonWithCustomAttribute);
}
Expand All @@ -467,8 +479,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)));

Expand All @@ -481,6 +491,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);
Expand All @@ -505,6 +524,7 @@ struct TestSchema {

checkNodeRecordWithoutCustomAttribute();
checkNodeRecordWithCustomAttribute();
checkCustomAttributes_getAttribute();
}

ValidSchema schema_;
Expand Down

0 comments on commit 734222e

Please sign in to comment.