modddels icon indicating copy to clipboard operation
modddels copied to clipboard

Add support for generics

Open CodingSoot opened this issue 1 year ago • 2 comments

Background

When it comes to implementing generics, we should consider two main issues : param transformations and type constraints.

In general, a "param transformation" is a change in the type of a parameter. There are three kinds of param transformations :

  • Valid param transformations : Happens on the member parameters of an Entity, after the contentValidation step. Example : Age age becomes ValidAge age.
  • Non-null param transformations : Happens on a nullable member parameter of a ValueObject or Entity, when it's annotated with @NullFailure. Example : Age? age becomes Age age.
  • Null param transformations : Happens on a nullable member parameter (which should be an InvalidModddel) of a SimpleEntity, when it's annotated with @invalidParam, after the contentValidation step. Example : InvalidAge? age becomes Null age.

Additionally, we should keep in mind that :

  • Member parameters of entities must be modddels
  • For Iterable/Iterable2 entities, the member parameter must match the TypeTemplate

Solution

With all this in mind, let's see how we can implement generics :

  • ✅ For dependency parameters, there's no problem
  • ✅ For member parameters that don't have any param transformation, there's no problem. These would be :
    • The member parameters of ValueObjects that are not annotated with @NullFailure
    • The member parameters of Entities that are annotated with @validParam and aren't annotated with @NullFailure
  • 🔴 For member parameters that have a valid param transformation : The type parameter must extend BaseModddel. However, there's no way to convert the type parameter to its valid version. We can either disable the valid param transformation for type parameters, or disallow type parameters in this case.
  • ✅ For member parameters that have a non-null param transformation : The type parameter must be non-nullable (ex : T extends Object), and the type of the member parameter must be nullable (T? param).
  • ✅ For member parameters that have a null param transformation : The type parameter must extend InvalidModddel, and there's no restriction on whether its nullable or not as long as the resulting type of the member parameter is nullable.

Another thing to consider is how these rules apply to the types of shared properties, knowing that a SharedProp has options to disable certain param transformations.

CodingSoot avatar Jun 24 '23 19:06 CodingSoot