kopykat-kt / kopykat

Little utilities for more pleasant immutable data in Kotlin
Other
283 stars 16 forks source link

New annotation for creating isomorphic class with nullable fields #91

Open sschuberth opened 1 year ago

sschuberth commented 1 year ago

I'm not sure whether this is in scope of this library, but I wanted to mention the idea:

Based on an existing data class which has non-nullable properties, I'm often in the need to create a variant of that class whose properties are the same, but nullable. A use-case is e.g. configuration classes with optional / nullable properties, that are turned into "full configuration classes" by applying non-null defaults to nullable properties, resulting in a configuration class with all non-nullable properties.

So, how about introducing an annotation named e.g. @CopyAsNullable that could be used like

@CopyAsNullable(to = "OptionalPerson")
data class Person(val name: String, val friends: List<Person>)

which would generate the class

data class OptionalPerson(val name: String? = null, val friends: List<Person>? = null)

The application of the default = null values could be made optional via another argument to @CopyAsNullable.