I am working on generic data-access library (for e...
# announcements
d
I am working on generic data-access library (for example for accessing your SQL database) inspired by the great model system in Django (Python). The general idea is that you write your database models like this:
Copy code
object UserModel : Model {
    val name by VarcharField()
}
From that I am doing to generate code like this (using an annotation processor):
data class User(val name: String)
If I now have a call like
dataSource.findAll(UserModel)
how can I express to the compiler that this returns
List<User>
?
p
d
Well, yes. But
User
is generated by an annotation processor.
p
Also look at QueryDSL http://www.querydsl.com/static/querydsl/4.1.3/reference/html_single/#d0e211 They do this other way around: generate model class based on annotations on entity class.
d
Yes, I have used QueryDSL in the past. Even if I do it the other way around there is still no way to "connect" the data class and the Model object. That would make thinks like this impossible:
findAll<User> { it.name eq "Peter" }
where
it
has to be
UserModel
, not
User
Maybe what I want is not possible...
p
but this would be possible
Copy code
findAll(UserModel) { it.name eq "Peter" }
looks almost the same 🙂
👍 1
d
Hm. True. I'll take a stab at that later when I'm back home. Thank you!
👍 1
One issue with using annotations is that it gives reduced type safety.
@Varchar val name: Int
is not a compile-time error, even though it should be.