Skip to content

Commit

Permalink
[C++] AVRO-4058: Allow custom attributes in arrays (#3168)
Browse files Browse the repository at this point in the history
* Allow custom attributes on array values

* Fix merge error of undoing conversion of custom attribute to string

---------

Co-authored-by: Pascal Ginter <ginpa2000@gmail.com~>
  • Loading branch information
pascalginter and Pascal Ginter authored Sep 24, 2024
1 parent 86fb29c commit 8ecb576
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 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
9 changes: 5 additions & 4 deletions lang/c++/test/SchemaTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const char *basicSchemas[] = {
"extra attribute": 1
})",
R"({"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 @@ -355,7 +355,8 @@ 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 @@ -448,11 +449,11 @@ static void testRoundTrip(const char *schema) {
compiledSchema.toJson(os);
std::string result = removeWhitespaceFromSchema(os.str());
std::string trimmedSchema = removeWhitespaceFromSchema(schema);
BOOST_CHECK(result == trimmedSchema);
BOOST_CHECK_EQUAL(result, trimmedSchema);
// Verify that the compact schema from toJson has the same content as the
// schema.
std::string result2 = compiledSchema.toJson(false);
BOOST_CHECK(result2 == trimmedSchema);
BOOST_CHECK_EQUAL(result2, trimmedSchema);
}

static void testCompactSchemas() {
Expand Down

0 comments on commit 8ecb576

Please sign in to comment.