jayasuryat/dowel

Classes with no-args-constructor or all-default-values-constructor need not to be annotated with @Dowel

jayasuryat opened this issue · 0 comments

What?

Properties of types having a no-args constructor or if all of the properties of a constructor have default values, then there is no need to annotate those class with @Dowel annotation or provide a custom PreviewParameterProvider via @ConsiderForDowel, as those class's instance could be easily created by invoking a no-args constructor.

For example :

@Dowel
data class Article(
    val title: String,
    val story: String,
    val type: Type,
    val meta: MetaData,
) {

    data class Type(
        val genre: String = "news",
    )

    class MetaData {
        var hasAffiliateLinks: Boolean = false
    }
}

As of now, it is an error to have models like this. It is necessary that both Type and MetaData classes be annotated with @Dowel annotation for Dowel to work.

But it is not really necessary, instances of both Type and MetaData classes can be created without any computational overhead, simply by invoking a no-args constructor (like Type() or MetaData()).

How?

This should be pretty straightforward to achieve given they way ClassRepresentation is structured.

A new type under ParameterSpec could be added for such no-args-constructor declarations. Given that ParameterSpec is a sealed type, the Kotlin compiler itself should yell until all of the cases are handled. And the object construction logic should be straightforward as well as classes can be just instantiated with ().

TODO

  • Add a new type under ParameterSpec for no-args-constructor declarations
  • Handle ClassRepresentation mapping
  • Handle object creation logic
  • Add tests
  • Update documentation