avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1384163 - 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/reflect/TestReflect.java
Date Wed, 12 Sep 2012 23:53:16 GMT
Author: cutting
Date: Wed Sep 12 23:53:15 2012
New Revision: 1384163

URL: http://svn.apache.org/viewvc?rev=1384163&view=rev
Log:
AVRO-1153. Java: Fix reflect to be able to write unions that contain stringable schemas. 
Contributed by Alexandre Normand.

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/reflect/TestReflect.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1384163&r1=1384162&r2=1384163&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Sep 12 23:53:15 2012
@@ -92,6 +92,9 @@ Avro 1.7.1 (16 July 2012)
     AVRO-1109. CSharp specific fails on multidimensional arrays.
     (Mark Farnan via thiru)
 
+    AVRO-1153. Java: Fix reflect to be able to write unions that
+    contain stringable schemas. (Alexandre Normand via cutting)
+
 Avro 1.7.0 (11 June 2012)
 
   NEW FEATURES

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=1384163&r1=1384162&r2=1384163&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 Sep
12 23:53:15 2012
@@ -271,8 +271,7 @@ public class ReflectData extends Specifi
       if (Map.class.isAssignableFrom(raw)) {                 // Map
         Schema schema = Schema.createMap(createSchema(params[1], names));
         Class key = (Class)params[0];
-        if (key.isAnnotationPresent(Stringable.class) || // Stringable key
-            stringableClasses.contains(key)) {
+        if (isStringable(key)) {                             // Stringable key
           schema.addProp(KEY_CLASS_PROP, key.getName());
         } else if (key != String.class) {
           throw new AvroTypeException("Map key class not String: "+key);
@@ -330,8 +329,7 @@ public class ReflectData extends Specifi
         Union union = c.getAnnotation(Union.class);
         if (union != null) {                                 // union annotated
           return getAnnotatedUnion(union, names);
-        } else if (c.isAnnotationPresent(Stringable.class) || // Stringable
-                   stringableClasses.contains(c)) {
+        } else if (isStringable(c)) {                        // Stringable
           Schema result = Schema.create(Schema.Type.STRING);
           result.addProp(CLASS_PROP, c.getName());
           return result;
@@ -376,6 +374,11 @@ public class ReflectData extends Specifi
     return super.createSchema(type, names);
   }
 
+  private boolean isStringable(Class<?> c) {
+    return c.isAnnotationPresent(Stringable.class) ||
+      stringableClasses.contains(c);
+  }
+
   private static final Schema THROWABLE_MESSAGE =
     makeNullable(Schema.create(Schema.Type.STRING));
 
@@ -509,6 +512,16 @@ public class ReflectData extends Specifi
   }
 
   @Override
+  protected String getSchemaName(Object datum) {
+    if (datum != null) {
+      Class c = datum.getClass();
+      if (isStringable(c))
+        return Schema.Type.STRING.getName();
+    }
+    return super.getSchemaName(datum);
+  }
+
+  @Override
   protected int compare(Object o1, Object o2, Schema s, boolean equals) {
     switch (s.getType()) {
     case ARRAY:

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java?rev=1384163&r1=1384162&r2=1384163&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java (original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java Wed Sep
12 23:53:15 2012
@@ -646,6 +646,18 @@ public class TestReflect {
     checkBinary(data, data.getSchema(M1.class), record, true);
   }
 
+  public static class NullableStringable {
+    java.math.BigDecimal number;
+  }
+
+  @Test public void testNullableStringableField() throws Exception {
+    NullableStringable datum = new NullableStringable();
+    datum.number = java.math.BigDecimal.TEN;
+
+    Schema schema = ReflectData.AllowNull.get().getSchema(NullableStringable.class);
+    checkBinary(schema, datum);
+  }
+
   public static void checkBinary(ReflectData reflectData, Schema schema,
                                  Object datum, boolean equals)
     throws IOException {



Mime
View raw message