avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From th...@apache.org
Subject svn commit: r1182355 - in /avro/trunk: CHANGES.txt lang/c++/CMakeLists.txt lang/c++/impl/avrogencpp.cc lang/c++/jsonschemas/union_conflict lang/c++/test/AvrogencppTests.cc
Date Wed, 12 Oct 2011 13:26:56 GMT
Author: thiru
Date: Wed Oct 12 13:26:55 2011
New Revision: 1182355

URL: http://svn.apache.org/viewvc?rev=1182355&view=rev
Log:
AVRO-840. C++ generate nullable types for optional fields int the schema

Added:
    avro/trunk/lang/c++/jsonschemas/union_conflict
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/c++/CMakeLists.txt
    avro/trunk/lang/c++/impl/avrogencpp.cc
    avro/trunk/lang/c++/test/AvrogencppTests.cc

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1182355&r1=1182354&r2=1182355&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Oct 12 13:26:55 2011
@@ -111,6 +111,8 @@ Avro 1.6.0 (unreleased)
 
     AVRO-917. Avrogencpp does not insert avro prefix for avro headers in the generated files.
(thiru)
 
+    AVRO-840. C++ generate nullable types for optional fields int the schema. (thiru)
+
   BUG FIXES
 
     AVRO-824. Java: Fix usage message of BinaryFragmentToJsonTool.

Modified: avro/trunk/lang/c++/CMakeLists.txt
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/CMakeLists.txt?rev=1182355&r1=1182354&r2=1182355&view=diff
==============================================================================
--- avro/trunk/lang/c++/CMakeLists.txt (original)
+++ avro/trunk/lang/c++/CMakeLists.txt Wed Oct 12 13:26:55 2011
@@ -113,6 +113,11 @@ add_custom_target (union_map_union_hh
         -o ${BUILD_DIRECTORY}/union_map_union.hh -n umu
     DEPENDS avrogencpp)
 
+add_custom_target (union_conflict_hh
+    COMMAND avrogencpp -i jsonschemas/union_conflict
+        -o ${BUILD_DIRECTORY}/union_conflict.hh -n uc -U
+    DEPENDS avrogencpp)
+
 add_custom_target (recursive_hh
     COMMAND avrogencpp -i jsonschemas/recursive
         -o ${BUILD_DIRECTORY}/recursive.hh -n rec
@@ -154,8 +159,8 @@ target_link_libraries (testgentest avroc
 
 add_executable (AvrogencppTests test/AvrogencppTests.cc)
 add_dependencies (AvrogencppTests bigrecord_hh bigrecord2_hh
-    union_array_union_hh union_map_union_hh recursive_hh reuse_hh
-    circulardep_hh)
+    union_array_union_hh union_map_union_hh union_conflict_hh
+    recursive_hh reuse_hh circulardep_hh)
 target_link_libraries (AvrogencppTests avrocpp ${BOOST_LIBRARIES})
 
 add_executable (DataFileTests test/DataFileTests.cc)

Modified: avro/trunk/lang/c++/impl/avrogencpp.cc
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/impl/avrogencpp.cc?rev=1182355&r1=1182354&r2=1182355&view=diff
==============================================================================
--- avro/trunk/lang/c++/impl/avrogencpp.cc (original)
+++ avro/trunk/lang/c++/impl/avrogencpp.cc Wed Oct 12 13:26:55 2011
@@ -76,6 +76,7 @@ class CodeGen {
     const std::string headerFile_;
     const std::string schemaFile_;
     const std::string includePrefix_;
+    const bool noUnion_;
     boost::mt19937 random_;
 
     vector<PendingSetterGetter> pendingGettersAndSetters;
@@ -102,10 +103,10 @@ class CodeGen {
 public:
     CodeGen(std::ostream& os, const std::string& ns,
         const std::string& schemaFile, const std::string& headerFile,
-        const std::string& includePrefix) :
+        const std::string& includePrefix, bool noUnion) :
         unionNumber_(0), os_(os), inNamespace_(false), ns_(ns),
         schemaFile_(schemaFile), headerFile_(headerFile),
-        includePrefix_(includePrefix),
+        includePrefix_(includePrefix), noUnion_(noUnion),
         random_(::time(0)) { }
     void generate(const ValidSchema& schema);
 };
@@ -208,9 +209,21 @@ string CodeGen::generateRecordType(const
     }
 
     os_ << "struct " << n->name() << " {\n";
+    if (! noUnion_) {
+        for (int i = 0; i < c; ++i) {
+            if (n->leafAt(i)->type() == avro::AVRO_UNION) {
+                os_ << "    typedef " << types[i]
+                    << ' ' << n->nameAt(i) << "_t;\n";
+            }
+        }
+    }
     for (int i = 0; i < c; ++i) {
-        os_ << "    " << types[i]
-            << " " << n->nameAt(i) << ";\n";
+        if (! noUnion_ && n->leafAt(i)->type() == avro::AVRO_UNION) {
+            os_ << "    " << n->nameAt(i) << "_t";
+        } else {
+            os_ << "    " << types[i];
+        }
+        os_ << ' ' << n->nameAt(i) << ";\n";
     }
     os_ << "};\n\n";
     return n->name();
@@ -318,7 +331,10 @@ string CodeGen::generateUnionType(const 
     for (size_t i = 0; i < c; ++i) {
         const NodePtr& nn = n->leafAt(i);
         if (nn->type() == avro::AVRO_NULL) {
-            os_ << "    void set_null() {\n"
+            os_ << "    bool is_null() const {\n"
+                << "        return (idx_ == " << i << ");\n"
+                << "    }\n"
+                << "    void set_null() {\n"
                 << "        idx_ = " << i << ";\n"
                 << "        value_ = boost::any();\n"
                 << "    }\n";
@@ -634,6 +650,7 @@ static const string NS("namespace");
 static const string OUT("output");
 static const string IN("input");
 static const string INCLUDE_PREFIX("include-prefix");
+static const string NO_UNION_TYPEDEF("no-union-typedef");
 
 int main(int argc, char** argv)
 {
@@ -642,6 +659,7 @@ int main(int argc, char** argv)
         ("help,h", "produce help message")
         ("include-prefix,p", po::value<string>()->default_value("avro"),
             "prefix for include headers, - for none, default: avro")
+        ("no-union-typedef,U", "do not generate typedefs for unions in records")
         ("namespace,n", po::value<string>(), "set namespace for generated code")
         ("input,i", po::value<string>(), "input file")
         ("output,o", po::value<string>(), "output file to generate");
@@ -660,6 +678,7 @@ int main(int argc, char** argv)
     string outf = vm.count(OUT) > 0 ? vm[OUT].as<string>() : string();
     string inf = vm.count(IN) > 0 ? vm[IN].as<string>() : string();
     string incPrefix = vm[INCLUDE_PREFIX].as<string>();
+    bool noUnion = vm.count(NO_UNION_TYPEDEF) != 0;
     if (incPrefix == "-") {
         incPrefix.clear();
     } else if (*incPrefix.rbegin() != '/') {
@@ -678,9 +697,10 @@ int main(int argc, char** argv)
 
         if (! outf.empty()) {
             ofstream out(outf.c_str());
-            CodeGen(out, ns, inf, outf, incPrefix).generate(schema);
+            CodeGen(out, ns, inf, outf, incPrefix, noUnion).generate(schema);
         } else {
-            CodeGen(std::cout, ns, inf, outf, incPrefix).generate(schema);
+            CodeGen(std::cout, ns, inf, outf, incPrefix, noUnion).
+                generate(schema);
         }
         return 0;
     } catch (std::exception &e) {

Added: avro/trunk/lang/c++/jsonschemas/union_conflict
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/jsonschemas/union_conflict?rev=1182355&view=auto
==============================================================================
--- avro/trunk/lang/c++/jsonschemas/union_conflict (added)
+++ avro/trunk/lang/c++/jsonschemas/union_conflict Wed Oct 12 13:26:55 2011
@@ -0,0 +1,9 @@
+{
+  "type": "record",
+  "name": "uc",
+  "fields" : [
+    {"name": "rev_t", "type": "string"},
+    {"name": "data", "type": "bytes"},
+    {"name": "rev", "type": ["string", "null"]}
+  ]
+}

Modified: avro/trunk/lang/c++/test/AvrogencppTests.cc
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/test/AvrogencppTests.cc?rev=1182355&r1=1182354&r2=1182355&view=diff
==============================================================================
--- avro/trunk/lang/c++/test/AvrogencppTests.cc (original)
+++ avro/trunk/lang/c++/test/AvrogencppTests.cc Wed Oct 12 13:26:55 2011
@@ -20,6 +20,7 @@
 #include "bigrecord2.hh"
 #include "union_array_union.hh"
 #include "union_map_union.hh"
+#include "union_conflict.hh"
 #include "recursive.hh"
 #include "circulardep.hh"
 #include "reuse.hh"



Mime
View raw message