avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nkol...@apache.org
Subject [avro] branch master updated: AVRO-2375: Allow repeating AvroMeta use (#506)
Date Tue, 16 Apr 2019 08:36:25 GMT
This is an automated email from the ASF dual-hosted git repository.

nkollar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new 94b3fae  AVRO-2375: Allow repeating AvroMeta use (#506)
94b3fae is described below

commit 94b3faeccfd7507d63a69027d7d1703a4bd18c9d
Author: ivangreene <igreene@fanthreesixty.com>
AuthorDate: Tue Apr 16 03:36:19 2019 -0500

    AVRO-2375: Allow repeating AvroMeta use (#506)
---
 .../java/org/apache/avro/reflect/AvroMeta.java     |  8 ++++
 .../java/org/apache/avro/reflect/ReflectData.java  | 16 ++++++--
 .../java/org/apache/avro/reflect/TestReflect.java  | 45 +++++++++++++++++++++-
 3 files changed, 63 insertions(+), 6 deletions(-)

diff --git a/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroMeta.java b/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroMeta.java
index 3c25e46..a09ada1 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroMeta.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/reflect/AvroMeta.java
@@ -18,6 +18,7 @@
 package org.apache.avro.reflect;
 
 import java.lang.annotation.ElementType;
+import java.lang.annotation.Repeatable;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
@@ -28,8 +29,15 @@ import java.lang.annotation.Target;
  */
 @Retention(RetentionPolicy.RUNTIME)
 @Target({ ElementType.TYPE, ElementType.FIELD })
+@Repeatable(AvroMeta.AvroMetas.class)
 public @interface AvroMeta {
   String key();
 
   String value();
+
+  @Retention(RetentionPolicy.RUNTIME)
+  @Target({ ElementType.TYPE, ElementType.FIELD })
+  @interface AvroMetas {
+    AvroMeta[] value();
+  }
 }
diff --git a/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java b/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
index 372b81e..0c3ea2c 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectData.java
@@ -629,9 +629,13 @@ public class ReflectData extends SpecificData {
               String fieldName = (annotatedName != null) ? annotatedName.value() : field.getName();
               Schema.Field recordField = new Schema.Field(fieldName, fieldSchema, doc, defaultValue);
 
-              AvroMeta meta = field.getAnnotation(AvroMeta.class); // add metadata
-              if (meta != null)
+              AvroMeta[] metadata = field.getAnnotationsByType(AvroMeta.class); // add metadata
+              for (AvroMeta meta : metadata) {
+                if (recordField.getObjectProps().containsKey(meta.key())) {
+                  throw new AvroTypeException("Duplicate field prop key: " + meta.key());
+                }
                 recordField.addProp(meta.key(), meta.value());
+              }
               for (Schema.Field f : fields) {
                 if (f.name().equals(fieldName))
                   throw new AvroTypeException("double field entry: " + fieldName);
@@ -644,9 +648,13 @@ public class ReflectData extends SpecificData {
           if (error) // add Throwable message
             fields.add(new Schema.Field("detailMessage", THROWABLE_MESSAGE, null, null));
           schema.setFields(fields);
-          AvroMeta meta = c.getAnnotation(AvroMeta.class);
-          if (meta != null)
+          AvroMeta[] metadata = c.getAnnotationsByType(AvroMeta.class);
+          for (AvroMeta meta : metadata) {
+            if (schema.getObjectProps().containsKey(meta.key())) {
+              throw new AvroTypeException("Duplicate type prop key: " + meta.key());
+            }
             schema.addProp(meta.key(), meta.value());
+          }
         }
         names.put(fullName, schema);
       }
diff --git a/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java b/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java
index e68fdc4..27bce9f 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java
@@ -608,6 +608,7 @@ public class TestReflect {
         + "\"org.apache.avro.reflect.TestReflect\",\"fields\":[]}");
   }
 
+  @AvroMeta(key = "X", value = "Y")
   public static class RAvroMeta {
     @AvroMeta(key = "K", value = "V")
     int a;
@@ -615,8 +616,48 @@ public class TestReflect {
 
   @Test
   public void testAnnotationAvroMeta() throws Exception {
-    check(RAvroMeta.class, "{\"type\":\"record\",\"name\":\"RAvroMeta\",\"namespace\":"
-        + "\"org.apache.avro.reflect.TestReflect\",\"fields\":[" + "{\"name\":\"a\",\"type\":\"int\",\"K\":\"V\"}]}");
+    check(RAvroMeta.class,
+        "{\"type\":\"record\",\"name\":\"RAvroMeta\",\"namespace\":"
+            + "\"org.apache.avro.reflect.TestReflect\",\"fields\":[" + "{\"name\":\"a\",\"type\":\"int\",\"K\":\"V\"}]"
+            + ",\"X\":\"Y\"}");
+  }
+
+  @AvroMeta(key = "X", value = "Y")
+  @AvroMeta(key = "A", value = "B")
+  public static class RAvroMultiMeta {
+    @AvroMeta(key = "K", value = "V")
+    @AvroMeta(key = "L", value = "W")
+    int a;
+  }
+
+  @Test
+  public void testAnnotationMultiAvroMeta() {
+    check(RAvroMultiMeta.class,
+        "{\"type\":\"record\",\"name\":\"RAvroMultiMeta\",\"namespace\":"
+            + "\"org.apache.avro.reflect.TestReflect\",\"fields\":["
+            + "{\"name\":\"a\",\"type\":\"int\",\"K\":\"V\",\"L\":\"W\"}]" + ",\"X\":\"Y\",\"A\":\"B\"}");
+  }
+
+  public static class RAvroDuplicateFieldMeta {
+    @AvroMeta(key = "K", value = "V")
+    @AvroMeta(key = "K", value = "W")
+    int a;
+  }
+
+  @Test(expected = AvroTypeException.class)
+  public void testAnnotationDuplicateFieldAvroMeta() {
+    ReflectData.get().getSchema(RAvroDuplicateFieldMeta.class);
+  }
+
+  @AvroMeta(key = "K", value = "V")
+  @AvroMeta(key = "K", value = "W")
+  public static class RAvroDuplicateTypeMeta {
+    int a;
+  }
+
+  @Test(expected = AvroTypeException.class)
+  public void testAnnotationDuplicateTypeAvroMeta() {
+    ReflectData.get().getSchema(RAvroDuplicateTypeMeta.class);
   }
 
   public static class RAvroName {


Mime
View raw message