Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: .mapToBuilder() for data types with builders #2018

Open
azurvii opened this issue Apr 2, 2022 · 1 comment
Open

Feature request: .mapToBuilder() for data types with builders #2018

azurvii opened this issue Apr 2, 2022 · 1 comment
Labels

Comments

@azurvii
Copy link

azurvii commented Apr 2, 2022

.mapToBean() works wonders. However, this would require the bearing data class to be mutable. Protocol buffers, AutoValues, and other immutable data class helpers would be useless without additional mapping logic.

I feel .mapToBean() could have worked out of box for these cases (since the builder is a bean) by:

  • adding support for instance creation with their builders, instead of a default constructor.
  • returning an instance built from the builder, instead of itself (as in bean mapper's case).

Internally, the builders could use the BeanMapper's logic, i.e. with .setXxx() methods. The common build method I saw is .build(), which should be able to get reflectively called and return the built instance. Or, a user-defined Function to map the builder to the data instance can be used to allow customizations.

My proposal is to add sibling methods of .mapToBean() like these:

/**
 * Maps to a data type with its builder.
 *
 * <p>This would try to look for a `.build()` method in the builder to create the data instance.
 **/
<DataType, BuilderType> ResultIterable<DataType> mapToBuilder(Supplier<BuilderType> builderSupplier);

/**
 * Maps to a data type with its builder, with a provided `buildFunction` if the build method is not `build()`.
 **/
<DataType, BuilderType> ResultIterable<DataType> mapToBuilder(Supplier<BuilderType> builderSupplier, Function<BuilderType, DataType> buildFunction);

If Class<BuilderType> would be needed they could be added as well.

Example usage could be:

handle.createQuery("select * from my_table;").mapToBuilder(MyTypeWithBuilder::newBuilder).list();
handle.createQuery("select * from my_table;").mapToBuilder(MyTypeWithBuilder::newBuilder, MyTypeWithBuilder::buildInstance).list();

What do you say?

@hgschmie
Copy link
Contributor

Tracked in #2391, closing the issue here.

@hgschmie hgschmie reopened this Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants