Skip to content

Commit

Permalink
Allow custom attributes on array values
Browse files Browse the repository at this point in the history
  • Loading branch information
Pascal Ginter committed Sep 20, 2024
1 parent 599be18 commit 858253b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lang/c++/impl/Compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ static const std::unordered_set<std::string> &getKnownFields() {
// return known fields
static const std::unordered_set<std::string> kKnownFields =
{"name", "type", "aliases", "default", "doc", "size", "logicalType",
"values", "precision", "scale", "namespace"};
"values", "precision", "scale", "namespace", "items"};
return kKnownFields;
}

Expand Down Expand Up @@ -424,6 +424,9 @@ static NodePtr makeArrayNode(const Entity &e, const Object &m,
if (containsField(m, "doc")) {
node->setDoc(getDocField(e, m));
}
CustomAttributes customAttributes;
getCustomAttributes(m, customAttributes);
node->addCustomAttributesForField(customAttributes);
return node;
}

Expand Down
3 changes: 3 additions & 0 deletions lang/c++/impl/NodeImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,9 @@ void NodeArray::printJson(std::ostream &os, size_t depth) const {
os << indent(depth + 1) << "\"items\": ";
leafAttributes_.get()->printJson(os, depth + 1);
os << '\n';
for (size_t i = 0; i != customAttributes_.size(); i++){
printCustomAttributes(customAttributes_.get(i), depth + 1, os);
}
os << indent(depth) << '}';
}

Expand Down
4 changes: 2 additions & 2 deletions lang/c++/include/avro/NodeImpl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ using NodeImplSymbolic = NodeImpl<HasName, NoLeaves, NoLeafNames, NoAttributes,

using NodeImplRecord = NodeImpl<HasName, MultiLeaves, LeafNames, MultiAttributes, NoSize>;
using NodeImplEnum = NodeImpl<HasName, NoLeaves, LeafNames, NoAttributes, NoSize>;
using NodeImplArray = NodeImpl<NoName, SingleLeaf, NoLeafNames, NoAttributes, NoSize>;
using NodeImplArray = NodeImpl<NoName, SingleLeaf, NoLeafNames, MultiAttributes, NoSize>;
using NodeImplMap = NodeImpl<NoName, MultiLeaves, NoLeafNames, NoAttributes, NoSize>;
using NodeImplUnion = NodeImpl<NoName, MultiLeaves, NoLeafNames, NoAttributes, NoSize>;
using NodeImplFixed = NodeImpl<HasName, NoLeaves, NoLeafNames, NoAttributes, HasSize>;
Expand Down Expand Up @@ -363,7 +363,7 @@ class AVRO_DECL NodeArray : public NodeImplArray {
public:
NodeArray() : NodeImplArray(AVRO_ARRAY) {}

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

SchemaResolution resolve(const Node &reader) const override;

Expand Down
7 changes: 4 additions & 3 deletions lang/c++/test/SchemaTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const char *basicSchemas[] = {
"[{\"name\": \"f\",\"type\": \"long\"}], \"extra attribute\": 1}",
"{\"type\": \"enum\", \"name\": \"Test\", \"symbols\": [\"A\", \"B\"],"
"\"extra attribute\": 1}",
R"({"type": "array", "items": "long", "extra attribute": 1})",
R"({"type": "array", "items": "long", "extra attribute": "1"})",
R"({"type": "map", "values": "long", "extra attribute": 1})",
R"({"type": "fixed", "name": "Test", "size": 1, "extra attribute": 1})",

Expand Down Expand Up @@ -236,6 +236,7 @@ const char *roundTripSchemas[] = {
"[{\"name\":\"f1\",\"type\":\"long\",\"extra_field\":\"1\"},"
"{\"name\":\"f2\",\"type\":\"int\","
"\"extra_field1\":\"21\",\"extra_field2\":\"22\"}]}",
R"({"type":"array","items":"long","extra":"1"})"
};

const char *malformedLogicalTypes[] = {
Expand Down Expand Up @@ -315,11 +316,11 @@ static void testRoundTrip(const char *schema) {
compiledSchema.toJson(os);
std::string result = os.str();
result.erase(std::remove_if(result.begin(), result.end(), ::isspace), result.end()); // Remove whitespace
BOOST_CHECK(result == std::string(schema));
BOOST_CHECK_EQUAL(result, std::string(schema));
// Verify that the compact schema from toJson has the same content as the
// schema.
std::string result2 = compiledSchema.toJson(false);
BOOST_CHECK(result2 == std::string(schema));
BOOST_CHECK_EQUAL(result2, std::string(schema));
}

static void testCompactSchemas() {
Expand Down

0 comments on commit 858253b

Please sign in to comment.