Repository navigation
copyWith, hashCode and operator==. - #976
pro100andrey wants to merge 23 commits into
Conversation
|
This looks great! :) The tests are likely failing because you haven't run |
|
@vlidholt in progress |
Isakdl
left a comment
There was a problem hiding this comment.
Very nice PR, I love this feature, thank you! 😄 ❤️
Only one thing I would like you to change and that is the test and group descriptions. They don't tell us much about what is going on in the tests currently.
General guidelines:
- The test description should be precise enough that I understand what we are testing, think of them as documentation for the production code.
- The test description should make it very obvious what went wrong if the test fails when running them. I.E. when I see 20-30 test results and one random test failed I should get an idea of what happened without looking at the test implementation.
- Each test should test only one specific behavior (this also makes it easier to write the description).
I put an example of some test descriptions in the test file!
|
hi @Isakdl, could you check current tests implementation, please? |
pro100andrey
left a comment
There was a problem hiding this comment.
Please check
|
I wanted to provide a summary of changes I have made recently regarding the extra generation feature for models. To enable the generation of extra methods for the model, it is necessary to add the "extra" flag. Here is an example of the code implementation: class: ExtraDataClass
...
extra: client
fields:
duration: Duration
...Currently, only client-side support is available, as implementing server-side support would require significant modifications. However, I have made provisions to enable future expansions in this regard. For the implementation of the Regarding the standard To provide more context, please refer to the following link for a detailed explanation of the problem and the approach I took to overcome it. If you have any questions or require further clarification on these developments, please don't hesitate to reach me. I am more than happy to provide any additional information you may need. |
|
@pro100andrey, I think that the Need to dig into what you did with setting null values in |
|
Hi @vlidholt, ok, but I understand that users may have specific use cases that could potentially be affected if we generate both server and client models by default. I propose introducing the option for users to have control over whether they want the generation of extra methods. This way, they can decide if they need these additional methods based on their individual requirements. Like this: class: ExtraDataClass
...
extraMethods: true
fields:
duration: Duration
...Think about it. If you insist, I will enable default generation for both server and client. Thanks! |
|
Should be optional for both server and client. And leave room for when dart dataclasses land. |
* main: feat: Adds migrate --repair command to CLI. (#980) fix: Correctly validates tag names for database migrations. (#970) feat: Database migrations are applied by priority. (#968) fix: Handle invalid return types when parsing endpoint methods. (#967) test: Fixes issues in migration and adds tests. (#966) # Conflicts: # packages/serverpod/lib/src/generated/database/database_definition.dart # packages/serverpod/lib/src/generated/database/database_migration.dart # packages/serverpod_service_client/lib/src/protocol/database/database_definition.dart # packages/serverpod_service_client/lib/src/protocol/database/database_migration.dart # tools/serverpod_cli/lib/src/generator/generator_database_definition.dart # tools/serverpod_cli/lib/src/migrations/generator.dart
|
Hi @vlidholt. After adding default support for copyWith, @==, and hashCode for the Server and Client classes, there are breaking changes: To adhere to the immutability principles, the fields should be final. Therefore, the setColumn method, which was used only to set the id value, has been removed. Now, this can be done through copyWith. As a result, the insert method has been modified, and it now returns a TableRow with set the id. Future<int?> storeObjectWithObject(
Session session,
ObjectWithObject object,
) async {
// insert returns ObjectWithObject instance with set id
var result = await ObjectWithObject.insert(session, object);
return result.id;
}Positive aspects:
Please review the recent changes and let me know if I'm moving in the right direction. Thanks! |
|
@pro100andrey We cannot approve any breaking changes for this PR. Adding immutability has the potential to break a lot of things for our users. Also, most of the tests doesn't pass at the moment, these all need to work for the PR to be approved. A tip is that you can run the tests on your own fork on Github by pushing to the To get this PR approved either:
class: MyClass
immutable: true
fields:
myInt: int
myString: StringIf I remember correctly, the |
|
@vlidholt Ok, but what's the point of including copyWith for server-side if the fields in it are mutable? |
|
@vlidholt I can provide you with code generation for the client side only that adheres to the best practices of immutability. Let's start with that. |
|
@pro100andrey I would assume there can be cases where it makes sense so copy over modifying an object. Having the extra method will add very little overhead, so I don't see a problem in including it even for mutable classes. It may be a better idea to break this PR up into two parts to get it approved faster:
|
|
@vlidholt I will work on it as soon as I have a little time, hopefully today :) |
|
Closing this as it is stale, but added a link in the issue so that we can revisit ahead of our next release. |
Many state management solutions like Redux, BloC, and Riverpod utilize data validation and notification mechanisms based on the data structure. To ensure successful validation, models need to meet certain requirements, such as implementing methods like hashCode and operator==.
To support this, I have added the capability to generate these methods for models.
748
Pre-launch Checklist
///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.Breaking changes
No breaking changes
Example:
generated post.dart
Currently, the generation of these methods only occurs for the client-side when the user invokes
serverpod generate.Perhaps, you would like me to add the generation of these methods with other way. Here are some approximate examples:
with yaml:
with serverpod generate:
serverpod generate --extra-data-class <server, client, both>