Repository: avro
Updated Branches:
  refs/heads/master 57983af07 -> cc31afef4


Fix for AVRO-1750


Project: http://git-wip-us.apache.org/repos/asf/avro/repo
Commit: http://git-wip-us.apache.org/repos/asf/avro/commit/cc31afef
Tree: http://git-wip-us.apache.org/repos/asf/avro/tree/cc31afef
Diff: http://git-wip-us.apache.org/repos/asf/avro/diff/cc31afef

Branch: refs/heads/master
Commit: cc31afef46b76eef0feb60c1e506db206d0df593
Parents: 57983af
Author: Thiruvalluvan M G <[email protected]>
Authored: Sun Jan 29 19:03:58 2017 +0530
Committer: Thiruvalluvan M G <[email protected]>
Committed: Sun Jan 29 19:03:58 2017 +0530

----------------------------------------------------------------------
 CHANGES.txt                  |  2 ++
 lang/c++/api/GenericDatum.hh | 32 ++++++++++++++++++++++----------
 lang/c++/impl/Generic.cc     |  4 ----
 3 files changed, 24 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/avro/blob/cc31afef/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 5a7137a..c2949b8 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -125,6 +125,8 @@ Trunk (not yet released)
 
     AVRO-1866. JsonNullFormatter fwd-declared as class, defined as struct ( 
Pietro Cerutti via thiru)
 
+    AVRO-1750. GenericDatum API behavior breaking change (thiru)
+
 Avro 1.8.1 (14 May 2016)
 
   INCOMPATIBLE CHANGES

http://git-wip-us.apache.org/repos/asf/avro/blob/cc31afef/lang/c++/api/GenericDatum.hh
----------------------------------------------------------------------
diff --git a/lang/c++/api/GenericDatum.hh b/lang/c++/api/GenericDatum.hh
index 5efcb7f..2b1b3a4 100644
--- a/lang/c++/api/GenericDatum.hh
+++ b/lang/c++/api/GenericDatum.hh
@@ -66,18 +66,14 @@ public:
     /**
      * The avro data type this datum holds.
      */
-    Type type() const {
-        return type_;
-    }
+    Type type() const;
 
     /**
      * Returns the value held by this datum.
      * T The type for the value. This must correspond to the
      * avro type returned by type().
      */
-    template<typename T> const T& value() const {
-        return *boost::any_cast<T>(&value_);
-    }
+    template<typename T> const T& value() const;
 
     /**
      * Returns the reference to the value held by this datum, which
@@ -88,9 +84,7 @@ public:
      * T The type for the value. This must correspond to the
      * avro type returned by type().
      */
-    template<typename T> T& value() {
-        return *boost::any_cast<T>(&value_);
-    }
+    template<typename T> T& value();
 
     /**
      * Returns true if and only if this datum is a union.
@@ -153,7 +147,7 @@ public:
     GenericDatum(const NodePtr& schema, const T& v) :
         type_(schema->type()) {
         init(schema);
-        value<T>() = v;
+        *boost::any_cast<T>(&value_) = v;
     }
 
     /**
@@ -493,6 +487,24 @@ public:
     }
 };
 
+inline Type GenericDatum::type() const {
+    return (type_ == AVRO_UNION) ?
+        boost::any_cast<GenericUnion>(&value_)->datum().type() :
+        type_;
+}
+
+template<typename T> T& GenericDatum::value() {
+    return (type_ == AVRO_UNION) ?
+        boost::any_cast<GenericUnion>(&value_)->datum().value<T>() :
+        *boost::any_cast<T>(&value_);
+}
+
+template<typename T> const T& GenericDatum::value() const {
+    return (type_ == AVRO_UNION) ?
+        boost::any_cast<GenericUnion>(&value_)->datum().value<T>() :
+        *boost::any_cast<T>(&value_);
+}
+
 inline size_t GenericDatum::unionBranch() const {
     return boost::any_cast<GenericUnion>(&value_)->currentBranch();
 }

http://git-wip-us.apache.org/repos/asf/avro/blob/cc31afef/lang/c++/impl/Generic.cc
----------------------------------------------------------------------
diff --git a/lang/c++/impl/Generic.cc b/lang/c++/impl/Generic.cc
index 884fadb..9eaa56f 100644
--- a/lang/c++/impl/Generic.cc
+++ b/lang/c++/impl/Generic.cc
@@ -58,8 +58,6 @@ void GenericReader::read(GenericDatum& datum, Decoder& d, 
bool isResolving)
 {
     if (datum.isUnion()) {
         datum.selectBranch(d.decodeUnionIndex());
-        read(datum.value<GenericUnion>().datum(), d, isResolving);
-        return;
     }
     switch (datum.type()) {
     case AVRO_NULL:
@@ -176,8 +174,6 @@ void GenericWriter::write(const GenericDatum& datum, 
Encoder& e)
 {
     if (datum.isUnion()) {
         e.encodeUnionIndex(datum.unionBranch());
-        write(datum.value<GenericUnion>().datum(), e);
-        return;
     }
     switch (datum.type()) {
     case AVRO_NULL:

Reply via email to