avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject avro git commit: AVRO-1819, AVRO-1820: Java: Add aliases, custom properties to Protocol. Contributed by Konstantin Usachev.
Date Wed, 20 Apr 2016 16:57:59 GMT
Repository: avro
Updated Branches:
  refs/heads/master b30b9e7a3 -> 4c1463cba


AVRO-1819, AVRO-1820: Java: Add aliases, custom properties to Protocol. Contributed by Konstantin
Usachev.


Project: http://git-wip-us.apache.org/repos/asf/avro/repo
Commit: http://git-wip-us.apache.org/repos/asf/avro/commit/4c1463cb
Tree: http://git-wip-us.apache.org/repos/asf/avro/tree/4c1463cb
Diff: http://git-wip-us.apache.org/repos/asf/avro/diff/4c1463cb

Branch: refs/heads/master
Commit: 4c1463cba1bc268c2ccf2a506d27e6ee84030316
Parents: b30b9e7
Author: k.usachev <k.usachev@corp.mail.ru>
Authored: Sun Apr 3 14:43:07 2016 +0300
Committer: Ryan Blue <blue@apache.org>
Committed: Wed Apr 20 09:57:39 2016 -0700

----------------------------------------------------------------------
 CHANGES.txt                                     |  3 ++
 .../src/main/java/org/apache/avro/Protocol.java | 24 ++++++++++++--
 .../src/main/java/org/apache/avro/Schema.java   |  2 +-
 .../org/apache/avro/TestProtocolParsing.java    | 34 ++++++++++++++++----
 share/test/schemas/simple.avpr                  |  2 +-
 5 files changed, 53 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/avro/blob/4c1463cb/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index c60a81c..b1c3799 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -24,6 +24,9 @@ Trunk (not yet released)
 
     AVRO-1825: Allow running build.sh dist under git (nielsbasjes)
 
+    AVRO-1819, AVRO-1820: Java: Add aliases, custom properties to Protocol.
+    (Konstantin Usachev via blue)
+
   BUG FIXES
 
     AVRO-1493. Java: Avoid the "Turkish Locale Problem". Schema fingerprints are

http://git-wip-us.apache.org/repos/asf/avro/blob/4c1463cb/lang/java/avro/src/main/java/org/apache/avro/Protocol.java
----------------------------------------------------------------------
diff --git a/lang/java/avro/src/main/java/org/apache/avro/Protocol.java b/lang/java/avro/src/main/java/org/apache/avro/Protocol.java
index 03844f6..9c896d2 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/Protocol.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/Protocol.java
@@ -70,6 +70,12 @@ public class Protocol extends JsonProperties {
                        "doc", "response","request", "errors", "one-way");
   }
 
+  private static final Set<String> FIELD_RESERVED = new HashSet<String>();
+  static {
+    Collections.addAll(FIELD_RESERVED,
+                       "name", "type", "doc", "default", "aliases");
+  }
+
   /** A protocol message. */
   public class Message extends JsonProperties {
     private String name;
@@ -482,9 +488,21 @@ public class Protocol extends JsonProperties {
       JsonNode fieldDocNode = field.get("doc");
       if (fieldDocNode != null)
         fieldDoc = fieldDocNode.getTextValue();
-      fields.add(new Field(name, Schema.parse(fieldTypeNode,types),
-                           fieldDoc,
-                           field.get("default")));
+      Field newField = new Field(name, Schema.parse(fieldTypeNode,types),
+                                 fieldDoc, field.get("default"));
+      Set<String> aliases = Schema.parseAliases(field);
+      if (aliases != null) {                      // add aliases
+        for (String alias : aliases)
+          newField.addAlias(alias);
+      }
+
+      Iterator<String> i = field.getFieldNames();
+      while (i.hasNext()) {                       // add properties
+        String prop = i.next();
+        if (!FIELD_RESERVED.contains(prop))      // ignore reserved
+          newField.addProp(prop, field.get(prop));
+      }
+      fields.add(newField);
     }
     Schema request = Schema.createRecord(fields);
     

http://git-wip-us.apache.org/repos/asf/avro/blob/4c1463cb/lang/java/avro/src/main/java/org/apache/avro/Schema.java
----------------------------------------------------------------------
diff --git a/lang/java/avro/src/main/java/org/apache/avro/Schema.java b/lang/java/avro/src/main/java/org/apache/avro/Schema.java
index 600f7aa..e2ba927 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/Schema.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/Schema.java
@@ -1344,7 +1344,7 @@ public abstract class Schema extends JsonProperties {
     }
   }
 
-  private static Set<String> parseAliases(JsonNode node) {
+  static Set<String> parseAliases(JsonNode node) {
     JsonNode aliasesNode = node.get("aliases");
     if (aliasesNode == null)
       return null;

http://git-wip-us.apache.org/repos/asf/avro/blob/4c1463cb/lang/java/ipc/src/test/java/org/apache/avro/TestProtocolParsing.java
----------------------------------------------------------------------
diff --git a/lang/java/ipc/src/test/java/org/apache/avro/TestProtocolParsing.java b/lang/java/ipc/src/test/java/org/apache/avro/TestProtocolParsing.java
index ba07225..208a3d9 100644
--- a/lang/java/ipc/src/test/java/org/apache/avro/TestProtocolParsing.java
+++ b/lang/java/ipc/src/test/java/org/apache/avro/TestProtocolParsing.java
@@ -18,6 +18,7 @@
 package org.apache.avro;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.File;
@@ -30,19 +31,18 @@ import org.apache.avro.Protocol.Message;
 public class TestProtocolParsing {
   public static Protocol getSimpleProtocol() throws IOException {
     File file = new File("../../../share/test/schemas/simple.avpr");
-    Protocol protocol = Protocol.parse(file);
-    return protocol;
+    return Protocol.parse(file);
   }
-  
+
   @Test
   public void testParsing() throws IOException {
     Protocol protocol = getSimpleProtocol();
-    
+
     assertEquals(protocol.getDoc(), "Protocol used for testing.");
     assertEquals(6, protocol.getMessages().size());
-    assertEquals("Pretend you're in a cave!", protocol.getMessages().get("echo").getDoc());
   
+    assertEquals("Pretend you're in a cave!", protocol.getMessages().get("echo").getDoc());
   }
-  
+
   private static Message parseMessage(String message) throws Exception {
     return Protocol.parse("{\"protocol\": \"org.foo.Bar\","
                           +"\"types\": [],"
@@ -51,7 +51,8 @@ public class TestProtocolParsing {
                           + "}}").getMessages().values().iterator().next();
   }
 
-  @Test public void oneWay() throws Exception {
+  @Test
+  public void oneWay() throws Exception {
     Message m;
     // permit one-way messages w/ null resposne
     m = parseMessage("\"ack\": {"
@@ -84,4 +85,23 @@ public class TestProtocolParsing {
                  +"\"one-way\": true}");
   }
 
+  @Test
+  public void testMessageFieldAliases() throws IOException{
+    Protocol protocol = getSimpleProtocol();
+    final Message msg = protocol.getMessages().get("hello");
+    assertNotNull(msg);
+    final Schema.Field field = msg.getRequest().getField("greeting");
+    assertNotNull(field);
+    assertTrue(field.aliases().contains("salute"));
+  }
+
+  @Test
+  public void testMessageCustomProperties() throws IOException{
+    Protocol protocol = getSimpleProtocol();
+    final Message msg = protocol.getMessages().get("hello");
+    assertNotNull(msg);
+    final Schema.Field field = msg.getRequest().getField("greeting");
+    assertNotNull(field);
+    assertEquals("customValue", field.getProp("customProp"));
+  }
 }

http://git-wip-us.apache.org/repos/asf/avro/blob/4c1463cb/share/test/schemas/simple.avpr
----------------------------------------------------------------------
diff --git a/share/test/schemas/simple.avpr b/share/test/schemas/simple.avpr
index 7165ad8..c1f2a4c 100644
--- a/share/test/schemas/simple.avpr
+++ b/share/test/schemas/simple.avpr
@@ -40,7 +40,7 @@
 
      "hello": {
          "doc": "Send a greeting",
-         "request": [{"name": "greeting", "type": "string"}],
+         "request": [{"name": "greeting", "type": "string", "aliases" : [ "salute" ], "customProp"
: "customValue"}],
          "response": "string"
      },
 


Mime
View raw message