Menu ▾ ▴

#140 Multiple columns for CsvBindByName annotation

closed
None
5
2021-03-16
2020-10-25
Mark Green
No

Hello,

Would it be possible to change "column" from String to String[] in the CsvBindByName annotation?
During processing first matching column name from the list would bind to POJO field.

Discussion

  • Andrew Rucker Jones

    Without breaking backward compatibility, which we will not consider until 6.0? No. I don't think I see a way of doing this.

    I would be interested in your use case, though. It's an odd request, and a bit intriguing.

     
  • Mark Green

    Mark Green - 2020-10-25

    To be honest I don't think it would break backward compatibility as you can define String[] as

    column="value"
    column={"value"} 
    column={"value", "value2"}
    

    For my use case I gather data from multiple CSV files with different header names. Currently I have to map them into multiple beans. The data has the same bussines meaning under different headers depending on source.

     

    Last edit: Mark Green 2020-10-25
  • Andrew Rucker Jones

    Then I will cogitate on it for a while.

     
  • Andrew Rucker Jones

    What we both forgot about was writing. If we have a bean like this with multiple options for a header name and the user wants to write the bean out to a CSV file, which of the possible header names should opencsv choose?

     
  • Mark Green

    Mark Green - 2020-10-26

    I would use the first one for simplicity.

    Another approach can be with repeating annotations(since Java 8 is minimum supported version) and adding something like profiles/strategies:

    @CsvBindByName(column="column1", profile="profile1")
    @CsvBindByName(column="column2", profile="profile2")
    private String field;
    

    and during building reader or writer user can tell which profile/strategy is used:

    .withProfileName("profile1")
    

    What do you think?

     
    • Andrew Rucker Jones

      At first I hated your profile idea. Then I thought about it a few minutes, and now I'm in love with it. Profiles would be a wonderful feature. I suspect many users would find a use for them, and they're far more general than your original suggestion. I think we have a winner!

       
  • Mark Green

    Mark Green - 2020-10-26

    Yeah, they would allow different configurations for other goodies. I'm glad you like it :)

     
  • Andrew Rucker Jones

    • status: open --> pending
    • assigned_to: Andrew Rucker Jones
     
  • Andrew Rucker Jones

    The profile feature will be released with the next version of opencsv.

     
  • Andrew Rucker Jones

    • status: pending --> closed
     
  • Andrew Rucker Jones

    5.4 was released a couple of days ago. Sorry to leave you hanging like that.

     

Log in to post a comment.