Author: thiru
Date: Fri Jan 10 07:10:58 2014
New Revision: 1557041
URL: http://svn.apache.org/r1557041
Log:
AVRO-1415. C++ binary encoder and decoder doesn't handle uninitialzed enums
Added:
avro/trunk/lang/c++/jsonschemas/empty_record
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=1557041&r1=1557040&r2=1557041&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Fri Jan 10 07:10:58 2014
@@ -84,6 +84,8 @@ Trunk (not yet released)
AVRO-1432. Java: Reduce javadoc warnings. (cutting)
+ AVRO-1415. C++ binary encoder and decoder doesn't handle uninitialzed
enums (Ramana Suvarapu via thiru)
+
BUG FIXES
AVRO-1368. Fix SpecificDatumWriter to, when writing a string
Modified: avro/trunk/lang/c++/CMakeLists.txt
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/CMakeLists.txt?rev=1557041&r1=1557040&r2=1557041&view=diff
==============================================================================
--- avro/trunk/lang/c++/CMakeLists.txt (original)
+++ avro/trunk/lang/c++/CMakeLists.txt Fri Jan 10 07:10:58 2014
@@ -122,6 +122,7 @@ macro (gen file ns)
add_custom_target (${file}_hh DEPENDS ${file}.hh)
endmacro (gen)
+gen (empty_record empty)
gen (bigrecord testgen)
gen (bigrecord2 testgen2)
gen (tweet testgen3)
@@ -162,7 +163,7 @@ endif()
add_dependencies (AvrogencppTests bigrecord_hh bigrecord2_hh tweet_hh
union_array_union_hh union_map_union_hh union_conflict_hh
- recursive_hh reuse_hh circulardep_hh)
+ recursive_hh reuse_hh circulardep_hh empty_record_hh)
include (InstallRequiredSystemLibraries)
Modified: avro/trunk/lang/c++/impl/avrogencpp.cc
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/impl/avrogencpp.cc?rev=1557041&r1=1557040&r2=1557041&view=diff
==============================================================================
--- avro/trunk/lang/c++/impl/avrogencpp.cc (original)
+++ avro/trunk/lang/c++/impl/avrogencpp.cc Fri Jan 10 07:10:58 2014
@@ -225,7 +225,8 @@ string CodeGen::generateRecordType(const
return it->second;
}
- os_ << "struct " << decorate(n->name()) << " {\n";
+ string decoratedName = decorate(n->name());
+ os_ << "struct " << decoratedName << " {\n";
if (! noUnion_) {
for (size_t i = 0; i < c; ++i) {
if (n->leafAt(i)->type() == avro::AVRO_UNION) {
@@ -242,6 +243,26 @@ string CodeGen::generateRecordType(const
}
os_ << ' ' << n->nameAt(i) << ";\n";
}
+
+ os_ << " " << decoratedName << "()";
+ if (c > 0) {
+ os_ << " :";
+ }
+ os_ << "\n";
+ for (size_t i = 0; i < c; ++i) {
+ os_ << " " << n->nameAt(i) << "(";
+ if (! noUnion_ && n->leafAt(i)->type() == avro::AVRO_UNION) {
+ os_ << n->nameAt(i) << "_t";
+ } else {
+ os_ << types[i];
+ }
+ os_ << "())";
+ if (i != (c - 1)) {
+ os_ << ',';
+ }
+ os_ << "\n";
+ }
+ os_ << " { }\n";
os_ << "};\n\n";
return decorate(n->name());
}
@@ -454,15 +475,45 @@ string CodeGen::generateDeclaration(cons
void CodeGen::generateEnumTraits(const NodePtr& n)
{
- string fn = fullname(decorate(n->name()));
- os_ << "template<> struct codec_traits<" << fn << "> {\n"
- << " static void encode(Encoder& e, " << fn << " v) {\n"
- << " e.encodeEnum(v);\n"
- << " }\n"
- << " static void decode(Decoder& d, " << fn << "& v) {\n"
- << " v = static_cast<" << fn << ">(d.decodeEnum());\n"
- << " }\n"
- << "};\n\n";
+ string dname = decorate(n->name());
+ string fn = fullname(dname);
+ size_t c = n->names();
+ string first;
+ string last;
+ if (!ns_.empty())
+ {
+ first = ns_;
+ first += "::";
+ first += n->nameAt(0);
+
+ last = ns_;
+ last += "::";
+ last += n->nameAt(c-1);
+ } else {
+ first = n->nameAt(0);
+ last = n->nameAt(c-1);
+ }
+ os_ << "template<> struct codec_traits<" << fn << "> {\n"
+ << " static void encode(Encoder& e, " << fn << " v) {\n"
+ << " if (v < " << first << " || v > " << last <<
")\n"
+ << " {\n"
+ << " std::ostringstream error;\n"
+ << " error << \"enum value \" << v << \" is
out of bound for " << fn << " and cannot be encoded\";\n"
+ << " throw avro::Exception(error.str());\n"
+ << " }\n"
+ << " e.encodeEnum(v);\n"
+ << " }\n"
+ << " static void decode(Decoder& d, " << fn << "& v) {\n"
+ << " size_t index = d.decodeEnum();\n"
+ << " if (index < " << first << " || index > " <<
last << ")\n"
+ << " {\n"
+ << " std::ostringstream error;\n"
+ << " error << \"enum value \" << index << \"
is out of bound for " << fn << " and cannot be decoded\";\n"
+ << " throw avro::Exception(error.str());\n"
+ << " }\n"
+ << " v = static_cast<" << fn << ">(index);\n"
+ << " }\n"
+ << "};\n\n";
}
void CodeGen::generateRecordTraits(const NodePtr& n)
@@ -622,7 +673,8 @@ void CodeGen::generate(const ValidSchema
os_ << "#ifndef " << h << "\n";
os_ << "#define " << h << "\n\n\n";
- os_ << "#include \"boost/any.hpp\"\n"
+ os_ << "#include <sstream>\n"
+ << "#include \"boost/any.hpp\"\n"
<< "#include \"" << includePrefix_ << "Specific.hh\"\n"
<< "#include \"" << includePrefix_ << "Encoder.hh\"\n"
<< "#include \"" << includePrefix_ << "Decoder.hh\"\n"
Added: avro/trunk/lang/c++/jsonschemas/empty_record
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/jsonschemas/empty_record?rev=1557041&view=auto
==============================================================================
--- avro/trunk/lang/c++/jsonschemas/empty_record (added)
+++ avro/trunk/lang/c++/jsonschemas/empty_record Fri Jan 10 07:10:58 2014
@@ -0,0 +1,5 @@
+{
+ "type": "record",
+ "name": "Empty",
+ "fields": []
+}
Modified: avro/trunk/lang/c++/test/AvrogencppTests.cc
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/test/AvrogencppTests.cc?rev=1557041&r1=1557040&r2=1557041&view=diff
==============================================================================
--- avro/trunk/lang/c++/test/AvrogencppTests.cc (original)
+++ avro/trunk/lang/c++/test/AvrogencppTests.cc Fri Jan 10 07:10:58 2014
@@ -16,6 +16,7 @@
* limitations under the License.
*/
+#include "empty_record.hh"
#include "bigrecord.hh"
#include "bigrecord2.hh"
#include "tweet.hh"