how does room’s ksp plugin obtains the default val...
# ksp
h
how does room’s ksp plugin obtains the default values from a data class? I can see that it’s nearly impossible to do so, there must be some workaround
j
h
the proposed workaround doesn’t work, at least not for me
m
I think Room will run the SQL in the query so it knows what fields are returned by the SQL query. It doesn't need to know the default value. It can choose the constructor based on the fields that are returned by running the query, and thus omit fields that are not returned by the query (e.g. allowing the default to take effect)
g
Is this not visible in the generated Room classes? (in /build/generated I mean)
h
i could not find it in the generated room classes ex.
Copy code
@ColumnInfo(name = "contacts_count") var contactsCount: Int = -1
and in the generated one
Copy code
_columnsGroups.put("contacts_count", new TableInfo.Column("contacts_count", "INTEGER", true, 0, null, TableInfo.CREATED_FROM_ENTITY));
where the default value is null which comes after
true, 0
m
The ColumnInfo annotation has a defaultValue property (which is applied to the generated create table SQL statements): https://developer.android.com/reference/androidx/room/ColumnInfo .
If you want to see what Room is really doing (and if it is by any magic getting a default value from source), then look at the generated dao _Impl class where it actually runs the query.
h
The problem is that it's restricted to a String as a default value, it's known already I think that your default values can come from the annotation, but you can't put Any in an annotation, let alone make it nullable I wonder how Moshi does it
m
h
As a string, please refer to my latter
m
Yes, it is using a string to do that. But I still don't see anything that indicates that room knows the default value from a data class. Room generates something like this in the _Impl classs:
Copy code
final String _tmpAgentMbox_sha1sum;
        if (_cursor.isNull(_cursorIndexOfAgentMboxSha1sum)) {
          _tmpAgentMbox_sha1sum = null;
        } else {
          _tmpAgentMbox_sha1sum = _cursor.getString(_cursorIndexOfAgentMboxSha1sum);
        }
_result.setAgentMbox_sha1sum(_tmpAgentMbox_sha1sum);