@Test public void testBasicCopy() throws Exception { String[] directives = new String[] { "parse-as-csv body ,", "copy body_1 name" }; List<Row> rows = Arrays.asList( new Row("body", "A,B,1"), new Row("body", "D,E,2"), new Row("body", "G,H,3") ); rows = TestingRig.execute(directives, rows); Assert.assertTrue(rows.size() == 3); Assert.assertEquals(5, rows.get(0).width()); Assert.assertEquals("A", rows.get(0).getValue("name")); Assert.assertEquals("D", rows.get(1).getValue("name")); Assert.assertEquals("G", rows.get(2).getValue("name")); Assert.assertEquals(rows.get(0).getValue("name"), rows.get(0).getValue("body_1")); Assert.assertEquals(rows.get(1).getValue("name"), rows.get(1).getValue("body_1")); Assert.assertEquals(rows.get(2).getValue("name"), rows.get(2).getValue("body_1")); }
@Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int sidx = row.find(source.value()); if (sidx == -1) { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", source.value())); } int didx = row.find(destination.value()); if (didx == sidx) { continue; } if (didx == -1) { row.add(destination.value(), row.getValue(sidx)); } else { if (!force) { throw new DirectiveExecutionException( NAME, String.format("Destination column '%s' already exists in the row. Use 'force' " + "option to overwrite the column.", destination.value())); } row.setValue(didx, row.getValue(sidx)); } } return rows; }
Copy implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int sidx = row.find(source.value()); if (sidx == -1) { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", source.value())); } int didx = row.find(destination.value()); if (didx == sidx) { continue; } if (didx == -1) { row.add(destination.value(), row.getValue(sidx)); } else { if (!force) { throw new DirectiveExecutionException( NAME, String.format("Destination column '%s' already exists in the row. Use 'force' " + "option to overwrite the column.", destination.value())); } row.setValue(didx, row.getValue(sidx)); } } return rows; } }
Copy implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int sidx = row.find(source.value()); if (sidx == -1) { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", source.value())); } int didx = row.find(destination.value()); if (didx == sidx) { continue; } if (didx == -1) { row.add(destination.value(), row.getValue(sidx)); } else { if (!force) { throw new DirectiveExecutionException( NAME, String.format("Destination column '%s' already exists in the row. Use 'force' " + "option to overwrite the column.", destination.value())); } row.setValue(didx, row.getValue(sidx)); } } return rows; }  }
Copy implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int sidx = row.find(source.value()); if (sidx == -1) { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", source.value())); } int didx = row.find(destination.value()); if (didx == sidx) { continue; } if (didx == -1) { row.add(destination.value(), row.getValue(sidx)); } else { if (!force) { throw new DirectiveExecutionException( NAME, String.format("Destination column '%s' already exists in the row. Use 'force' " + "option to overwrite the column.", destination.value())); } row.setValue(didx, row.getValue(sidx)); } } return rows; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override List<Row> execute(List<Row> rows, ExecutorContext context); @Override Mutation lineage(); }
Copy implements Directive, Lineage { @Override public List<Row> execute(List<Row> rows, ExecutorContext context) throws DirectiveExecutionException { for (Row row : rows) { int sidx = row.find(source.value()); if (sidx == -1) { throw new DirectiveExecutionException(NAME, String.format("Column '%s' does not exist.", source.value())); } int didx = row.find(destination.value()); if (didx == sidx) { continue; } if (didx == -1) { row.add(destination.value(), row.getValue(sidx)); } else { if (!force) { throw new DirectiveExecutionException( NAME, String.format("Destination column '%s' already exists in the row. Use 'force' " + "option to overwrite the column.", destination.value())); } row.setValue(didx, row.getValue(sidx)); } } return rows; }  @Override UsageDefinition define(); @Override void initialize(Arguments args); @Override void destroy(); @Override List<Row> execute(List<Row> rows, ExecutorContext context); @Override Mutation lineage(); static final String NAME; }