Dart2 `class` and `enum` improvements

Merged
NO
4mo ago
+569-457

This PR brings 2 new improvements to the dart2 template:

  1. Added a new cli property (--additional-property) to cause class properties to be final (immutable).
  2. When properties are final, a new copyWith() method is introduced in order to mutate an instance safely.
  3. Switches to using native enum with value rather than a class.

Especially for the 3rd point, this makes it possible to switch on that enum properly.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@jaumard (2018/09) @josh-burton (2019/12) @amondnet (2019/12) @sbu-WBT (2020/12) @kuhnroyal (2020/12) @agilob (2020/12) @ahmednfwela (2021/08)


Summary by cubic

Adds an opt-in useFinalProperties flag for immutable model fields and replaces class-based enums with native Dart enums that carry a value. Requires Dart >= 2.17; JSON (de)serialization remains compatible and enables clean switch/case.

  • New Features

    • --additional-property useFinalProperties=true now generates final properties and a copyWith() for safe updates.
    • Generate native Dart enums with an underlying value and updated transformers (toJson(), fromJson(), listFromJson()); decoders accept enum instances.
  • Migration

    • Replace enum .value reads with toJson(). Use switch (myEnum) { ... } on enum members.
    • If you enable useFinalProperties, stop mutating fields directly and use constructors or the new copyWith().
    • Ensure your project uses Dart >= 2.17 (pubspec and README updated).