record-builder icon indicating copy to clipboard operation
record-builder copied to clipboard

Allow nullable collections with useImmutableCollections=true

Open fprochazka opened this issue 3 years ago • 1 comments

Hi, somewhat related to #122, I'd like to propose allowing collections to become nullable, because right now useImmutableCollections = true does two things

  • it adds the conversion to immutable collections (awesome!)
  • but also enforces that the output record can never have the collection values nullable

I'm proposing to change the behaviour based on interpretNotNulls value.

  • useImmutableCollections = true && interpretNotNulls = false
    • mapping will be return (o != null) ? Map.copyOf(o) : null;
  • useImmutableCollections = true && interpretNotNulls = true
    • and field is determined to be nullable
      • mapping will be return (o != null) ? Map.copyOf(o) : null;
    • and field is determined to be notnull
      • mapping will be return (o != null) ? Map.copyOf(o) : Map.of();
  • useImmutableCollections = true
    • current behaviour

This should also (consistently) affect the default value for collections mentioned in #122

fprochazka avatar Jul 19 '22 12:07 fprochazka

I started working on this and so far I got to a point where record components return null when a new useNullableCollections option is set to true (default is false for backwards compatibility). It does not yet take into account if nulls are interpreted or not.

I decided to create a new option for it to not break any existing code as I'd not be surprised if library users were relying on these collection/list/set/map components always being non-null. Please let me know if it should be done otherwise.

hofiisek avatar Oct 26 '23 13:10 hofiisek