Randgalt / record-builder

Record builder generator for Java records

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow nullable collections with useImmutableCollections=true

fprochazka opened this issue · 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

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.