DeepPrint
DeepPrint copied to clipboard
Add support for external data classes or non-data classes
If we have a data class we don't own, we should be able to deep print it somehow. Given:
data class SomeExternalClass(val name: String, val age: Int)
Not sure the best way to do this.
- we might be able to generate
SomeExternalClass.deepPrint()
exactly once, and place it in a package that matches its declaration, if any instances are the properties of an annotated class, without any modification:
@DeepPrint
data class MyClass(val externalClass: SomeExternalClass)
- or we give the processor a heads up about the class we need to generate it for:
@DeepPrint
data class MyClass(@DeepPrint val externalClass: SomeExternalClass)
- or we can manually create a deep print function, and pass it as an optional parameter:
@DeepPrint
data class MyClass(@DeepPrint(with = ::myExternalClassDeepPrintFunction)val externalClass: SomeExternalClass)
fun myEternalClassDepPrintFunction(externalClass: SomeExternalClass) {
println("""
SomeExternalClass(
val someProperty = ${externalClass.someProperty}
)
""").trimIndent()
}
Options 1 or 2 would be ideal, as it would require little to no extra work from the caller. The main issue with options 1 and 2 is getting enough information about the externally declared class. KSP is not being run on compiled code, and there is just less type information available. There might be a way to do this still, so it's worth taking another stab at.
Option 3 is probably the easiest to implement, and the most flexible. It would allow for any class from any module to be deep-printable. It does require an extra step. But, depending on the class in question, the benefit might outweigh the cost of writing another function.