natario1
09/09/2020, 6:02 PMclass Foo : Bar {
val foo by bar()
}
It would be nice, from foo
property, to retrieve the initializer "by bar()"
as a code block / string that can be analyzed.Ting-Yuan Huang
09/09/2020, 6:09 PMnatario1
09/09/2020, 6:19 PMPropertySpec.initializer.toString()
.
If you want a concrete example, I have a library that lets you model Firestore database objects like this:
@FirestoreClass
class User {
val firstName: String by this
val lastName: String by this
}
A processor cycles over the properties to store their names, however sometimes the server field name is slightly different. It would be powerful to be able to write val firstName: String by this("first_name")
and retrieve "first_name" in the processor, without adding verbose annotations to each field.Ting-Yuan Huang
09/15/2020, 4:50 PMval firstName: String by this("first_name")
, we would need to model function call and delegation before "first_name"
can be retrieved. That (supporting expressions / statements) is beyond our current plan.natario1
09/15/2020, 5:12 PMval property: KSPropertyDeclaration = ...
val initializer: String = property.initializer
// initializer is a string equal to "by this(\"first_name\")"
require(initializer.startsWith("by this")) {
"DB properties must be delegated to this!"
}
val fieldName = initializer.substring(9, initializer.lastIndexOf("\""))
So you don't really need models for functions and delegation, it's just an opaque string, saying nothing about its contents, like KotlinPoet's CodeBlock .
This example is a bit convoluted but, for instance, for literals const val foo = 0L
this string would be equal to "0L" which one could transform to Long. And just like KSPropertyDeclaration.initializer one could have KSFunctionDeclaration.body and so on. I understand if you think this is out of scope, I just wanted to be clearTing-Yuan Huang
09/15/2020, 5:14 PMnatario1
09/15/2020, 5:15 PM