Home > other >  how can I convert my room entity to my data class in kotlin?
how can I convert my room entity to my data class in kotlin?

Time:12-02

I have a data class that I pull from internet and I want to save room database but there is a problem like that.

enter image description here

It always gives an error like this, how can I overcome this problem?

my room entity class

@Entity(tableName = "ExchangeValues")
data class ExchangeEntity(

    @ColumnInfo(name = "base_code") val base_code: String,
    @ColumnInfo(name = "conversion_rates") val conversion_rates: ConversionRates,
    @ColumnInfo(name = "result") val result: String,
    @PrimaryKey(autoGenerate = true) val uid:Int?=null

)

my dao

@Dao
interface ExchangeDao {


    @Query("SELECT * FROM ExchangeValues")
    suspend fun getAll() : List<ExchangeEntity>

    @Query("UPDATE ExchangeValues SET  base_code=:base_code,conversion_rates=:conversion_rates , result=:result")
    suspend fun update(base_code:String,conversion_rates:ConversionRates,result:String)

}

my exchange data class

@Serializable
data class Exchange(
    val base_code: String,
    val conversion_rates: ConversionRates,
    val documentation: String,
    val result: String,
    val terms_of_use: String,
    val time_last_update_unix: Int,
    val time_last_update_utc: String,
    val time_next_update_unix: Int,
    val time_next_update_utc: String
) {
    fun toEntity() = ExchangeEntity(
        base_code = base_code,
        conversion_rates = conversion_rates,
        result = result
    )
}

@Serializable
data class ConversionRates(
    val conversionRates : Map<String,Double>
)

I cant use toEntity function in getAll()

exchangeRepositoryImpl

class ExchangeRepositoryImpl @Inject constructor(
    private val dao:ExchangeDao
) : ExchangeRepository{


    override suspend fun getAll() : Flow<List<Exchange>> {

        return flow {

            emit(dao.getAll())
        }

    }

    override suspend fun update(exchange: Exchange) {
        dao.update(exchange.base_code,exchange.result,exchange.conversion_rates)
    }


}

my exchange converter

class ExchangeConverter {

    @TypeConverter
    fun fromSource(conversionRates: ConversionRates) : String{
        val gson = Gson()
        return gson.toJson(conversionRates)
    }

    @TypeConverter
    fun toSource(json: String): ConversionRates {
        val gson = Gson()
        val typeToken = object : TypeToken<List<ConversionRates>>() {}.type
        return Gson().fromJson(json, typeToken)
    }
}

I wrote a converter like this, but it might not be correct, I'm not so sure. How can I solve this problem?

CodePudding user response:

Inside flow you have created call map function the call to toEntity() eg

    flow{ 
          emit (dao.getAll().map{it.toEntity()})
        }

CodePudding user response:

Well your flow returns a flow of

List<Exchange>

and your repo returns

List<ExchangeEntity>

and there's nothing in your code to map an ExchangeEntity to an Exchange.

So you need something like:

override suspend fun getAll() : Flow<List<Exchange>> {

        return flow {

            emit(dao.getAll().map{Exchange(base_code = it.baseCode)})// add in other fields on exchange constructor
        }

    }
  • Related