quux00 / simplecsv

CSV parser for Java, based on the OpenCSV model, but fixing many of the inconsistencies and defects of that codebase.
Other
11 stars 3 forks source link

ColumnPositionMappingStrategy: need to set type manually #7

Closed kryger closed 10 years ago

kryger commented 10 years ago

As mentioned in #6 it took me some time to get BeanToCsv writer to work, only after analysing in the debugger I discovered that type variable inside HeaderColumnNameMappingStrategy (code) is not being set, I had to do it manually:

BeanToCsv<Institution> beanWriter = new BeanToCsv<>();
try (Writer printWriter = new PrintWriter(System.out)) {
    ColumnPositionMappingStrategy<Institution> strategy = new ColumnPositionMappingStrategy<>();
    strategy.setType(Institution.class); // the surprising line
    strategy.setColumnMapping(new String[] { "name", "email", "url" });
    beanWriter.write(strategy, printWriter, institutions);
}

This feels redundant, would be nice if the strategy object set it automatically upon construction.

quux00 commented 10 years ago

Sounds good. Usually in Java that would be solved via the constructor, like so:

ColumnPositionMappingStrategy<Institution> strategy = 
  new ColumnPositionMappingStrategy<Institution>(Institution.class);

so I think that would be a good addition.

quux00 commented 10 years ago

Just pushed a commit to add this constructor to all the MappingStrategies. Added new tests as well. Let me know if this works for you.

kryger commented 10 years ago

Thanks for quick reaction, will give it a try when I get a chance. Closing this issue to keep things clean, will reopen if needed.