avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1211702 - in /avro/trunk: CHANGES.txt lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java lang/java/avro/src/test/java/org/apache/avro/TestReflect.java
Date Wed, 07 Dec 2011 23:15:58 GMT
Author: cutting
Date: Wed Dec  7 23:15:57 2011
New Revision: 1211702

URL: http://svn.apache.org/viewvc?rev=1211702&view=rev
Log:
AVRO-966. Java: Fix ReflectDatumWriter to be able to correctly write unions containing Collection
and/or ByteBuffer.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1211702&r1=1211701&r2=1211702&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Dec  7 23:15:57 2011
@@ -19,6 +19,9 @@ Avro 1.6.2 (unreleased)
     AVRO-835. C#: Fix codgen for protocols to not fail.
     (Dona Alvarez via cutting)
 
+    AVRO-966. Java: Fix ReflectDatumWriter to be able to correctly
+    write unions containing Collection and/or ByteBuffer. (cutting)
+
 Avro 1.6.1 (8 November 2011)
 
   INCOMPATIBLE CHANGES

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java?rev=1211702&r1=1211701&r2=1211702&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java Wed Dec
 7 23:15:57 2011
@@ -24,6 +24,7 @@ import java.lang.reflect.Type;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.GenericArrayType;
 import java.lang.annotation.Annotation;
+import java.nio.ByteBuffer;
 import java.util.Collection;
 import java.util.List;
 import java.util.ArrayList;
@@ -105,6 +106,7 @@ public class ReflectData extends Specifi
   protected boolean isRecord(Object datum) {
     if (datum == null) return false;
     if (super.isRecord(datum)) return true;
+    if (datum instanceof Collection) return false;
     return getSchema(datum.getClass()).getType() == Schema.Type.RECORD;
   }
 
@@ -257,6 +259,10 @@ public class ReflectData extends Specifi
       }
       if (CharSequence.class.isAssignableFrom(c))            // String
         return Schema.create(Schema.Type.STRING);
+      if (ByteBuffer.class.isAssignableFrom(c))              // bytes
+        return Schema.create(Schema.Type.BYTES);
+      if (Collection.class.isAssignableFrom(c))              // array
+        throw new AvroRuntimeException("Can't find element type of Collection");
       String fullName = c.getName();
       Schema schema = names.get(fullName);
       if (schema == null) {

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java?rev=1211702&r1=1211701&r2=1211702&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java (original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/TestReflect.java Wed Dec  7 23:15:57
2011
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertTru
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.nio.ByteBuffer;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -93,6 +94,19 @@ public class TestReflect {
     check(new byte[0], "\"bytes\"");
   }
 
+  @Test public void testUnionWithCollection() {
+    Schema s = Schema.parse
+      ("[\"null\", {\"type\":\"array\",\"items\":\"float\"}]");
+    GenericData data = ReflectData.get();
+    assertEquals(1, data.resolveUnion(s, new ArrayList<Float>()));
+  }
+
+  @Test public void testUnionWithBytes() {
+    Schema s = Schema.parse ("[\"null\", \"bytes\"]");
+    GenericData data = ReflectData.get();
+    assertEquals(1, data.resolveUnion(s, ByteBuffer.wrap(new byte[]{1})));
+  }
+
   // test map, array and list type inference
   public static class R1 {
     private Map<String,String> mapField = new HashMap<String,String>();



Mime
View raw message