pushtorefresh / storio

Reactive API for SQLiteDatabase and ContentResolver.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Kotlin Data Class support

ramasauskas opened this issue · comments

commented

Hello, I've recently dug up into StarIO and so far the API seemed nice and smooth until I tried applying it to a Kotlin Data Class. Is there any support for it, or am I using bad annotations here ? I keep getting this issue:
Error:(19, 1) Some error(s) occurred while processing annotations. Please see the error messages above.

And my data class looks like:

@StorIOSQLiteType(table = "users")
data class User @StorIOSQLiteCreator constructor(
        @StorIOSQLiteColumn(name = "_id", key = true) val _id: String = "",
        @StorIOSQLiteColumn(name = "email") val email: String,
        @StorIOSQLiteColumn(name = "password") val password: String? = null,
        @StorIOSQLiteColumn(name = "firstName") val firstName: String,
        @StorIOSQLiteColumn(name = "profilePicture") val profilePicture: String? = null,
        @StorIOSQLiteColumn(name = "location") val location: String? = null,
        @StorIOSQLiteColumn(name = "createdAt") val createdAt: String? = null,
        @StorIOSQLiteColumn(name = "updatedAt") val updatedAt: String? = null)

Hi! Are there any messages about an error or it is the only one? Because if it is the only one - it is not very helpful and we need to improve it for sure.
Regarding the error, you need to change properties declaration from val to var since we need both getter and setter to be able to generate correct code for this case.

commented

Got it, yes the only error I'm getting is the one I provided, plus a warning:
Warning:warning: The following options were not recognized by any processor: '[kapt.kotlin.generated]' , besides that nothing more.

Here's the final code that seems to work now

@StorIOSQLiteType(table = "users")
data class User @StorIOSQLiteCreator constructor(
        @StorIOSQLiteColumn(name = "_id", key = true) var _id: String = "",
        @StorIOSQLiteColumn(name = "email") var email: String,
        @StorIOSQLiteColumn(name = "password") var password: String? = null,
        @StorIOSQLiteColumn(name = "firstName") var firstName: String,
        @StorIOSQLiteColumn(name = "profilePicture") var profilePicture: String? = null,
        @StorIOSQLiteColumn(name = "location") var location: String? = null,
        @StorIOSQLiteColumn(name = "createdAt") var createdAt: String? = null,
        @StorIOSQLiteColumn(name = "updatedAt") var updatedAt: String? = null) {

    constructor() : this (email = "", firstName = "")
}

I just double-checked it and realised that current Kotlin integration doesn't work as it was intended. Your initial code should be perfectly valid and this will be fixed in incoming PR. Meanwhile, the solution with vars and an empty secondary constructor is just a workaround and shouldn't be used at all since it doesn't respect immutability.
Thank your for reporting this issue and I hope that not many people were affected by it.

PR is merged and the fix will be available in the next release.