Skip to content

Commit

Permalink
Merge pull request #267 from andrwng/element-id-api
Browse files Browse the repository at this point in the history
C++: add elementId to programmatic API
  • Loading branch information
andrwng authored Aug 2, 2024
2 parents 121d2a8 + 409503d commit d889083
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lang/c++/impl/Schema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ ArraySchema::ArraySchema(const ArraySchema &itemsSchema) : Schema(std::make_shar
node_->addLeaf(itemsSchema.root());
}

ArraySchema::ArraySchema(const Schema &itemsSchema, int64_t elementId) : Schema(std::make_shared<NodeArray>(elementId)) {
node_->addLeaf(itemsSchema.root());
}

MapSchema::MapSchema(const Schema &valuesSchema) : Schema(std::make_shared<NodeMap>()) {
node_->addLeaf(valuesSchema.root());
}
Expand Down
1 change: 1 addition & 0 deletions lang/c++/include/avro/NodeImpl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ public:
class AVRO_DECL NodeArray : public NodeImplArray {
public:
NodeArray() : NodeImplArray(AVRO_ARRAY) {}
explicit NodeArray(int64_t elementId) : NodeImplArray(AVRO_ARRAY), elementId_(elementId) {}

explicit NodeArray(const SingleLeaf &items) : NodeImplArray(AVRO_ARRAY, NoName(), items, NoLeafNames(), NoAttributes(), NoSize()) {}

Expand Down
1 change: 1 addition & 0 deletions lang/c++/include/avro/Schema.hh
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public:
class AVRO_DECL ArraySchema : public Schema {
public:
explicit ArraySchema(const Schema &itemsSchema);
ArraySchema(const Schema &itemsSchema, int64_t elementId);
ArraySchema(const ArraySchema &itemsSchema);
};

Expand Down
21 changes: 21 additions & 0 deletions lang/c++/test/unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,26 @@ void testNestedMapSchema() {
BOOST_CHECK_EQUAL(expected, actual.str());
}

void testArraySchemaElementId() {
ArraySchema b0 = ArraySchema(NullSchema(), 2);
ArraySchema a0 = ArraySchema(b0, 1);

avro::ValidSchema vs(a0);
std::ostringstream actual;
vs.toJson(actual);

std::string expected = "{\n\
\"type\": \"array\",\n\
\"element-id\": 1,\n\
\"items\": {\n\
\"type\": \"array\",\n\
\"element-id\": 2,\n\
\"items\": \"null\"\n\
}\n\
}\n";
BOOST_CHECK_EQUAL(expected, actual.str());
}

boost::unit_test::test_suite *
init_unit_test_suite(int /*argc*/, char * /*argv*/[]) {
using namespace boost::unit_test;
Expand All @@ -1104,6 +1124,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(&testArraySchemaElementId));

return test;
}

0 comments on commit d889083

Please sign in to comment.