AVRO-2649 Made argument order non-enforceable avro-tools cli (#741)
* AVRO-2649 Made argument order non-enforceable avro-tools cli
* AVRO-2649 Dynamic index value for encoding and templatedir
* AVRO-2649 Add positionindex to save positions of flags
* AVRO-2649 Add test cases
Co-authored-by: Daniel Kulp <dkulp@apache.org>
diff --git a/lang/java/tools/src/main/java/org/apache/avro/tool/SpecificCompilerTool.java b/lang/java/tools/src/main/java/org/apache/avro/tool/SpecificCompilerTool.java
index 550294e..e50af59 100644
--- a/lang/java/tools/src/main/java/org/apache/avro/tool/SpecificCompilerTool.java
+++ b/lang/java/tools/src/main/java/org/apache/avro/tool/SpecificCompilerTool.java
@@ -68,16 +68,17 @@
Optional<FieldVisibility> fieldVisibility = Optional.empty();
int arg = 0;
+ int counter = 0;
- if ("-encoding".equals(args.get(arg))) {
- arg++;
+ if (args.contains("-encoding")) {
+ arg = args.indexOf("-encoding") + 1;
encoding = Optional.of(args.get(arg));
- arg++;
+ counter = counter + 2;
}
- if ("-string".equals(args.get(arg))) {
+ if (args.contains("-string")) {
stringType = StringType.String;
- arg++;
+ counter = counter + 1;
}
if ("-fieldVisibility".equals(args.get(arg))) {
@@ -93,15 +94,15 @@
if ("-bigDecimal".equalsIgnoreCase(args.get(arg))) {
useLogicalDecimal = true;
- arg++;
+ counter = counter + 1;
}
- if ("-templateDir".equals(args.get(arg))) {
- arg++;
+ if (args.contains("-templateDir")) {
+ arg = args.indexOf("-templateDir") + 1;
templateDir = Optional.of(args.get(arg));
- arg++;
+ counter = counter + 2;
}
-
+ arg = counter;
String method = args.get(arg);
List<File> inputs = new ArrayList<>();
File output = new File(args.get(args.size() - 1));
diff --git a/lang/java/tools/src/test/java/org/apache/avro/tool/TestSpecificCompilerTool.java b/lang/java/tools/src/test/java/org/apache/avro/tool/TestSpecificCompilerTool.java
index c511d66..b6c0070 100644
--- a/lang/java/tools/src/test/java/org/apache/avro/tool/TestSpecificCompilerTool.java
+++ b/lang/java/tools/src/test/java/org/apache/avro/tool/TestSpecificCompilerTool.java
@@ -126,6 +126,34 @@
assertFileMatch(TEST_EXPECTED_STRING_FIELDTEST, TEST_OUTPUT_STRING_FIELDTEST);
}
+ @Test
+ public void testOrderingOfFlags() throws Exception {
+
+ // Order of Flags as per initial implementation
+ doCompile(new String[] { "-encoding", "UTF-8", "-string", "-bigDecimal", "schema",
+ TEST_INPUT_DIR.toString() + "/fieldtest.avsc", TEST_INPUT_DIR.toString() + "/fieldtest.avsc",
+ TEST_OUTPUT_STRING_DIR.getPath() });
+ assertFileMatch(TEST_EXPECTED_STRING_FIELDTEST, TEST_OUTPUT_STRING_FIELDTEST);
+
+ // Change order of encoding and string
+ doCompile(new String[] { "-string", "-encoding", "UTF-8", "-bigDecimal", "schema",
+ TEST_INPUT_DIR.toString() + "/fieldtest.avsc", TEST_INPUT_DIR.toString() + "/fieldtest.avsc",
+ TEST_OUTPUT_STRING_DIR.getPath() });
+ assertFileMatch(TEST_EXPECTED_STRING_FIELDTEST, TEST_OUTPUT_STRING_FIELDTEST);
+
+ // Change order of -string and -bigDecimal
+ doCompile(new String[] { "-bigDecimal", "-encoding", "UTF-8", "-string", "schema",
+ TEST_INPUT_DIR.toString() + "/fieldtest.avsc", TEST_INPUT_DIR.toString() + "/fieldtest.avsc",
+ TEST_OUTPUT_STRING_DIR.getPath() });
+ assertFileMatch(TEST_EXPECTED_STRING_FIELDTEST, TEST_OUTPUT_STRING_FIELDTEST);
+
+ // Keep encoding at the end
+ doCompile(new String[] { "-bigDecimal", "-string", "-encoding", "UTF-8", "schema",
+ TEST_INPUT_DIR.toString() + "/fieldtest.avsc", TEST_INPUT_DIR.toString() + "/fieldtest.avsc",
+ TEST_OUTPUT_STRING_DIR.getPath() });
+ assertFileMatch(TEST_EXPECTED_STRING_FIELDTEST, TEST_OUTPUT_STRING_FIELDTEST);
+ }
+
// Runs the actual compiler tool with the given input args
private void doCompile(String[] args) throws Exception {
SpecificCompilerTool tool = new SpecificCompilerTool();