avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r986950 - in /avro/trunk: ./ doc/src/content/xdocs/ lang/java/src/java/org/apache/avro/idl/ lang/java/src/java/org/apache/avro/tool/ lang/java/src/test/idl/input/ lang/java/src/test/idl/output/ lang/java/src/test/java/org/apache/avro/idl/
Date Wed, 18 Aug 2010 21:24:19 GMT
Author: cutting
Date: Wed Aug 18 21:24:18 2010
New Revision: 986950

URL: http://svn.apache.org/viewvc?rev=986950&view=rev
Log:
AVRO-495. IDL: Add support for file includes.

Added:
    avro/trunk/lang/java/src/test/idl/input/bar.avpr
    avro/trunk/lang/java/src/test/idl/input/foo.avsc
    avro/trunk/lang/java/src/test/idl/input/import.avdl
    avro/trunk/lang/java/src/test/idl/output/import.avpr
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/doc/src/content/xdocs/idl.xml
    avro/trunk/lang/java/src/java/org/apache/avro/idl/idl.jj
    avro/trunk/lang/java/src/java/org/apache/avro/tool/IdlTool.java
    avro/trunk/lang/java/src/test/java/org/apache/avro/idl/TestIdl.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=986950&r1=986949&r2=986950&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Wed Aug 18 21:24:18 2010
@@ -53,6 +53,8 @@ Avro 1.4.0 (unreleased)
     AVRO-600. Add support for type and field name aliases,
     facilitating schema migration. (cutting)
 
+    AVRO-495. IDL: Add support for file includes. (cutting)
+
   IMPROVEMENTS
 
     AVRO-587. Add Charts and Templating to Stats View

Modified: avro/trunk/doc/src/content/xdocs/idl.xml
URL: http://svn.apache.org/viewvc/avro/trunk/doc/src/content/xdocs/idl.xml?rev=986950&r1=986949&r2=986950&view=diff
==============================================================================
--- avro/trunk/doc/src/content/xdocs/idl.xml (original)
+++ avro/trunk/doc/src/content/xdocs/idl.xml Wed Aug 18 21:24:18 2010
@@ -105,17 +105,36 @@ protocol MyProtocol {
 }
       </source>
       <p>
-3        This notation is used throughout Avro IDL as a way of specifying properties for
the annotated element,
+        This notation is used throughout Avro IDL as a way of specifying properties for the
annotated element,
         as will be described later in this document.
       </p>
       <p>
         Protocols in Avro IDL can contain the following items:
       </p>
         <ul>
+          <li>Imports of external protocol and schema files.</li>
           <li>Definitions of named schemata, including <em>record</em>s,
<em>error</em>s, <em>enum</em>s, and <em>fixed</em>s.</li>
           <li>Definitions of RPC messages</li>
         </ul>
     </section>
+    <section id="imports">
+      <title>Imports</title>
+      <p>Files may be imported in one of three formats: </p>
+      <ul>
+        <li>An IDL file may be imported with a statement like:
+	  <source>import idl "foo.avdl";</source>
+	</li>
+        <li>A JSON protocol file may be imported with a statement like:
+	  <source>import protocol "foo.avpr";</source>
+	</li>
+        <li>A JSON schema file may be imported with a statement like:
+	  <source>import schema "foo.avsc";</source>
+	</li>
+      </ul>
+      <p>Messages and types in the imported file are added to this
+	file's protocol.</p>
+      <p>Imported file names are resolved relative to the current IDL file.</p>
+    </section>
     <section id="format_enums">
       <title>Defining an Enumeration</title>
       <p>

Modified: avro/trunk/lang/java/src/java/org/apache/avro/idl/idl.jj
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/idl/idl.jj?rev=986950&r1=986949&r2=986950&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/idl/idl.jj (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/idl/idl.jj Wed Aug 18 21:24:18 2010
@@ -87,8 +87,15 @@ public class Idl
 {
   static JsonNodeFactory FACTORY = JsonNodeFactory.instance;
 
+  File inputDir = new File(".");
   String namespace;
   Map<String,Schema> names = new LinkedHashMap<String,Schema>();
+
+  public Idl(File inputFile) throws IOException {
+    this(new FileInputStream(inputFile), "UTF-8");
+    this.inputDir = inputFile.getParentFile();
+  }
+
 }
 
 PARSER_END(Idl)
@@ -148,10 +155,13 @@ TOKEN :
 | < FALSE: "false" >
 | < FIXED: "fixed" >
 | < FLOAT: "float" >
+| < IDL: "idl" >
+| < IMPORT: "import" >
 | < INT: "int" >
 | < LONG: "long" >
 | < MAP: "map" >
 | < BYTES: "bytes" >
+| < SCHEMA: "schema" >
 | < STRING: "string" >
 | < NULL: "null" >
 | < PROTOCOL: "protocol" >
@@ -1042,30 +1052,78 @@ void ProtocolBody(Protocol p):
 {
   Schema schema;
   Message message;
-  List<Schema> schemata = new ArrayList<Schema>();
+  Protocol importProtocol;
 }
 {
-  "{"
-  (
-      schema = NamedSchemaDeclaration()
-      {
-        schemata.add(schema);
-      }
-
-    | message = MessageDeclaration(p)
-      {
-        // TODO this is kind of sketch!!
-        p.getMessages().put(message.getName(), message);
-      }
+  "{" 
+  (  
+   schema = NamedSchemaDeclaration()
+
+   | <IMPORT>
+   ((( importProtocol = ImportIdl() | importProtocol = ImportProtocol()) {
+       for (Schema s : importProtocol.getTypes())
+         names.put(s.getFullName(), s);
+       p.getMessages().putAll(importProtocol.getMessages());
+     })
+     | schema = ImportSchema()  {
+     names.put(schema.getFullName(), schema);
+   })
+      
+   | message = MessageDeclaration(p) {
+     p.getMessages().put(message.getName(), message);
+   }
   ) *
   "}"
 
   {
-    p.setTypes(schemata);
+    p.setTypes(names.values());
   }
 }
 
 
+Protocol ImportIdl() : {
+  String importFile;
+}
+{
+  <IDL> importFile = JsonString() ";"
+    {
+      try {
+        return new Idl(new File(inputDir, importFile)).CompilationUnit();
+      } catch (IOException e) {
+        throw new ParseException("Error importing "+importFile+": "+e);
+      }        
+    }
+}
+
+Protocol ImportProtocol() : {
+  String importFile;
+}
+{
+  <PROTOCOL> importFile = JsonString() ";"
+    {
+
+      try {
+        return Protocol.parse(new File(inputDir, importFile));
+      } catch (IOException e) {
+        throw new ParseException("Error importing "+importFile+": "+e);
+      }        
+    }
+}
+
+Schema ImportSchema() : {
+  String importFile;
+}
+{
+  <SCHEMA> importFile = JsonString() ";"
+    {
+      try {
+        return Schema.parse(new File(inputDir, importFile));
+      } catch (IOException e) {
+        throw new ParseException("Error importing "+importFile+": "+e);
+      }        
+    }
+}
+
 Schema FixedDeclaration():
 {
   String name;

Modified: avro/trunk/lang/java/src/java/org/apache/avro/tool/IdlTool.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/tool/IdlTool.java?rev=986950&r1=986949&r2=986950&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/tool/IdlTool.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/tool/IdlTool.java Wed Aug 18 21:24:18 2010
@@ -21,7 +21,7 @@ package org.apache.avro.tool;
 import org.apache.avro.Protocol;
 import org.apache.avro.idl.Idl;
 
-import java.io.FileInputStream;
+import java.io.File;
 import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.PrintStream;
@@ -36,7 +36,6 @@ public class IdlTool implements Tool {
   public int run(InputStream in, PrintStream out, PrintStream err,
                   List<String> args) throws Exception {
 
-    InputStream parseIn = in;
     PrintStream parseOut = out;
 
     if (args.size() > 2 ||
@@ -52,15 +51,17 @@ public class IdlTool implements Tool {
       return -1;
     }
 
+    Idl parser;
     if (args.size() >= 1 && ! "-".equals(args.get(0))) {
-      parseIn = new FileInputStream(args.get(0));
+      parser = new Idl(new File(args.get(0)));
+    } else {
+      parser = new Idl(in);
     }
+    
     if (args.size() == 2 && ! "-".equals(args.get(1))) {
       parseOut = new PrintStream(new FileOutputStream(args.get(1)));
     }
 
-
-    Idl parser = new Idl(parseIn);
     Protocol p = parser.CompilationUnit();
     parseOut.print(p.toString(true));
     return 0;

Added: avro/trunk/lang/java/src/test/idl/input/bar.avpr
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/idl/input/bar.avpr?rev=986950&view=auto
==============================================================================
--- avro/trunk/lang/java/src/test/idl/input/bar.avpr (added)
+++ avro/trunk/lang/java/src/test/idl/input/bar.avpr Wed Aug 18 21:24:18 2010
@@ -0,0 +1,2 @@
+{"protocol": "org.foo.Bar",
+ "messages": { "bar": {"request": [], "response": "null"}}}

Added: avro/trunk/lang/java/src/test/idl/input/foo.avsc
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/idl/input/foo.avsc?rev=986950&view=auto
==============================================================================
--- avro/trunk/lang/java/src/test/idl/input/foo.avsc (added)
+++ avro/trunk/lang/java/src/test/idl/input/foo.avsc Wed Aug 18 21:24:18 2010
@@ -0,0 +1,3 @@
+{"type": "record", "name": "org.foo.Foo",
+ "fields": [ {"name": "x", "type": "int"} ]
+}

Added: avro/trunk/lang/java/src/test/idl/input/import.avdl
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/idl/input/import.avdl?rev=986950&view=auto
==============================================================================
--- avro/trunk/lang/java/src/test/idl/input/import.avdl (added)
+++ avro/trunk/lang/java/src/test/idl/input/import.avdl Wed Aug 18 21:24:18 2010
@@ -0,0 +1,30 @@
+/**
+ * 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.
+ */
+
+@namespace("org.foo")
+protocol Import {
+  import idl "reservedwords.avdl";
+  import schema "foo.avsc";
+  import protocol "bar.avpr";
+  
+  record Bar {
+    Foo foo;
+  }
+
+  Bar barf(Foo foo);
+}

Added: avro/trunk/lang/java/src/test/idl/output/import.avpr
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/idl/output/import.avpr?rev=986950&view=auto
==============================================================================
--- avro/trunk/lang/java/src/test/idl/output/import.avpr (added)
+++ avro/trunk/lang/java/src/test/idl/output/import.avpr Wed Aug 18 21:24:18 2010
@@ -0,0 +1,40 @@
+{
+  "protocol" : "Import",
+  "namespace" : "org.foo",
+  "types" : [ {
+    "type" : "record",
+    "name" : "Foo",
+    "fields" : [ {
+      "name" : "x",
+      "type" : "int"
+    } ]
+  }, {
+    "type" : "record",
+    "name" : "Bar",
+    "fields" : [ {
+      "name" : "foo",
+      "type" : "Foo"
+    } ]
+  } ],
+  "messages" : {
+    "error" : {
+      "request" : [ ],
+      "response" : "null"
+    },
+    "void" : {
+      "request" : [ ],
+      "response" : "null"
+    },
+    "bar" : {
+      "request" : [ ],
+      "response" : "null"
+    },
+    "barf" : {
+      "request" : [ {
+        "name" : "foo",
+        "type" : "Foo"
+      } ],
+      "response" : "Bar"
+    }
+  }
+}
\ No newline at end of file

Modified: avro/trunk/lang/java/src/test/java/org/apache/avro/idl/TestIdl.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/java/org/apache/avro/idl/TestIdl.java?rev=986950&r1=986949&r2=986950&view=diff
==============================================================================
--- avro/trunk/lang/java/src/test/java/org/apache/avro/idl/TestIdl.java (original)
+++ avro/trunk/lang/java/src/test/java/org/apache/avro/idl/TestIdl.java Wed Aug 18 21:24:18
2010
@@ -121,7 +121,7 @@ public class TestIdl {
     }
 
     private String generate() throws Exception {
-      Idl parser = new Idl(new FileInputStream(in), "UTF-8");
+      Idl parser = new Idl(in);
       Protocol p = parser.CompilationUnit();
       return p.toString(true);
     }



Mime
View raw message