avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r905827 - in /hadoop/avro/trunk: CHANGES.txt lang/java/src/java/org/apache/avro/generic/GenericData.java lang/java/src/test/java/org/apache/avro/generic/TestGenericData.java
Date Tue, 02 Feb 2010 22:51:58 GMT
Author: cutting
Date: Tue Feb  2 22:51:57 2010
New Revision: 905827

URL: http://svn.apache.org/viewvc?rev=905827&view=rev
Log:
AVRO-350.  Fix GenericData.Record#get(String) to return null rather than throw NPE when passed
a field name not in the record.  Contributed by Kevin Oliver.

Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java
    hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/generic/TestGenericData.java

Modified: hadoop/avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=905827&r1=905826&r2=905827&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Tue Feb  2 22:51:57 2010
@@ -414,6 +414,10 @@
 
     AVRO-361. Specific Compiler fails to handle union with two fixed branches (Scott Carey
via philz)
 
+    AVRO-350. Fix GenericData.Record#get(String) to return null rather than
+    throw NPE when passed a field name that's not in the record.
+    (Kevin Oliver via cutting)
+
 Avro 1.2.0 (14 October 2009)
 
   INCOMPATIBLE CHANGES

Modified: hadoop/avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java?rev=905827&r1=905826&r2=905827&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java (original)
+++ hadoop/avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java Tue Feb
 2 22:51:57 2010
@@ -56,7 +56,9 @@
     }
     @Override public void put(int i, Object v) { values[i] = v; }
     @Override public Object get(String key) {
-      return values[schema.getFields().get(key).pos()];
+      Field field = schema.getFields().get(key);
+      if (field == null) return null;
+      return values[field.pos()];
     }
     @Override public Object get(int i) { return values[i]; }
     @Override public boolean equals(Object o) {

Modified: hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/generic/TestGenericData.java
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/generic/TestGenericData.java?rev=905827&r1=905826&r2=905827&view=diff
==============================================================================
--- hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/generic/TestGenericData.java
(original)
+++ hadoop/avro/trunk/lang/java/src/test/java/org/apache/avro/generic/TestGenericData.java
Tue Feb  2 22:51:57 2010
@@ -17,11 +17,14 @@
  */
 package org.apache.avro.generic;
 
+import java.util.LinkedHashMap;
+
 import static org.junit.Assert.*;
 
 import java.util.Arrays;
 
 import org.apache.avro.Schema;
+import org.apache.avro.Schema.Field;
 import org.apache.avro.AvroRuntimeException;
 import org.apache.avro.Schema.Type;
 
@@ -57,4 +60,12 @@
         Arrays.asList(Schema.create(Type.BOOLEAN), Schema.create(Type.STRING))));
   }
 
+  @Test
+  public void testRecordGetFieldDoesntExist() throws Exception {
+    LinkedHashMap<String, Field> fields = new LinkedHashMap<String, Field>();
+    Schema schema = Schema.createRecord(fields);
+    GenericData.Record record = new GenericData.Record(schema);
+    assertNull(record.get("does not exist"));
+  }
+    
 }



Mime
View raw message