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 2013/02/14 22:43:06 UTC

svn commit: r1446349 - in /avro/trunk: ./ lang/java/tools/src/main/java/org/apache/avro/tool/ lang/java/tools/src/test/compiler/output-string/ lang/java/tools/src/test/compiler/output-string/avro/ lang/java/tools/src/test/compiler/output-string/avro/ex...

Author: cutting
Date: Thu Feb 14 21:43:05 2013
New Revision: 1446349

URL: http://svn.apache.org/r1446349
Log:
AVRO-1252. Java: Add a '-string' option to compiler command line.

Added:
    avro/trunk/lang/java/tools/src/test/compiler/output-string/
    avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/
    avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/
    avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/
    avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java   (with props)
    avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Position.java   (with props)
Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/SpecificCompilerTool.java
    avro/trunk/lang/java/tools/src/test/java/org/apache/avro/tool/TestSpecificCompilerTool.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1446349&r1=1446348&r2=1446349&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Thu Feb 14 21:43:05 2013
@@ -41,6 +41,9 @@ Trunk (not yet released)
 
     AVRO-1198. Java: Improve error message for malformed data. (cutting)
 
+    AVRO-1252. Java: Add a '-string' option to compiler command line
+    to cause java.lang.String to be used instead of Utf8. (cutting)
+
   BUG FIXES
 
     AVRO-1231. Java: Fix Trevni shredder to work on non-recursive

Modified: avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/SpecificCompilerTool.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/SpecificCompilerTool.java?rev=1446349&r1=1446348&r2=1446349&view=diff
==============================================================================
--- avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/SpecificCompilerTool.java (original)
+++ avro/trunk/lang/java/tools/src/main/java/org/apache/avro/tool/SpecificCompilerTool.java Thu Feb 14 21:43:05 2013
@@ -26,6 +26,9 @@ import java.util.Set;
 import java.util.LinkedHashSet;
 import java.util.List;
 
+import org.apache.avro.Protocol;
+import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericData.StringType;
 import org.apache.avro.compiler.specific.SpecificCompiler;
 
 /**
@@ -39,25 +42,46 @@ public class SpecificCompilerTool implem
       List<String> args) throws Exception {
     if (args.size() < 3) {
       System.err
-          .println("Need at least 3 arguments: (schema|protocol) input... outputdir");
+          .println("Usage: [-string] (schema|protocol) input... outputdir");
       System.err
           .println(" input - input files or directories");
       System.err
           .println(" outputdir - directory to write generated java");
+      System.err.println(" -string - use java.lang.String instead of Utf8");
       return 1;
     }
-    String method = args.get(0);
+
+    StringType stringType = StringType.CharSequence;
+
+    int arg = 0;
+    if ("-string".equals(args.get(arg))) {
+      stringType = StringType.String;
+      arg++;
+    }
+      
+    String method = args.get(arg);
     List<File> inputs = new ArrayList<File>();
     File output = new File(args.get(args.size() - 1));
 
-    for (int i = 1; i < args.size() - 1; i++) {
+    for (int i = arg+1; i < args.size() - 1; i++) {
       inputs.add(new File(args.get(i)));
     }
 
     if ("schema".equals(method)) {
-      SpecificCompiler.compileSchema(determineInputs(inputs, SCHEMA_FILTER), output);
+      Schema.Parser parser = new Schema.Parser();
+      for (File src : determineInputs(inputs, SCHEMA_FILTER)) {
+        Schema schema = parser.parse(src);
+        SpecificCompiler compiler = new SpecificCompiler(schema);
+        compiler.setStringType(stringType);
+        compiler.compileToDestination(src, output);
+      }
     } else if ("protocol".equals(method)) {
-      SpecificCompiler.compileProtocol(determineInputs(inputs, PROTOCOL_FILTER), output);
+      for (File src : determineInputs(inputs, PROTOCOL_FILTER)) {
+        Protocol protocol = Protocol.parse(src);
+        SpecificCompiler compiler = new SpecificCompiler(protocol);
+        compiler.setStringType(stringType);
+        compiler.compileToDestination(src, output);
+      }
     } else {
       System.err.println("Expected \"schema\" or \"protocol\".");
       return 1;

Added: avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java?rev=1446349&view=auto
==============================================================================
--- avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java (added)
+++ avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java Thu Feb 14 21:43:05 2013
@@ -0,0 +1,285 @@
+/**
+ * Autogenerated by Avro
+ * 
+ * DO NOT EDIT DIRECTLY
+ */
+package avro.examples.baseball;  
+@SuppressWarnings("all")
+@org.apache.avro.specific.AvroGenerated
+public class Player extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
+  public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"Player\",\"namespace\":\"avro.examples.baseball\",\"fields\":[{\"name\":\"number\",\"type\":\"int\"},{\"name\":\"first_name\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}},{\"name\":\"last_name\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}},{\"name\":\"position\",\"type\":{\"type\":\"array\",\"items\":{\"type\":\"enum\",\"name\":\"Position\",\"symbols\":[\"P\",\"C\",\"B1\",\"B2\",\"B3\",\"SS\",\"LF\",\"CF\",\"RF\",\"DH\"]}}}]}");
+  public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
+  @Deprecated public int number;
+  @Deprecated public java.lang.String first_name;
+  @Deprecated public java.lang.String last_name;
+  @Deprecated public java.util.List<avro.examples.baseball.Position> position;
+
+  /**
+   * Default constructor.
+   */
+  public Player() {}
+
+  /**
+   * All-args constructor.
+   */
+  public Player(java.lang.Integer number, java.lang.String first_name, java.lang.String last_name, java.util.List<avro.examples.baseball.Position> position) {
+    this.number = number;
+    this.first_name = first_name;
+    this.last_name = last_name;
+    this.position = position;
+  }
+
+  public org.apache.avro.Schema getSchema() { return SCHEMA$; }
+  // Used by DatumWriter.  Applications should not call. 
+  public java.lang.Object get(int field$) {
+    switch (field$) {
+    case 0: return number;
+    case 1: return first_name;
+    case 2: return last_name;
+    case 3: return position;
+    default: throw new org.apache.avro.AvroRuntimeException("Bad index");
+    }
+  }
+  // Used by DatumReader.  Applications should not call. 
+  @SuppressWarnings(value="unchecked")
+  public void put(int field$, java.lang.Object value$) {
+    switch (field$) {
+    case 0: number = (java.lang.Integer)value$; break;
+    case 1: first_name = (java.lang.String)value$; break;
+    case 2: last_name = (java.lang.String)value$; break;
+    case 3: position = (java.util.List<avro.examples.baseball.Position>)value$; break;
+    default: throw new org.apache.avro.AvroRuntimeException("Bad index");
+    }
+  }
+
+  /**
+   * Gets the value of the 'number' field.
+   */
+  public java.lang.Integer getNumber() {
+    return number;
+  }
+
+  /**
+   * Sets the value of the 'number' field.
+   * @param value the value to set.
+   */
+  public void setNumber(java.lang.Integer value) {
+    this.number = value;
+  }
+
+  /**
+   * Gets the value of the 'first_name' field.
+   */
+  public java.lang.String getFirstName() {
+    return first_name;
+  }
+
+  /**
+   * Sets the value of the 'first_name' field.
+   * @param value the value to set.
+   */
+  public void setFirstName(java.lang.String value) {
+    this.first_name = value;
+  }
+
+  /**
+   * Gets the value of the 'last_name' field.
+   */
+  public java.lang.String getLastName() {
+    return last_name;
+  }
+
+  /**
+   * Sets the value of the 'last_name' field.
+   * @param value the value to set.
+   */
+  public void setLastName(java.lang.String value) {
+    this.last_name = value;
+  }
+
+  /**
+   * Gets the value of the 'position' field.
+   */
+  public java.util.List<avro.examples.baseball.Position> getPosition() {
+    return position;
+  }
+
+  /**
+   * Sets the value of the 'position' field.
+   * @param value the value to set.
+   */
+  public void setPosition(java.util.List<avro.examples.baseball.Position> value) {
+    this.position = value;
+  }
+
+  /** Creates a new Player RecordBuilder */
+  public static avro.examples.baseball.Player.Builder newBuilder() {
+    return new avro.examples.baseball.Player.Builder();
+  }
+  
+  /** Creates a new Player RecordBuilder by copying an existing Builder */
+  public static avro.examples.baseball.Player.Builder newBuilder(avro.examples.baseball.Player.Builder other) {
+    return new avro.examples.baseball.Player.Builder(other);
+  }
+  
+  /** Creates a new Player RecordBuilder by copying an existing Player instance */
+  public static avro.examples.baseball.Player.Builder newBuilder(avro.examples.baseball.Player other) {
+    return new avro.examples.baseball.Player.Builder(other);
+  }
+  
+  /**
+   * RecordBuilder for Player instances.
+   */
+  public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<Player>
+    implements org.apache.avro.data.RecordBuilder<Player> {
+
+    private int number;
+    private java.lang.String first_name;
+    private java.lang.String last_name;
+    private java.util.List<avro.examples.baseball.Position> position;
+
+    /** Creates a new Builder */
+    private Builder() {
+      super(avro.examples.baseball.Player.SCHEMA$);
+    }
+    
+    /** Creates a Builder by copying an existing Builder */
+    private Builder(avro.examples.baseball.Player.Builder other) {
+      super(other);
+    }
+    
+    /** Creates a Builder by copying an existing Player instance */
+    private Builder(avro.examples.baseball.Player other) {
+            super(avro.examples.baseball.Player.SCHEMA$);
+      if (isValidValue(fields()[0], other.number)) {
+        this.number = data().deepCopy(fields()[0].schema(), other.number);
+        fieldSetFlags()[0] = true;
+      }
+      if (isValidValue(fields()[1], other.first_name)) {
+        this.first_name = data().deepCopy(fields()[1].schema(), other.first_name);
+        fieldSetFlags()[1] = true;
+      }
+      if (isValidValue(fields()[2], other.last_name)) {
+        this.last_name = data().deepCopy(fields()[2].schema(), other.last_name);
+        fieldSetFlags()[2] = true;
+      }
+      if (isValidValue(fields()[3], other.position)) {
+        this.position = data().deepCopy(fields()[3].schema(), other.position);
+        fieldSetFlags()[3] = true;
+      }
+    }
+
+    /** Gets the value of the 'number' field */
+    public java.lang.Integer getNumber() {
+      return number;
+    }
+    
+    /** Sets the value of the 'number' field */
+    public avro.examples.baseball.Player.Builder setNumber(int value) {
+      validate(fields()[0], value);
+      this.number = value;
+      fieldSetFlags()[0] = true;
+      return this; 
+    }
+    
+    /** Checks whether the 'number' field has been set */
+    public boolean hasNumber() {
+      return fieldSetFlags()[0];
+    }
+    
+    /** Clears the value of the 'number' field */
+    public avro.examples.baseball.Player.Builder clearNumber() {
+      fieldSetFlags()[0] = false;
+      return this;
+    }
+
+    /** Gets the value of the 'first_name' field */
+    public java.lang.String getFirstName() {
+      return first_name;
+    }
+    
+    /** Sets the value of the 'first_name' field */
+    public avro.examples.baseball.Player.Builder setFirstName(java.lang.String value) {
+      validate(fields()[1], value);
+      this.first_name = value;
+      fieldSetFlags()[1] = true;
+      return this; 
+    }
+    
+    /** Checks whether the 'first_name' field has been set */
+    public boolean hasFirstName() {
+      return fieldSetFlags()[1];
+    }
+    
+    /** Clears the value of the 'first_name' field */
+    public avro.examples.baseball.Player.Builder clearFirstName() {
+      first_name = null;
+      fieldSetFlags()[1] = false;
+      return this;
+    }
+
+    /** Gets the value of the 'last_name' field */
+    public java.lang.String getLastName() {
+      return last_name;
+    }
+    
+    /** Sets the value of the 'last_name' field */
+    public avro.examples.baseball.Player.Builder setLastName(java.lang.String value) {
+      validate(fields()[2], value);
+      this.last_name = value;
+      fieldSetFlags()[2] = true;
+      return this; 
+    }
+    
+    /** Checks whether the 'last_name' field has been set */
+    public boolean hasLastName() {
+      return fieldSetFlags()[2];
+    }
+    
+    /** Clears the value of the 'last_name' field */
+    public avro.examples.baseball.Player.Builder clearLastName() {
+      last_name = null;
+      fieldSetFlags()[2] = false;
+      return this;
+    }
+
+    /** Gets the value of the 'position' field */
+    public java.util.List<avro.examples.baseball.Position> getPosition() {
+      return position;
+    }
+    
+    /** Sets the value of the 'position' field */
+    public avro.examples.baseball.Player.Builder setPosition(java.util.List<avro.examples.baseball.Position> value) {
+      validate(fields()[3], value);
+      this.position = value;
+      fieldSetFlags()[3] = true;
+      return this; 
+    }
+    
+    /** Checks whether the 'position' field has been set */
+    public boolean hasPosition() {
+      return fieldSetFlags()[3];
+    }
+    
+    /** Clears the value of the 'position' field */
+    public avro.examples.baseball.Player.Builder clearPosition() {
+      position = null;
+      fieldSetFlags()[3] = false;
+      return this;
+    }
+
+    @Override
+    public Player build() {
+      try {
+        Player record = new Player();
+        record.number = fieldSetFlags()[0] ? this.number : (java.lang.Integer) defaultValue(fields()[0]);
+        record.first_name = fieldSetFlags()[1] ? this.first_name : (java.lang.String) defaultValue(fields()[1]);
+        record.last_name = fieldSetFlags()[2] ? this.last_name : (java.lang.String) defaultValue(fields()[2]);
+        record.position = fieldSetFlags()[3] ? this.position : (java.util.List<avro.examples.baseball.Position>) defaultValue(fields()[3]);
+        return record;
+      } catch (Exception e) {
+        throw new org.apache.avro.AvroRuntimeException(e);
+      }
+    }
+  }
+}

Propchange: avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Position.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Position.java?rev=1446349&view=auto
==============================================================================
--- avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Position.java (added)
+++ avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Position.java Thu Feb 14 21:43:05 2013
@@ -0,0 +1,13 @@
+/**
+ * Autogenerated by Avro
+ * 
+ * DO NOT EDIT DIRECTLY
+ */
+package avro.examples.baseball;  
+@SuppressWarnings("all")
+@org.apache.avro.specific.AvroGenerated
+public enum Position { 
+  P, C, B1, B2, B3, SS, LF, CF, RF, DH  ;
+  public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"enum\",\"name\":\"Position\",\"namespace\":\"avro.examples.baseball\",\"symbols\":[\"P\",\"C\",\"B1\",\"B2\",\"B3\",\"SS\",\"LF\",\"CF\",\"RF\",\"DH\"]}");
+  public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
+}

Propchange: avro/trunk/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Position.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: avro/trunk/lang/java/tools/src/test/java/org/apache/avro/tool/TestSpecificCompilerTool.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/tools/src/test/java/org/apache/avro/tool/TestSpecificCompilerTool.java?rev=1446349&r1=1446348&r2=1446349&view=diff
==============================================================================
--- avro/trunk/lang/java/tools/src/test/java/org/apache/avro/tool/TestSpecificCompilerTool.java (original)
+++ avro/trunk/lang/java/tools/src/test/java/org/apache/avro/tool/TestSpecificCompilerTool.java Thu Feb 14 21:43:05 2013
@@ -56,6 +56,13 @@ public class TestSpecificCompilerTool {
   private static final File TEST_OUTPUT_POSITION =
     new File(TEST_OUTPUT_DIR, "avro/examples/baseball/Position.java");
 
+  private static final File TEST_OUTPUT_STRING_DIR =
+    new File("target/compiler/output-string");
+  private static final File TEST_OUTPUT_STRING_PLAYER =
+    new File(TEST_OUTPUT_DIR, "avro/examples/baseball/Player.java");
+  private static final File TEST_OUTPUT_STRING_POSITION =
+    new File(TEST_OUTPUT_DIR, "avro/examples/baseball/Position.java");
+
   @Before
   public void setUp() {
     TEST_OUTPUT_DIR.delete();
@@ -92,6 +99,17 @@ public class TestSpecificCompilerTool {
     assertFileMatch(TEST_EXPECTED_PLAYER,   TEST_OUTPUT_PLAYER);
   }
 
+  @Test
+  public void testCompileSchemasUsingString() throws Exception {
+
+    doCompile(new String[]{"-string", "schema",
+      TEST_INPUT_DIR.toString() + "/position.avsc",
+      TEST_INPUT_DIR.toString() + "/player.avsc",
+      TEST_OUTPUT_DIR.getPath()});
+    assertFileMatch(TEST_EXPECTED_POSITION, TEST_OUTPUT_STRING_POSITION);
+    assertFileMatch(TEST_EXPECTED_PLAYER,   TEST_OUTPUT_STRING_PLAYER);
+  }
+
   // Runs the actual compiler tool with the given input args
   private void doCompile(String[] args) throws Exception {
     SpecificCompilerTool tool = new SpecificCompilerTool();