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

Dart: fix problems with default values that use collection types #1622

Conversation

pwrobeldev
Copy link
Contributor

This series fixes problems that occur when either @Immutable
or @PositionalDefaults annotation is used in combination with
collection types.

-------- @PositionalDefaults --------
The usage of 'PositionalDefaults' annotation
combined with nullable collection field that has 'null'
default value yields the code that does not compile.

The generator blindly applies 'const' keyword before
the default value for collections. A special 'null' value is
not taken into account.

-------- @Immutable --------
Usage of 'Immutable' annotation combined
with collection fields that have default values yields the
code that does not compile.

The generator does not apply 'const' keyword before the calls
to constructors of collections. It is required, because the
constructor of 'Immutable' structure is marked as 'const'.

The same happens when an explicit field constructor is used.
Because of 'Immutable' usage it is marked as a const, but
the default values are not initialized with values prepended
by 'const'.

-------- Content of change --------

  • New smoke and functional tests that were used to confirm
    the invalid behavior.
  • Adjustments of 'DartStructConstructors.mustache' file to
    correctly prepend 'const' keyword when needed and to treat
    'null' as it should be treated.

…rrect code

This change is used to showcase the invalid behavior of
Dart generator. Usage of 'PositionalDefaults' annotation
combined with nullable collection field that has 'null'
default value yields the code that does not compile.

The generator blindly applies 'const' keyword before
the default value for collections. This is done in
'DartStructConstructors.mustache' depending on the
field LIME type. However, a special 'null' value is
not taken into account. It seems to be invalid.

Signed-off-by: Patryk Wrobel <[email protected]>
This change is used to showcase the invalid behavior of
Dart generator. Usage of 'Immutable' annotation combined
with collection fields that have default values yields the
code that does not compile.

The generator does not apply 'const' keyword before the calls
to constructors of collections. It is required, because the
constructor of 'Immutable' structure is marked as 'const'.

The same happens in an explicit field constructor is used.
Because of 'Immutable' usage it is marked as a const, but
the default values are not initialized with values prepended
by 'const'.

Signed-off-by: Patryk Wrobel <[email protected]>
…lections

This change adjusts the logic that prepends 'const'
keyword in 'DartStructConstructors.mustache' to avoid
adding 'const' keyword when the default value is null.

It was required to generate the code that compiles.
Without the adjustment the functional test did not compile.

Signed-off-by: Patryk Wrobel <[email protected]>
This change adjusts 'DartStructConstructors.mustache' to ensure
that 'const' keyword is prepended to constructors of default
values that are collections when 'Immutable' annotation is used.

It was required because the constructors of 'Immutable' structures
are always const. To initialize the default values const values
are needed.

Signed-off-by: Patryk Wrobel <[email protected]>
Describe implemented bug fix related to missing/superflous const
keyword usage in constructors when annotations were specified.

Signed-off-by: Patryk Wrobel <[email protected]>
@pwrobeldev
Copy link
Contributor Author

Note for reviewers:

  • two first commits were used to cause functional tests build failure that confirmed invalid behavior of Dart generator
  • then the adjustments were implemented in mustache templates to ensure that the tests compile without problems

@pwrobeldev pwrobeldev merged commit b516e6b into master Nov 21, 2024
17 checks passed
@pwrobeldev pwrobeldev deleted the pwrobeldev/handling-of-default-value-of-nullable-collections-in-dart branch November 21, 2024 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants