Repository: avro Updated Branches: refs/heads/master ce36fffca -> ec8514f71
Fixed AVRO-1898, C++ library cannot parse unions with default values Project: http://git-wip-us.apache.org/repos/asf/avro/repo Commit: http://git-wip-us.apache.org/repos/asf/avro/commit/ec8514f7 Tree: http://git-wip-us.apache.org/repos/asf/avro/tree/ec8514f7 Diff: http://git-wip-us.apache.org/repos/asf/avro/diff/ec8514f7 Branch: refs/heads/master Commit: ec8514f711b739307d460b23bcb6eccc784f419a Parents: ce36fff Author: Thiruvalluvan M. G <[email protected]> Authored: Sat Feb 18 08:48:43 2017 +0530 Committer: Thiruvalluvan M G <[email protected]> Committed: Sat Feb 18 08:51:23 2017 +0530 ---------------------------------------------------------------------- CHANGES.txt | 2 ++ lang/c++/impl/Compiler.cc | 37 +++++++---------------------------- lang/c++/jsonschemas/bigrecord_r | 6 ++---- lang/c++/test/AvrogencppTests.cc | 3 ++- 4 files changed, 13 insertions(+), 35 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/avro/blob/ec8514f7/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt index a7cfb38..83c9a4d 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -139,6 +139,8 @@ Trunk (not yet released) AVRO-1937: C++ generator for recursive structure crashes (thiru) + AVRO-1892. C++ library cannot parse unions with default values (Hua Zhang via thiru) + Avro 1.8.1 (14 May 2016) INCOMPATIBLE CHANGES http://git-wip-us.apache.org/repos/asf/avro/blob/ec8514f7/lang/c++/impl/Compiler.cc ---------------------------------------------------------------------- diff --git a/lang/c++/impl/Compiler.cc b/lang/c++/impl/Compiler.cc index 9670844..be5fe3f 100644 --- a/lang/c++/impl/Compiler.cc +++ b/lang/c++/impl/Compiler.cc @@ -79,7 +79,7 @@ static bool isFullName(const string& s) { return s.find('.') != string::npos; } - + static Name getName(const string& name, const string& ns) { return (isFullName(name)) ? Name(name) : Name(name, ns); @@ -143,7 +143,7 @@ const int64_t getLongField(const Entity& e, const Object& m, ensureType<int64_t>(it->second, fieldName); return it->second.longValue(); } - + struct Field { const string& name; const NodePtr schema; @@ -283,32 +283,9 @@ static GenericDatum makeGenericDatum(NodePtr n, const Entity& e, case AVRO_UNION: { GenericUnion result(n); - string name; - Entity e2; - if (e.type() == json::etNull) { - name = "null"; - e2 = e; - } else { - assertType(e, json::etObject); - const map<string, Entity>& v = e.objectValue(); - if (v.size() != 1) { - throw Exception(boost::format("Default value for " - "union has more than one field: %1%") % e.toString()); - } - map<string, Entity>::const_iterator it = v.begin(); - name = it->first; - e2 = it->second; - } - for (size_t i = 0; i < n->leaves(); ++i) { - const NodePtr& b = n->leafAt(i); - if (nameof(b) == name) { - result.selectBranch(i); - result.datum() = makeGenericDatum(b, e2, st); - return GenericDatum(n, result); - } - } - throw Exception(boost::format("Invalid default value %1%") % - e.toString()); + result.selectBranch(0); + result.datum() = makeGenericDatum(n->leafAt(0), e, st); + return GenericDatum(n, result); } case AVRO_FIXED: assertType(e, json::etString); @@ -334,12 +311,12 @@ static Field makeField(const Entity& e, SymbolTable& st, const string& ns) static NodePtr makeRecordNode(const Entity& e, const Name& name, const Object& m, SymbolTable& st, const string& ns) -{ +{ const Array& v = getArrayField(e, m, "fields"); concepts::MultiAttribute<string> fieldNames; concepts::MultiAttribute<NodePtr> fieldValues; vector<GenericDatum> defaultValues; - + for (Array::const_iterator it = v.begin(); it != v.end(); ++it) { Field f = makeField(*it, st, ns); fieldNames.add(f.name); http://git-wip-us.apache.org/repos/asf/avro/blob/ec8514f7/lang/c++/jsonschemas/bigrecord_r ---------------------------------------------------------------------- diff --git a/lang/c++/jsonschemas/bigrecord_r b/lang/c++/jsonschemas/bigrecord_r index f079162..7c477cd 100644 --- a/lang/c++/jsonschemas/bigrecord_r +++ b/lang/c++/jsonschemas/bigrecord_r @@ -70,13 +70,11 @@ { "name": "union1WithDefaultValue", "type": [ "string", "int" ], - "default": { - "string": "sval" - } + "default": "sval" }, { "name": "union2WithDefaultValue", - "type": [ "string", "null" ], + "type": [ "null", "string" ], "default": null }, { http://git-wip-us.apache.org/repos/asf/avro/blob/ec8514f7/lang/c++/test/AvrogencppTests.cc ---------------------------------------------------------------------- diff --git a/lang/c++/test/AvrogencppTests.cc b/lang/c++/test/AvrogencppTests.cc index 24c5870..add78f5 100644 --- a/lang/c++/test/AvrogencppTests.cc +++ b/lang/c++/test/AvrogencppTests.cc @@ -27,7 +27,8 @@ #include "recursive.hh" #include "circulardep.hh" #include "reuse.hh" -#include "tree.hh" +#include "tree1.hh" +#include "tree2.hh" #include "crossref.hh" #include "Compiler.hh"
