You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by cu...@apache.org on 2009/08/19 00:32:23 UTC

svn commit: r805610 - in /hadoop/avro/trunk: CHANGES.txt src/java/org/apache/avro/specific/SpecificCompiler.java

Author: cutting
Date: Tue Aug 18 22:32:22 2009
New Revision: 805610

URL: http://svn.apache.org/viewvc?rev=805610&view=rev
Log:
AVRO-83. In generated Java code, elide unions with null.

Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java

Modified: hadoop/avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=805610&r1=805609&r2=805610&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Tue Aug 18 22:32:22 2009
@@ -33,6 +33,8 @@
     AVRO-89. In fields of Java generated classes, use unboxed numeric
     types.  (cutting)
 
+    AVRO-83. In generated Java code, elide unions with null. (cutting)
+
   OPTIMIZATIONS
 
   BUG FIXES

Modified: hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java?rev=805610&r1=805609&r2=805610&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java (original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java Tue Aug 18 22:32:22 2009
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.util.Map;
 import java.util.Set;
+import java.util.List;
 import java.util.HashSet;
 
 import org.apache.avro.Protocol;
@@ -223,6 +224,8 @@
     }
   }
 
+  private static final Schema NULL_SCHEMA = Schema.create(Schema.Type.NULL);
+
   private String type(Schema schema, String name) {
     switch (schema.getType()) {
     case RECORD:
@@ -233,7 +236,11 @@
       return "GenericArray<"+type(schema.getElementType(),name+"Element")+">";
     case MAP:
       return "Map<Utf8,"+type(schema.getValueType(),name+"Value")+">";
-    case UNION:   return "Object";
+    case UNION:
+      List<Schema> types = schema.getTypes();     // elide unions with null
+      if ((types.size() == 2) && types.contains(NULL_SCHEMA))
+        return type(types.get(types.get(0).equals(NULL_SCHEMA) ? 1 : 0), name);
+      return "Object";
     case STRING:  return "Utf8";
     case BYTES:   return "ByteBuffer";
     case INT:     return "Integer";