contentful / vault

Easy persistence of Contentful data for Android over SQLite.
https://contentful.github.io/vault/
Apache License 2.0
85 stars 19 forks source link

@Field Map in Kotlin gives unsupported type error #154

Closed omarterem closed 5 years ago

omarterem commented 5 years ago

Hi. We are using Kotlin to create vault models, and it works great with Strings, Ints, and Lists. However, we recently introduced a Map (JSON Object) in contentful and are having problems parsing it in Kotlin. @JvmField @Field var config: Map<String, String>? = null gives the following error at compile time: @Field specified for unsupported type("java.util.Map<java.lang.String,java.lang.String>") I've tried using Map<*, *>? and many other combinations as well. Also tried adding @JvmSuppressWildcards, but same error.

So I converted that particular Model to java and used @Field Map config like mentioned here: https://github.com/contentful/vault/blob/master/tests-integration/src/test/java/com/contentful/vaultintegration/lib/allthethings/AllTheThingsResource.java#L28 and it worked without any issues.

I would prefer to keep my model in Kotlin, is there a a workaround for adding a Map? Or am I doing something wrong?

mariobodemann commented 5 years ago

Hello,

you can indeed keep you model in Kotlin. You need to change the fields generic parameters to Nothing like so:

@Field
@JvmField
public var json: Map<Nothing, Nothing>? = null

Then the right map gets generated and you can access it in kotlin:

val withJ = vault.fetch(MyModel::class.java).first()
Log.d("TAG", withJ.json!!["field"].toString())

(Please keep in mind that you now also need to take care of the nullabillity and the typing of the map)

Greetings, Mario

omarterem commented 5 years ago

Ah. You are a legend. Thank you very much for the quick reply. Confirming that it works with Map<Nothing, Nothing>.

Regards,

Omar

mariobodemann commented 5 years ago

Thank you for the nice words, I'm happy it works for you..