Skip to content

refactor: TableRow implementation - #2775

Merged
SandPod merged 7 commits into
serverpod:mainfrom
BenAuerDev:refactor-tablerow
Sep 25, 2024
Merged

SandPod merged 7 commits into
serverpod:mainfrom
BenAuerDev:refactor-tablerow

Conversation

@BenAuerDev

@BenAuerDev BenAuerDev commented Sep 24, 2024 •

Copy link
Copy Markdown
Contributor

This PR refactors the implementation of TableRow from:
an abstract class that was extended
to
an abstract interface class that is implemented

with the following steps:
TableRow

  1. TableRow was changed to be an interface
  2. Instead of the constructor a getter and a setter was added for id

ModelLibraryGenerator

  1. The generated class now implements TableRow instead of extending it
  2. The class now has a private field _id that is initialized in the constructor body instead of calling super(id)
  3. Additionally it overrides the getter and setter from TableRow

entity_class_test.dart

  1. Test no longer expects TableRow to be extended but implemented
  2. Test no longer expects id to be passed to super but to be initialized in the constructor body

insert_sql_query_test

  • Test util classes were changed in the same fashion

closes #2711
connected to #1873

I would recommend reviewing it commit by commit, as the PR includes the output of generate_all but note, I forgot to update the test in the database package at first. This is done in the last commit

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.

If you need help, consider asking for advice on the discussion board.

Breaking changes

I don't think there are breaking changes as explained by @SandPod in #2711

change c.extends to x.implements

add a private field `_id` to class (to escape recursive getter problem)

add getter and setter for id

instead of calling super, _id is initialized in the constructor body

tests where updated to:

not expect .extends but .implements

not expect super(id) but `_id = id`

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice clean work!

Couple of suggestions that would probably simplify this change and make the code diff a lot smaller.

Simplifies the way ID is handled in generated table-classes

instead of getter and setter we override the id field itself

ID is now initialized in the initializer list instead of the body

also the url referring to TableRow is not hardcoded anymore

entity_class_test were updated to expect the id being initialized/declared in the initializer list/constructor

inser_sql_query_test util classes were changed to align with the changes above
This removes `hasPrimaryKey` from the assertion of `shouldIncludeType` this way we also don't need to check for isIdFieldInMainConstructor

@SandPod SandPod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice clean change! 👍

@SandPod
SandPod merged commit cfbe001 into serverpod:main Sep 25, 2024
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 25, 2024
@BenAuerDev
BenAuerDev deleted the refactor-tablerow branch October 11, 2024 12:52
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.

Refactor TableRow and the way it is implemented to allow to use inheritance on Models with Database

2 participants