Skip to content

feat: Add copyWith methods on all generated protocol files. - #1358

Merged
Isakdl merged 17 commits into
serverpod:mainfrom
Isakdl:748-copyWith
Sep 13, 2023
Merged

Isakdl merged 17 commits into
serverpod:mainfrom
Isakdl:748-copyWith

Conversation

@Isakdl

@Isakdl Isakdl commented Sep 12, 2023

Copy link
Copy Markdown
Collaborator

Changes

Adds a copyWith method on all generated classes. The method has support for differentiating between null and undefined input. This means that you are able to set a nullable field to null again via the copyWith method.

The method also makes a deep copy of all objects and variables included in the class. This also means that any custom classes that is used together with this method also must implement their own copyWith method. If the method does not exist an exception is thrown.

Example:

class Example {
  String? name;
  ...
}

var example = Example();

var copy = example.copyWith(name: 'Isak');
print(copy.name); // Isak

var exactCopy = example.copyWith();
print(exactCopy.name); // Isak

var anotherCopy = copy.copyWith(name: null);
print(anotherCopy.name); // null

closes: #748

This PR is based on: #976 and the work done by @pro100andrey (thank you!)

Limitation

This PR only implements the copyWith method but does not include any immutability or equality overrides etc. These features should be implemented in a separate PR dealing with immutability.

Review

Most of the code changes are generated code and tests, the key additions are inside the code generator as well as inside the serialisation package.

Pre-launch Checklist

  • I read the Contribute page and followed the process outlined there for submitting PRs.
  • This update contains only one single feature or bug fix and nothing else. (If you are submitting multiple fixes, please make multiple PRs.)
  • I read and followed the Dart Style Guide and formatted the code with dart format.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.
  • I added new tests to check the change I am making.
  • All existing and new tests are passing.
  • Any breaking changes are documented below.

@Isakdl
Isakdl requested a review from vlidholt September 12, 2023 15:33

@vlidholt vlidholt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with comments fixed/checked. Very cool work!

@Isakdl
Isakdl merged commit 7b41e0f into serverpod:main Sep 13, 2023
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 13, 2023
@Isakdl
Isakdl deleted the 748-copyWith branch November 13, 2023 15:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: copyWith() method for entities

2 participants