avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1550260 - in /avro/trunk: ./ lang/java/avro/src/main/java/org/apache/avro/ lang/java/avro/src/main/java/org/apache/avro/reflect/ lang/java/avro/src/test/java/org/apache/avro/reflect/
Date Wed, 11 Dec 2013 21:16:39 GMT
Author: cutting
Date: Wed Dec 11 21:16:39 2013
New Revision: 1550260

URL: http://svn.apache.org/r1550260
Log:
AVRO-1400. Java: Add AvroDefault reflect annotation to specify default values.

Added:
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroDefault.java   (with
props)
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java
    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=1550260&r1=1550259&r2=1550260&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Dec 11 21:16:39 2013
@@ -12,6 +12,9 @@ Trunk (not yet released)
     AVRO-1373. Java: Add support for "xz" compresssion codec, using LZMA2.
     (Nick White via cutting)
 
+    AVRO-1400. Java: Add AvroDefault reflect annotation to specify
+    default values. (cutting)
+
   IMPROVEMENTS
 
     AVRO-1355. Java: Reject schemas with duplicate field

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java?rev=1550260&r1=1550259&r2=1550260&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/Schema.java Wed Dec 11 21:16:39
2013
@@ -1230,7 +1230,8 @@ public abstract class Schema extends Jso
     return jsonNode != null ? jsonNode.getTextValue() : null;
   }
 
-  static JsonNode parseJson(String s) {
+  /** Parses a string as Json. */
+  public static JsonNode parseJson(String s) {
     try {
       return MAPPER.readTree(FACTORY.createJsonParser(new StringReader(s)));
     } catch (JsonParseException e) {

Added: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroDefault.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroDefault.java?rev=1550260&view=auto
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroDefault.java (added)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroDefault.java Wed Dec
11 21:16:39 2013
@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro.reflect;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Specifies a default value for a field as a JSON string.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.FIELD})
+public @interface AvroDefault {
+  String value();
+}

Propchange: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroDefault.java
------------------------------------------------------------------------------
    svn:eol-style = native

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=1550260&r1=1550259&r2=1550260&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
11 21:16:39 2013
@@ -465,8 +465,14 @@ public class ReflectData extends Specifi
             if ((field.getModifiers()&(Modifier.TRANSIENT|Modifier.STATIC))==0 
                 && !field.isAnnotationPresent(AvroIgnore.class)) {
               Schema fieldSchema = createFieldSchema(field, names);
-              JsonNode defaultValue = null;
-              if (fieldSchema.getType() == Schema.Type.UNION) {
+              AvroDefault defaultAnnotation
+                = field.getAnnotation(AvroDefault.class);
+              JsonNode defaultValue = (defaultAnnotation == null)
+                ? null
+                : Schema.parseJson(defaultAnnotation.value());
+              
+              if (defaultValue == null
+                  && fieldSchema.getType() == Schema.Type.UNION) {
                 Schema defaultType = fieldSchema.getTypes().get(0);
                 if (defaultType.getType() == Schema.Type.NULL) {
                   defaultValue = NullNode.getInstance();

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=1550260&r1=1550259&r2=1550260&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 Dec
11 21:16:39 2013
@@ -943,4 +943,18 @@ public class TestReflect {
     check(AliasB.class, "{\"type\":\"record\",\"name\":\"AliasB\",\"namespace\":\"org.apache.avro.reflect.TestReflect$\",\"fields\":[],\"aliases\":[\"a\"]}");
     check(AliasC.class, "{\"type\":\"record\",\"name\":\"AliasC\",\"namespace\":\"org.apache.avro.reflect.TestReflect$\",\"fields\":[],\"aliases\":[\"a\"]}");
   
   }
+
+  private static class DefaultTest {
+    @AvroDefault("1")
+    int foo;
+  }  
+  
+  @Test
+  public void testAvroDefault() {
+    check(DefaultTest.class,
+          "{\"type\":\"record\",\"name\":\"DefaultTest\","
+          +"\"namespace\":\"org.apache.avro.reflect.TestReflect$\",\"fields\":["
+          +"{\"name\":\"foo\",\"type\":\"int\",\"default\":1}]}");
+  }
+
 }



Mime
View raw message