avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r891000 - in /hadoop/avro/trunk: CHANGES.txt src/java/org/apache/avro/Schema.java src/test/java/org/apache/avro/TestSchema.java
Date Tue, 15 Dec 2009 21:29:39 GMT
Author: cutting
Date: Tue Dec 15 21:29:39 2009
New Revision: 891000

URL: http://svn.apache.org/viewvc?rev=891000&view=rev
Log:
AVRO-255.  Fix java implementation so that, when parsing schemas, unspecified namespaces are
defaulted to nearest enclosing namespace.

Modified:
    hadoop/avro/trunk/CHANGES.txt
    hadoop/avro/trunk/src/java/org/apache/avro/Schema.java
    hadoop/avro/trunk/src/test/java/org/apache/avro/TestSchema.java

Modified: hadoop/avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=891000&r1=890999&r2=891000&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Tue Dec 15 21:29:39 2009
@@ -196,6 +196,9 @@
     AVRO-256. Use fully-qualified class names in generated Java code
     to eliminate name conflicts. (cutting)
 
+    AVRO-255. Fix Java so that, when parsing schemas, unspecified
+    namespaces are defaulted to nearest enclosing namespace. (cutting)
+
 Avro 1.2.0 (14 October 2009)
 
   INCOMPATIBLE CHANGES

Modified: hadoop/avro/trunk/src/java/org/apache/avro/Schema.java
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/Schema.java?rev=891000&r1=890999&r2=891000&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/Schema.java (original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/Schema.java Tue Dec 15 21:29:39 2009
@@ -128,13 +128,13 @@
   /** Create a named record schema. */
   public static Schema createRecord(String name, String namespace,
                                     boolean isError) {
-     return new RecordSchema(name, namespace, isError);
+    return new RecordSchema(new Name(name, namespace), isError);
   }
 
   /** Create an enum schema. */
   public static Schema createEnum(String name, String namespace,
                                   List<String> values) {
-    return new EnumSchema(name, namespace, values);
+    return new EnumSchema(new Name(name, namespace), values);
   }
 
   /** Create an array schema. */
@@ -154,7 +154,7 @@
 
   /** Create a union schema. */
   public static Schema createFixed(String name, String space, int size) {
-    return new FixedSchema(name, space, size);
+    return new FixedSchema(new Name(name, space), size);
   }
 
   /** Return the type of this schema. */
@@ -358,9 +358,9 @@
 
   private static abstract class NamedSchema extends Schema {
     private final Name name;
-    public NamedSchema(Type type, String name, String space) {
+    public NamedSchema(Type type, Name name) {
       super(type);
-      this.name = new Name(name, space);
+      this.name = name;
     }
     public String getName() { return name.name; }
     public String getNamespace() { return name.space; }
@@ -412,8 +412,8 @@
     private Map<String,Field> fields;
     private Iterable<Map.Entry<String,Schema>> fieldSchemas;
     private final boolean isError;
-    public RecordSchema(String name, String space, boolean isError) {
-      super(Type.RECORD, name, space);
+    public RecordSchema(Name name, boolean isError) {
+      super(Type.RECORD, name);
       this.isError = isError;
     }
     public boolean isError() { return isError; }
@@ -495,8 +495,8 @@
   private static class EnumSchema extends NamedSchema {
     private final List<String> symbols;
     private final Map<String,Integer> ordinals;
-    public EnumSchema(String name, String space, List<String> symbols) {
-      super(Type.ENUM, name, space);
+    public EnumSchema(Name name, List<String> symbols) {
+      super(Type.ENUM, name);
       this.symbols = symbols;
       this.ordinals = new HashMap<String,Integer>();
       int i = 0;
@@ -624,8 +624,8 @@
 
   private static class FixedSchema extends NamedSchema {
     private final int size;
-    public FixedSchema(String name, String space, int size) {
-      super(Type.FIXED, name, space);
+    public FixedSchema(Name name, int size) {
+      super(Type.FIXED, name);
       if (size < 0)
         throw new IllegalArgumentException("Invalid fixed size: "+size);
       this.size = size;
@@ -777,21 +777,25 @@
     } else if (schema.isObject()) {
       Schema result;
       String type = getRequiredText(schema, "type", "No type");
-      String name = null, space = null;
+      Name name = null;
+      String savedSpace = null;
       if (type.equals("record") || type.equals("error")
           || type.equals("enum") || type.equals("fixed")) {
-        name = getRequiredText(schema, "name", "No name in schema");
-        space = getOptionalText(schema, "namespace");
+        String space = getOptionalText(schema, "namespace");
         if (space == null)
           space = names.space();
-        if (names.space() == null && space != null)
-          names.space(space);                     // set default namespace
+        name = new Name(getRequiredText(schema, "name", "No name in schema"),
+                        space);
+        if (name.space != null) {                 // set default namespace
+          savedSpace = names.space();
+          names.space(name.space);
+        }
       }
       if (PRIMITIVES.containsKey(type)) {         // primitive
         result = create(PRIMITIVES.get(type));
       } else if (type.equals("record") || type.equals("error")) { // record
         LinkedHashMap<String,Field> fields = new LinkedHashMap<String,Field>();
-        result = new RecordSchema(name, space, type.equals("error"));
+        result = new RecordSchema(name, type.equals("error"));
         if (name != null) names.add(result);
         JsonNode fieldsNode = schema.get("fields");
         if (fieldsNode == null || !fieldsNode.isArray())
@@ -819,7 +823,7 @@
         List<String> symbols = new ArrayList<String>();
         for (JsonNode n : symbolsNode)
           symbols.add(n.getTextValue());
-        result = new EnumSchema(name, space, symbols);
+        result = new EnumSchema(name, symbols);
         if (name != null) names.add(result);
       } else if (type.equals("array")) {          // array
         JsonNode itemsNode = schema.get("items");
@@ -835,7 +839,7 @@
         JsonNode sizeNode = schema.get("size");
         if (sizeNode == null || !sizeNode.isInt())
           throw new SchemaParseException("Invalid or no size: "+schema);
-        result = new FixedSchema(name, space, sizeNode.getIntValue());
+        result = new FixedSchema(name, sizeNode.getIntValue());
         if (name != null) names.add(result);
       } else
         throw new SchemaParseException("Type not supported: "+type);
@@ -845,6 +849,8 @@
         if (!RESERVED_PROPS.contains(prop))       // ignore reserved
           result.setProp(prop, schema.get(prop).getTextValue());
       }
+      if (savedSpace != null)
+        names.space(savedSpace);                  // restore space
       return result;
     } else if (schema.isArray()) {                // union
       List<Schema> types = new ArrayList<Schema>(schema.size());

Modified: hadoop/avro/trunk/src/test/java/org/apache/avro/TestSchema.java
URL: http://svn.apache.org/viewvc/hadoop/avro/trunk/src/test/java/org/apache/avro/TestSchema.java?rev=891000&r1=890999&r2=891000&view=diff
==============================================================================
--- hadoop/avro/trunk/src/test/java/org/apache/avro/TestSchema.java (original)
+++ hadoop/avro/trunk/src/test/java/org/apache/avro/TestSchema.java Tue Dec 15 21:29:39 2009
@@ -206,6 +206,21 @@
     assertEquals(Schema.parse("\"boolean\""), s);
   }
 
+  @Test
+  public void testNamespaceScope() throws Exception {
+    String z = "{\"type\":\"record\",\"name\":\"Z\",\"fields\":[]}";
+    String y = "{\"type\":\"record\",\"name\":\"q.Y\",\"fields\":["
+      +"{\"name\":\"f\",\"type\":"+z+"}]}";
+    String x = "{\"type\":\"record\",\"name\":\"p.X\",\"fields\":["
+      +"{\"name\":\"f\",\"type\":"+y+"},"
+      +"{\"name\":\"g\",\"type\":"+z+"}"
+      +"]}";
+    Schema xs = Schema.parse(x);
+    Schema ys = xs.getFields().get("f").schema();
+    assertEquals("p.Z", xs.getFields().get("g").schema().getFullName());
+    assertEquals("q.Z", ys.getFields().get("f").schema().getFullName());
+  }
+
   private static void checkParseError(String json) {
     try {
       Schema schema = Schema.parse(json);



Mime
View raw message