You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by cu...@apache.org on 2010/07/01 22:17:27 UTC

svn commit: r959768 - in /avro/trunk: ./ doc/src/content/xdocs/ lang/java/src/java/org/apache/avro/idl/ lang/java/src/test/idl/input/ lang/java/src/test/idl/output/

Author: cutting
Date: Thu Jul  1 20:17:27 2010
New Revision: 959768

URL: http://svn.apache.org/viewvc?rev=959768&view=rev
Log:
AVRO-494.  Add support for default values to IDL.

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/test/idl/input/interop.avdl
    avro/trunk/lang/java/src/test/idl/input/simple.avdl
    avro/trunk/lang/java/src/test/idl/output/interop.avpr
    avro/trunk/lang/java/src/test/idl/output/simple.avpr

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=959768&r1=959767&r2=959768&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Thu Jul  1 20:17:27 2010
@@ -57,6 +57,8 @@ Avro 1.4.0 (unreleased)
     AVRO-150. Java: fix compiler to not re-generate up-to-date code.
     (John Yu via cutting)
 
+    AVRO-494. Add support for default values to IDL.  (cutting)
+
   BUG FIXES
 
     AVRO-502. Memory leak from parsing JSON schema.

Modified: avro/trunk/doc/src/content/xdocs/idl.xml
URL: http://svn.apache.org/viewvc/avro/trunk/doc/src/content/xdocs/idl.xml?rev=959768&r1=959767&r2=959768&view=diff
==============================================================================
--- avro/trunk/doc/src/content/xdocs/idl.xml (original)
+++ avro/trunk/doc/src/content/xdocs/idl.xml Thu Jul  1 20:17:27 2010
@@ -149,7 +149,7 @@ fixed MD5(16);
       <source>
 record Employee {
   string name;
-  boolean active;
+  boolean active = true;
   long salary;
 }
       </source>
@@ -163,11 +163,12 @@ record Employee {
       <source>
 error Kaboom {
   string explanation;
-  int result_code;
+  int result_code = -1;
 }
       </source>
       <p>
-        Each field in a record or error consists of a type and a name, along with optional property annotations.
+        Each field in a record or error consists of a type and a name,
+        optional property annotations and an optional default value.
       </p>
       <p>A type reference in Avro IDL must be one of:</p>
       <ul>
@@ -196,6 +197,16 @@ record Card {
 }
         </source>
       </section>
+      <section id="default_values">
+        <title>Default Values</title>
+
+	<p>Default values for fields may be optionally
+	specified by using an equals sign after the field name
+	followed by a JSON expression indicating the default value.
+	This JSON is interpreted as described in
+	the <a href="spec.html#schema_record">spec</a>.</p>
+
+      </section> <!-- default values -->
       <section id="complex_types">
         <title>Complex Types</title>
 
@@ -241,8 +252,10 @@ record RecordWithUnion {
       returning an <code>int</code>, simply include the following definition within the protocol:
       </p>
       <source>
-int add(int foo, int bar);
+int add(int foo, int bar = 0);
       </source>
+      <p>Message arguments, like record fields, may specify default
+      values.</p>
       <p>To define a message with no response, you may use the alias <code>void</code>, equivalent
       to the Avro <code>null</code> type:
       </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=959768&r1=959767&r2=959768&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 Thu Jul  1 20:17:27 2010
@@ -72,6 +72,9 @@ import org.apache.avro.Schema.*;
 import org.apache.avro.Protocol;
 import org.apache.avro.Protocol.*;
 
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.node.*;
+
 import org.apache.commons.lang.StringEscapeUtils;
 
 /**
@@ -82,6 +85,8 @@ import org.apache.commons.lang.StringEsc
  */
 public class Idl
 {
+  static JsonNodeFactory FACTORY = JsonNodeFactory.instance;
+
   String namespace;
   Map<String,Schema> names = new LinkedHashMap<String,Schema>();
 }
@@ -162,9 +167,11 @@ TOKEN :
 TOKEN :
 {
   < INTEGER_LITERAL:
-        <DECIMAL_LITERAL> (["l","L"])?
+  ("-")?
+    (   <DECIMAL_LITERAL> (["l","L"])?
       | <HEX_LITERAL> (["l","L"])?
       | <OCTAL_LITERAL> (["l","L"])?
+        )  
   >
 |
   < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
@@ -174,8 +181,8 @@ TOKEN :
   < #OCTAL_LITERAL: "0" (["0"-"7"])* >
 |
   < FLOATING_POINT_LITERAL:
-        <DECIMAL_FLOATING_POINT_LITERAL>
-      | <HEXADECIMAL_FLOATING_POINT_LITERAL>
+  ("-")?
+    ( <DECIMAL_FLOATING_POINT_LITERAL> | <HEXADECIMAL_FLOATING_POINT_LITERAL> )
   >
 |
   < #DECIMAL_FLOATING_POINT_LITERAL:
@@ -879,9 +886,13 @@ TOKEN :
 | < RPAREN: ")" >
 | < LBRACE: "{" >
 | < RBRACE: "}" >
+| < LBRACK: "[" >
+| < RBRACK: "]" >
+| < COLON:  ":" >
 | < SEMICOLON: ";" >
 | < COMMA: "," >
 | < AT: "@" >
+| < EQUALS: "=" >
 | < DOT: "." >
 }
 
@@ -1103,7 +1114,7 @@ void SchemaProperty(Map<String, String> 
   String val;
 }
 {
-  "@" key = Identifier() "(" val = Literal() ")"
+  "@" key = Identifier() "(" val = JsonString() ")"
   {
     if (properties.containsKey(key)) {
       throw new ParseException("Property '" + key + "' already specified " +
@@ -1138,11 +1149,15 @@ void FieldDeclaration(List<Field> fields
 void VariableDeclarator(Schema type, List<Field> fields):
 {
   String name;
+  JsonNode defaultValue = null;
 }
 {
   name = Identifier()
+
+    [ <EQUALS> defaultValue=Json() ]
+    
   {
-    fields.add(new Field(name, type, null, null));
+    fields.add(new Field(name, type, null, defaultValue));
   }
 }
 
@@ -1292,46 +1307,6 @@ Schema ResultType():
   | schema = Type() { return schema; }
 }
 
-
-String Literal():
-{ Token t; }
-{
-  (
-    t = <INTEGER_LITERAL>
-  | t = <FLOATING_POINT_LITERAL>
-  | t = <CHARACTER_LITERAL>
-  | t = BooleanLiteral()
-  | t = NullLiteral()
-  )
-  {
-    return t.image;
-  }
-| t = <STRING_LITERAL>
-  {
-    String betweenQuotes = t.image.substring(1, t.image.length() - 1);
-    return StringEscapeUtils.unescapeJavaScript(betweenQuotes);
-  }
-}
-
-Token BooleanLiteral():
-{ Token t; }
-{
-  (   t = "true"
-    | t = "false"
-  )
-  {
-    return t;
-  }
-}
-
-Token NullLiteral():
-{
-  Token t;
-}
-{
-  t = "null" { return t; }
-}
-
 String Identifier():
 {
   Token t;
@@ -1372,3 +1347,66 @@ Token AnyIdentifier():
     return t;
   }
 }
+
+JsonNode Json() :
+{ String s; Token t; JsonNode n; }
+{ 
+( s = JsonString() { n = new TextNode(s); }
+| (t=<INTEGER_LITERAL> { n = new LongNode(Long.parseLong(t.image)); })
+| (t=<FLOATING_POINT_LITERAL> {n=new DoubleNode(Double.parseDouble(t.image));})
+| n=JsonObject()
+| n=JsonArray()
+| ( "true" { n = BooleanNode.TRUE; } )
+| ( "false" { n = BooleanNode.FALSE; } )
+| ( "null" { n = NullNode.instance; } )
+ )
+  { return n; }
+}
+
+String JsonString() :
+{ Token t; }
+{
+  t = <STRING_LITERAL>
+  {
+    String betweenQuotes = t.image.substring(1, t.image.length() - 1);
+    return StringEscapeUtils.unescapeJavaScript(betweenQuotes);
+  }
+}
+
+JsonNode JsonObject() :
+{ 
+  ObjectNode o = FACTORY.objectNode();
+}
+{
+  "{" [ JsonFields(o) ] "}"
+  { return o; }
+}
+
+void JsonFields(ObjectNode o) :
+{}
+{
+  JsonPair(o) [ "," JsonFields(o) ]
+}
+
+void JsonPair(ObjectNode o) :
+{
+  String name;
+  JsonNode value;
+}
+{
+  name=JsonString() <COLON> value=Json()
+    { o.put(name, value); } 
+}
+
+JsonNode JsonArray() :
+{ ArrayNode a = FACTORY.arrayNode(); }
+{
+  <LBRACK> [ JsonElements(a) ] <RBRACK>
+    { return a; }
+}
+
+void JsonElements(ArrayNode a) :
+{ JsonNode element; }
+{
+  element=Json() { a.add(element); } [ "," JsonElements(a) ]
+}

Modified: avro/trunk/lang/java/src/test/idl/input/interop.avdl
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/idl/input/interop.avdl?rev=959768&r1=959767&r2=959768&view=diff
==============================================================================
--- avro/trunk/lang/java/src/test/idl/input/interop.avdl (original)
+++ avro/trunk/lang/java/src/test/idl/input/interop.avdl Thu Jul  1 20:17:27 2010
@@ -28,18 +28,18 @@ protocol InteropProtocol {
 
   record Node {
     string label;
-    array<Node> children;
+    array<Node> children = [];
   }
 
   record Interop {
-    int intField;
-    long longField;
+    int intField = 1;
+    long longField = -1;
     string stringField;
-    boolean boolField;
-    float floatField;
-    double doubleField;
+    boolean boolField = false;
+    float floatField = 0.0;
+    double doubleField = -1.0e12;
     null nullField;
-    array<double> arrayField;
+    array<double> arrayField = [{"label":"foo", "children":[]}];
     map<Foo> mapField;
     union { boolean, double, array<bytes> } unionFIeld;
     Kind enumField;

Modified: avro/trunk/lang/java/src/test/idl/input/simple.avdl
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/idl/input/simple.avdl?rev=959768&r1=959767&r2=959768&view=diff
==============================================================================
--- avro/trunk/lang/java/src/test/idl/input/simple.avdl (original)
+++ avro/trunk/lang/java/src/test/idl/input/simple.avdl Thu Jul  1 20:17:27 2010
@@ -31,7 +31,7 @@ protocol Simple {
 
   record TestRecord {
     @order("ignore")
-    string name;
+    string name = "foo";
 
     @order("descending")
     Kind kind;
@@ -46,8 +46,8 @@ protocol Simple {
   }
 
   string hello(string greeting);
-  TestRecord echo(TestRecord `record`);
-  int add(int arg1, int arg2);
+  TestRecord echo(TestRecord `record` = {"name": "bar"});
+  int add(int arg1, int arg2 = 0);
   bytes echoBytes(bytes data);
   void `error`() throws TestError;
 }

Modified: avro/trunk/lang/java/src/test/idl/output/interop.avpr
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/idl/output/interop.avpr?rev=959768&r1=959767&r2=959768&view=diff
==============================================================================
--- avro/trunk/lang/java/src/test/idl/output/interop.avpr (original)
+++ avro/trunk/lang/java/src/test/idl/output/interop.avpr Thu Jul  1 20:17:27 2010
@@ -27,29 +27,35 @@
       "type" : {
         "type" : "array",
         "items" : "Node"
-      }
+      },
+      "default" : [ ]
     } ]
   }, {
     "type" : "record",
     "name" : "Interop",
     "fields" : [ {
       "name" : "intField",
-      "type" : "int"
+      "type" : "int",
+      "default" : 1
     }, {
       "name" : "longField",
-      "type" : "long"
+      "type" : "long",
+      "default" : -1
     }, {
       "name" : "stringField",
       "type" : "string"
     }, {
       "name" : "boolField",
-      "type" : "boolean"
+      "type" : "boolean",
+      "default" : false
     }, {
       "name" : "floatField",
-      "type" : "float"
+      "type" : "float",
+      "default" : 0.0
     }, {
       "name" : "doubleField",
-      "type" : "double"
+      "type" : "double",
+      "default" : -1.0E12
     }, {
       "name" : "nullField",
       "type" : "null"
@@ -58,7 +64,11 @@
       "type" : {
         "type" : "array",
         "items" : "double"
-      }
+      },
+      "default" : [ {
+        "label" : "foo",
+        "children" : [ ]
+      } ]
     }, {
       "name" : "mapField",
       "type" : {
@@ -84,4 +94,4 @@
   } ],
   "messages" : {
   }
-}
\ No newline at end of file
+}

Modified: avro/trunk/lang/java/src/test/idl/output/simple.avpr
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/idl/output/simple.avpr?rev=959768&r1=959767&r2=959768&view=diff
==============================================================================
--- avro/trunk/lang/java/src/test/idl/output/simple.avpr (original)
+++ avro/trunk/lang/java/src/test/idl/output/simple.avpr Thu Jul  1 20:17:27 2010
@@ -18,7 +18,8 @@
       "type" : {
         "type" : "string",
         "order" : "ignore"
-      }
+      },
+      "default" : "foo"
     }, {
       "name" : "kind",
       "type" : "Kind"
@@ -48,7 +49,10 @@
     "echo" : {
       "request" : [ {
         "name" : "record",
-        "type" : "TestRecord"
+        "type" : "TestRecord",
+        "default" : {
+          "name" : "bar"
+        }
       } ],
       "response" : "TestRecord"
     },
@@ -58,7 +62,8 @@
         "type" : "int"
       }, {
         "name" : "arg2",
-        "type" : "int"
+        "type" : "int",
+        "default" : 0
       } ],
       "response" : "int"
     },