From 858253bc8d9aeae46287fd22ab161fc8afb02438 Mon Sep 17 00:00:00 2001 From: Pascal Ginter Date: Fri, 20 Sep 2024 14:41:02 +0200 Subject: [PATCH 1/2] Allow custom attributes on array values --- lang/c++/impl/Compiler.cc | 5 ++++- lang/c++/impl/NodeImpl.cc | 3 +++ lang/c++/include/avro/NodeImpl.hh | 4 ++-- lang/c++/test/SchemaTests.cc | 7 ++++--- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lang/c++/impl/Compiler.cc b/lang/c++/impl/Compiler.cc index f3c2397da24..797e8b3813d 100644 --- a/lang/c++/impl/Compiler.cc +++ b/lang/c++/impl/Compiler.cc @@ -267,7 +267,7 @@ static const std::unordered_set &getKnownFields() { // return known fields static const std::unordered_set kKnownFields = {"name", "type", "aliases", "default", "doc", "size", "logicalType", - "values", "precision", "scale", "namespace"}; + "values", "precision", "scale", "namespace", "items"}; return kKnownFields; } @@ -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; } diff --git a/lang/c++/impl/NodeImpl.cc b/lang/c++/impl/NodeImpl.cc index e3073aaaef2..a17732bcc51 100644 --- a/lang/c++/impl/NodeImpl.cc +++ b/lang/c++/impl/NodeImpl.cc @@ -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) << '}'; } diff --git a/lang/c++/include/avro/NodeImpl.hh b/lang/c++/include/avro/NodeImpl.hh index 3e5546c94ea..b4759f70e27 100644 --- a/lang/c++/include/avro/NodeImpl.hh +++ b/lang/c++/include/avro/NodeImpl.hh @@ -234,7 +234,7 @@ using NodeImplSymbolic = NodeImpl; using NodeImplEnum = NodeImpl; -using NodeImplArray = NodeImpl; +using NodeImplArray = NodeImpl; using NodeImplMap = NodeImpl; using NodeImplUnion = NodeImpl; using NodeImplFixed = NodeImpl; @@ -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; diff --git a/lang/c++/test/SchemaTests.cc b/lang/c++/test/SchemaTests.cc index 029be79d57e..f4c4fb6af97 100644 --- a/lang/c++/test/SchemaTests.cc +++ b/lang/c++/test/SchemaTests.cc @@ -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})", @@ -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[] = { @@ -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() { From 235dde918bb973d59acc87a803b5fbc655a181f5 Mon Sep 17 00:00:00 2001 From: Pascal Ginter Date: Tue, 24 Sep 2024 14:34:49 +0200 Subject: [PATCH 2/2] Fix merge error of undoing conversion of custom attribute to string --- lang/c++/test/SchemaTests.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/c++/test/SchemaTests.cc b/lang/c++/test/SchemaTests.cc index c9fec0ed765..477e36046eb 100644 --- a/lang/c++/test/SchemaTests.cc +++ b/lang/c++/test/SchemaTests.cc @@ -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})",