Repository navigation
Feature/extend data classes - #2710
Conversation
SandPod
left a comment
There was a problem hiding this comment.
Awesome work! 🎉
Thanks for some great readable code that has tests included.
I think we should resolve the extended class connection in a different way. I we take inspiration from how we resolve relations we can work with references instead of making lookups in multiple places.
We should also think about how we want to relase this. It would be great if we can merge your work to main but without exposing the new functionality to our users. That would allow us to get more fixes in before we ship it.
|
Wow! Great work @BenAuerDev, really looking forward to getting this into Serverpod! 🚀 |
ClassDefinition now holds two fields related to inheritance `subClasses` and `extendsClass` of type `InheritanceDefinition` `InheritanceDefintion` was added with 3 classes extending it: * UnresolvedInheritanceDefinition * BaseClassInheritanceDefinition (holding a list of subClasses) * ExtendsClassInheritanceDefinition (holding the fields and path of the class that is extended) extendsClass is then parsed as `UnresolvedInheritanceDefinition` and in the resolver step, both `extendsClass` and `subClasses` are resolved to hold the needed information and model-library-generator now takes the necessary information from the inheritance fields of it's ClassDefinition also the tests and test_utils were updated reflecting the changes made to the resolving workflow
`classNames` and `table` were added to each others `mutuallyExclusiveKeys`. a new check was introduces checking if the moduleAlias is 'protocol' (if the extended class is from the same project and not from a module or serverpod itself. expected error-message in test was updated
Comment:I was not able to add a test covering the check for external models to as we create the models for the test like var modelSources = [
ModelSourceBuilder().withYaml(
'''
class: Example
fields:
name: String
''',
).build(),
ModelSourceBuilder().withFileName('example2').withYaml(
'''
class: ExampleSubClass
extends: Example
table: example_sub_class
fields:
age: int
''',
).build(),
];and when used like: var modelSources = [
ModelSourceBuilder().withFileName('example2').withYaml(
'''
class: ExampleSubClass
extends: ServerHealthMetric
fields:
age: int
''',
).build(),
];the previous restriction if (baseClass == null) {
return [
SourceSpanSeverityException(
'The class "$baseClassName" was not found in any model.',
span,
)
];
}is executed as every class not declared within But I tested it manually in the command line and the restriction is executed |
this is no longer needed as this is done in the model resolver
SandPod
left a comment
There was a problem hiding this comment.
Great improvement!
The resolver step will make this implementation a bit more flexible, still have a couple of comments regarding how the linking is built.
I'll also take a look at enabling testing with "serverpod" files. I believe this should be possible since we use the same interface as the code.
definitions where simplified: - ResolvedInheritanceDefinition - holds ClassDefinition - UnresolvedInheritanceDefinition - holds ClassName - classDefinition.subClasses (soon childClasses) is now of type List<ResolvedInheritanceDefinition> - classDefinition.extendsClass is of type ResolvedInheritanceDefinition - classDefinitionBuilder also had to be updated. - inheritance_class_test was updated/improved for both server and client
except in class_definition_builder there i named it "_extendsClass" as it is the same on ClassDefinition
improved readability for `var extendsClass = .....` and removed part I accepted when merging main
Adds a new multi-option "experimental-features" to the generate command. Config has new field "experimentalFeatures" which is propagated down to extractModelDefinition, and used in ModelParser to decide when extendsClass should be parsed
A singleton was added which can be instantiated and holds a list of experimental features. the multiOption on the argParser was moved from the Generate command to ServerpodCommandRunner another option was added to pass --experimental-features=all which enables all experimental features. This is used in migration_test_utils.dart and util/generate_all
this is more robust
Isakdl
left a comment
There was a problem hiding this comment.
Good work! Almost there I have one additional request to manage the import URL in the same way as the rest of the code, it is a little annoying but I think it will complete the functionality of our parser.
…arser As we don't guard in the parser anymore we also don't need to pass enableInheritance, I forgot that when I removed the guard
This adds a new field type (TypeDefinition) to SerializableModelDefinition which is required for ClassDefinition and EnumDefintion. Also some tests were added to check if the type field is properly set on: - classes - enums - exceptions
this creates a relative import and adds '..' for every subDirPart so it should be the childs subDirParts
Isakdl
left a comment
There was a problem hiding this comment.
One small little request and we should be ready.
the type is now non-nullable and final I also updated the test-utils - ClassDefinitionBuilder - EnumDefinitionBuilder Which now assigns = TypeDefinitionBuilder.... to it but can be otherwise modified if necessary with the .withType() method if needed
Isakdl
left a comment
There was a problem hiding this comment.
Almost there, the test builders do not produce a valid default state / management.
withType was removed, as it should not be manually overridden. type.enumDefinition is set to the instance of the enum ClassDefinitionBuilder.type.className is set dynamically
Isakdl
left a comment
There was a problem hiding this comment.
I found some unused dead code and some very small nit things. But this should be the final change request!
Isakdl
left a comment
There was a problem hiding this comment.
Good job @BenAuerDev, LGTM 🚀 🚀 🚀 let's go 😄

This PR adds the functionality to allow using the
extendskeyword to create a class based on another.This generates a SubClass that can be used like
It is not yet possible to add a DB-Table to a base- or sub-class, as a DB-Class extends TableRow and we can't extend two classes. To achieve this like requested in #1618 we would need to make TableRow an interface which can be implemented by the class so the extends property is free to be used for inheritance. New Issue: #2711
connected to: #1873
Review advice
I do recommend going through the changes commit by commit. I tried to make them (somewhat) bite sized.
Review changes
As proposed in the first review, the functionality was also added to not directly expose the functionality to the user when running
serverpod generate. To access the functionality the user must runserverpod generate --experimental-features=inheritanceAlso
ClassDefinitionandEnumDefintionnow have atypefield.Pre-launch Checklist
///), and made sure that the documentation follows the same style as other Serverpod documentation. I checked spelling and grammar.If you need help, consider asking for advice on the discussion board.
Breaking changes
No breaking changes