Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++: accessors for custom attributes #285

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lang/c++/include/avro/Node.hh
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ public:
doAddCustomAttribute(customAttributes);
}

virtual size_t customAttributes() const = 0;
virtual const CustomAttributes& customAttributesAt(size_t index) const = 0;

virtual bool isValid() const = 0;

virtual SchemaResolution resolve(const Node &reader) const = 0;
Expand Down
8 changes: 8 additions & 0 deletions lang/c++/include/avro/NodeImpl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ protected:
return nameIndex_.lookup(name, index);
}

size_t customAttributes() const override {
return customAttributes_.size();
}

const CustomAttributes& customAttributesAt(size_t index) const override {
return customAttributes_.get(index);
}

void doSetFixedSize(size_t size) override {
sizeAttribute_.add(size);
}
Expand Down
38 changes: 38 additions & 0 deletions lang/c++/test/unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,42 @@ struct TestSchema {
BOOST_CHECK_EQUAL(false, cf.getAttribute("not_existing").is_initialized());
}

void checkCustomAttributesFromNode()
{
std::string jsonWithCustomAttribute = R"(
{"type": "record", "name": "Test","fields":
[{"name": "f1", "type": "long",
"arrayField": [1],
"booleanField": true,
"mapField": {"key1":"value1", "key2":"value2"},
"nullField": null,
"numberField": 1.23,
"stringField": "field value with \"double quotes\""
}]}
)";
auto schema = avro::compileJsonSchemaFromString(jsonWithCustomAttribute);
const auto& nodePtr = schema.root();
BOOST_CHECK_EQUAL(nodePtr->type(), AVRO_RECORD);
BOOST_CHECK_EQUAL(1, nodePtr->customAttributes());
const auto& attrs = nodePtr->customAttributesAt(0);
BOOST_CHECK_EQUAL(6, attrs.attributes().size());
BOOST_CHECK_THROW(nodePtr->customAttributesAt(1), std::out_of_range);
}

void checkNoCustomAttributesFromNode()
{
std::string jsonNoCustomAttribute =
"{\"type\": \"record\", \"name\": \"Test\",\"fields\": "
"[{\"name\": \"f1\", \"type\": \"long\"}]}";
auto schema = avro::compileJsonSchemaFromString(jsonNoCustomAttribute);
const auto& nodePtr = schema.root();
BOOST_CHECK_EQUAL(nodePtr->type(), AVRO_RECORD);
BOOST_CHECK_EQUAL(1, nodePtr->customAttributes());
const auto& attrs = nodePtr->customAttributesAt(0);
BOOST_CHECK_EQUAL(0, attrs.attributes().size());
BOOST_CHECK_THROW(nodePtr->customAttributesAt(1), std::out_of_range);
}

void test() {
std::cout << "Before\n";
schema_.toJson(std::cout);
Expand All @@ -525,6 +561,8 @@ struct TestSchema {
checkNodeRecordWithoutCustomAttribute();
checkNodeRecordWithCustomAttribute();
checkCustomAttributes_getAttribute();
checkCustomAttributesFromNode();
checkNoCustomAttributesFromNode();
}

ValidSchema schema_;
Expand Down
Loading