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

Adds RecordBuilder.Options.defaultNotNull parameters #185

Merged

Conversation

agustafson
Copy link

Thanks for putting together record-builder! It's definitely a welcome addition and enhancement on top of records.

Issue/Enhancement

Some codebases expect fields to be non-null by default, for example if NullAway enforcement is in place. Therefore it is useful to set a parameter to expect that all fields will be non-null unless explicitly marked as nullable.
Currently our codebase uses https://immutables.github.io/immutable.html which defaults to non-null fields, and it would be good to have record-builder have the capability for the same behaviour.

Changes

Adds the ability to default fields to be non-null, unless they are annoted with a nullable annotation, ie:

@RecordBuilder.Options(defaultNotNull = true/false)

Example

For example:

@RecordBuilder.Options(defaultNotNull = true)
@RecordBuilder
public record RecordWithDefaultNotNull(Integer notNullInteger, String notNullString, @Null String nullString) {
}

will check/ensure that notNullInteger and notNullString are not null via requireNonNull calls.

Questions

  • What happens for collections? Can we enforce non-nullability in the same way by defaulting to non-null? Or is this not required because collections are non-null by default anyway.
  • Can we also support other annotations such as javax.annotation.ParametersAreNonnullByDefault? That might be a future enhancement.

@agustafson
Copy link
Author

Hi @Randgalt just bumping this one to see if there's any progress please? I'm happy to rework it if there's any suggestions that you have. Thanks!

@Randgalt
Copy link
Owner

I apologize - I've been completely slammed at work. I will get to this later today or tomorrow.

@agustafson
Copy link
Author

Don't apologise! It's tough work juggling work and open-source projects, so I appreciate you taking some time to look at it.

@Randgalt Randgalt merged commit 859e912 into Randgalt:master Oct 1, 2024
2 checks passed
@Randgalt
Copy link
Owner

Randgalt commented Oct 1, 2024

Good change - thank you

@Randgalt Randgalt added this to the v43 milestone Oct 1, 2024
@agustafson
Copy link
Author

Amazing! Thanks for that.

@agustafson agustafson deleted the agustafson/assume-non-null-parameters branch October 2, 2024 03:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants