Skip to content

Conversation

fredyw
Copy link

@fredyw fredyw commented Apr 9, 2025

When the record components are annotated with @RecordBuilder.Required, the builder method will take the required record components as its parameters. This is particularly useful to ensure that the builder is always populated with the required fields at compile-time instead of relying on a runtime validation.

Example:

public record MyRecord(@Required int a, @Required int b, int c)

@Generated("io.soabase.recordbuilder.core.RecordBuilder")
public class MyRecordBuilder {
    ...
    @Generated("io.soabase.recordbuilder.core.RecordBuilder")
    public static MyRecordBuilder builder(int a, int b) {
        return new MyRecordBuilder().a(a).b(b);
    }
    ...
}

When the record components are annotated with `@RecordBuilder.Required`,
the builder method will take the required record components as its
parameters. This is particularly useful to ensure that the builder is
always populated with the required fields at compile-time instead of
relying on a runtime validation.

Example:
```
public record MyRecord(@required int a, @required int b, int c)

@generated("io.soabase.recordbuilder.core.RecordBuilder")
public class MyRecordBuilder {
    ...
    @generated("io.soabase.recordbuilder.core.RecordBuilder")
    public static MyRecordBuilder builder(int a, int b) {
        return new MyRecordBuilder().a(a).b(b);
    }
    ...
}
```
@fredyw fredyw force-pushed the required_components branch from 4ba9d44 to 747b252 Compare April 9, 2025 06:27
@Randgalt
Copy link
Owner

If the arguments/parameters are required, we should not include the no-arg builder(). Or, there should be some kind of validation elsewhere.

tbh - I'm not sure I'm in favor of this. Anyone else?

@fredyw
Copy link
Author

fredyw commented Apr 21, 2025

If the arguments/parameters are required, we should not include the no-arg builder(). Or, there should be some kind of validation elsewhere.

tbh - I'm not sure I'm in favor of this. Anyone else?

I agree that the no-arg builder() should not be included or else it defeats the purpose of this change. I updated the PR to remove it when @Required is present. Let me know what you think.

@Randgalt
Copy link
Owner

This will get very complex quickly. RecordBuilder has so many combinations of options, etc. For example, try this:

@RecordBuilderFull
public record RequiredComponents2(@NotNull @RecordBuilder.Required String b) {
}

The builder() method should add @NotNull to the b parameter. i.e. all the parameters should be annotated like their individual setters. So, every combination of options in RecordBuilder.Options needs to be validated.

@CfFanDuel
Copy link

This is where staged builders shine for me. Gives compile safety of must specify parameters without turning your builder constructor into a copy of the actual object constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants