You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by th...@apache.org on 2018/11/13 02:07:10 UTC

[avro] branch master updated: Partially addressed the problem of constructing nested Array/Map schemas (#374)

This is an automated email from the ASF dual-hosted git repository.

thiru pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new 80632f4   Partially addressed the problem of constructing nested Array/Map schemas (#374)
80632f4 is described below

commit 80632f46e75f447739c2fe91b13d26910bc527ac
Author: Thiruvalluvan M G <th...@apache.org>
AuthorDate: Tue Nov 13 07:37:05 2018 +0530

     Partially addressed the problem of constructing nested Array/Map schemas (#374)
    
    * Partially addressed the problem of constrcuting nested Array/Map schemas
    
    * Removed an unused function
---
 lang/c++/api/Schema.hh         |  2 ++
 lang/c++/impl/DataFile.cc      |  9 ---------
 lang/c++/impl/Schema.cc        | 12 ++++++++++++
 lang/c++/test/CompilerTests.cc | 26 ++++++++++++++++++++++++++
 lang/c++/test/unittest.cc      | 40 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 80 insertions(+), 9 deletions(-)

diff --git a/lang/c++/api/Schema.hh b/lang/c++/api/Schema.hh
index 646b95e..51bdc43 100644
--- a/lang/c++/api/Schema.hh
+++ b/lang/c++/api/Schema.hh
@@ -117,11 +117,13 @@ public:
 class AVRO_DECL ArraySchema : public Schema {
 public:
     ArraySchema(const Schema &itemsSchema);
+    ArraySchema(const ArraySchema &itemsSchema);
 };
 
 class AVRO_DECL MapSchema : public Schema {
 public:
     MapSchema(const Schema &valuesSchema);
+    MapSchema(const MapSchema &itemsSchema);
 };
 
 class AVRO_DECL UnionSchema : public Schema {
diff --git a/lang/c++/impl/DataFile.cc b/lang/c++/impl/DataFile.cc
index 1777d53..d2662c3 100644
--- a/lang/c++/impl/DataFile.cc
+++ b/lang/c++/impl/DataFile.cc
@@ -63,15 +63,6 @@ boost::iostreams::zlib_params get_zlib_params() {
 }
 }
 
-
-static string toString(const ValidSchema& schema)
-{
-    ostringstream oss;
-    schema.toJson(oss);
-    return oss.str();
-}
-
-
 DataFileWriterBase::DataFileWriterBase(const char* filename, const ValidSchema& schema, size_t syncInterval,
                                        Codec codec) :
     filename_(filename),
diff --git a/lang/c++/impl/Schema.cc b/lang/c++/impl/Schema.cc
index 6676574..3e6d444 100644
--- a/lang/c++/impl/Schema.cc
+++ b/lang/c++/impl/Schema.cc
@@ -78,12 +78,24 @@ ArraySchema::ArraySchema(const Schema &itemsSchema) :
     node_->addLeaf(itemsSchema.root());
 }
 
+ArraySchema::ArraySchema(const ArraySchema &itemsSchema) :
+    Schema(new NodeArray)
+{
+    node_->addLeaf(itemsSchema.root());
+}
+
 MapSchema::MapSchema(const Schema &valuesSchema) :
     Schema(new NodeMap)
 {
     node_->addLeaf(valuesSchema.root());
 }
 
+MapSchema::MapSchema(const MapSchema &valuesSchema) :
+    Schema(new NodeMap)
+{
+    node_->addLeaf(valuesSchema.root());
+}
+
 UnionSchema::UnionSchema() :
     Schema(new NodeUnion)
 { }
diff --git a/lang/c++/test/CompilerTests.cc b/lang/c++/test/CompilerTests.cc
index 0b63dbb..0207f39 100644
--- a/lang/c++/test/CompilerTests.cc
+++ b/lang/c++/test/CompilerTests.cc
@@ -60,6 +60,31 @@ void testEmptyBytesDefault()
     BOOST_CHECK_EQUAL(expected, actual.str());
 }
 
+void test2dArray()
+{
+    std::string input = "{\n\
+    \"type\": \"array\",\n\
+    \"items\": {\n\
+        \"type\": \"array\",\n\
+        \"items\": \"double\"\n\
+    }\n\
+}\n";
+
+    std::string expected = "{\n\
+    \"type\": \"array\",\n\
+    \"items\": {\n\
+        \"type\": \"array\",\n\
+        \"items\": \"double\"\n\
+    }\n\
+}\n\
+";
+    avro::ValidSchema schema = avro::compileJsonSchemaFromString(input);
+    std::ostringstream actual;
+    schema.toJson(actual);
+    BOOST_CHECK_EQUAL(expected, actual.str());
+
+}
+
 boost::unit_test::test_suite*
 init_unit_test_suite(int argc, char* argv[])
 {
@@ -67,5 +92,6 @@ init_unit_test_suite(int argc, char* argv[])
 
     test_suite* ts= BOOST_TEST_SUITE("Avro C++ unit tests for Compiler.cc");
     ts->add(BOOST_TEST_CASE(&testEmptyBytesDefault));
+    ts->add(BOOST_TEST_CASE(&test2dArray));
     return ts;
 }
diff --git a/lang/c++/test/unittest.cc b/lang/c++/test/unittest.cc
index 55a33a0..cb5aaa7 100644
--- a/lang/c++/test/unittest.cc
+++ b/lang/c++/test/unittest.cc
@@ -975,6 +975,44 @@ struct TestResolution
     ValidSchema unionTwo_;
 };
 
+void testNestedArraySchema()
+{
+    ArraySchema b0 = ArraySchema(NullSchema());
+    ArraySchema a0 = ArraySchema(b0);
+
+    avro::ValidSchema vs(a0);
+    std::ostringstream actual;
+    vs.toJson(actual);
+
+    std::string expected = "{\n\
+    \"type\": \"array\",\n\
+    \"items\": {\n\
+        \"type\": \"array\",\n\
+        \"items\": \"null\"\n\
+    }\n\
+}\n";
+    BOOST_CHECK_EQUAL(expected, actual.str());
+}
+
+void testNestedMapSchema()
+{
+    MapSchema b0 = MapSchema(NullSchema());
+    MapSchema a0 = MapSchema(b0);
+
+    avro::ValidSchema vs(a0);
+    std::ostringstream actual;
+    vs.toJson(actual);
+
+    std::string expected = "{\n\
+    \"type\": \"map\",\n\
+    \"values\": {\n\
+        \"type\": \"map\",\n\
+        \"values\": \"null\"\n\
+    }\n\
+}\n";
+    BOOST_CHECK_EQUAL(expected, actual.str());
+}
+
 boost::unit_test::test_suite*
 init_unit_test_suite( int argc, char* argv[] )
 {
@@ -994,6 +1032,8 @@ init_unit_test_suite( int argc, char* argv[] )
                                     boost::make_shared<TestBadStuff>()));
     test->add(BOOST_CLASS_TEST_CASE(&TestResolution::test,
                                     boost::make_shared<TestResolution>()));
+    test->add(BOOST_TEST_CASE(&testNestedArraySchema));
+    test->add(BOOST_TEST_CASE(&testNestedMapSchema));
 
     return test;
 }