Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Add methods for appending columns of a CsvSchema into another #142

Closed
sharpau opened this issue Feb 2, 2017 · 2 comments
Closed

Add methods for appending columns of a CsvSchema into another #142

sharpau opened this issue Feb 2, 2017 · 2 comments

Comments

@sharpau
Copy link

sharpau commented Feb 2, 2017

Currently if I am building a CsvSchema with columns from a POJO and some other custom columns, I have to first build a CsvSchema from the POJO, and then provide the columns from that schema to a second CsvSchema.Builder. It's not a huge deal, but it seems like it'd be a nice improvement.

Current situation:

        CsvMapper mapper = new CsvMapper();
        CsvSchema pojoSchema = mapper.schemaFor(Pojo.class);

        CsvSchema fullSchema = CsvSchema.builder()
                .addColumns(pojoSchema)
                .addColumns(otherProperties, CsvSchema.ColumnType.STRING)
                .build().withHeader().withNullValue("");

Desired situation:

        CsvMapper mapper = new CsvMapper();

        CsvSchema fullSchema = CsvSchema.builder()
                .addColumnsFromPojo(Pojo.class)
                .addColumns(otherProperties, CsvSchema.ColumnType.STRING)
                .build().withHeader().withNullValue("");
@cowtowncoder
Copy link
Member

cowtowncoder commented Feb 4, 2017

While I understand why it would be convenient, I am not sure it is feasible because actual schema generation requires knowledge that only ObjectMapper has (and/or perhaps ObjectReader or ObjectWriter) -- basically information equivalent to knowing how to serialize a POJO.

It might be possible to add ObjectMapper argument.
Or add specific method in CsvMapper to make this more convenient... like, give CsvSchema.Builder initialized with columns for given POJO? That might be the way to go.

@cowtowncoder cowtowncoder changed the title CsvSchema.Builder should take a POJO Add methods for appending columns of a CsvSchema into another Mar 16, 2017
@cowtowncoder cowtowncoder added this to the 2.9.0.pr2 milestone Mar 16, 2017
@cowtowncoder
Copy link
Member

Added CsvSchema.withColumnsFrom(CsvSchema) and CsvSchema.Builder.addColumnsFrom(CsvSchema) methods which should help here.
It is unfortunately not possible to pass just type (Class, JavaType) since these objects do not have proper execution context (ObjectMapper or ObjectWriter) for introspection; but I think this should be helpful enough considering that mapper/writer is needed for initial introspection anyway.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants