All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.harmony.kotlin.data.datasource.flow.FlowSerializationDataSourceMapper.kt Maven / Gradle / Ivy

package com.harmony.kotlin.data.datasource.flow

import com.harmony.kotlin.data.mapper.Mapper
import com.harmony.kotlin.data.query.Query
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

/**
 * This data source uses mappers to map objects and redirects them to the contained data source, acting as a simple "translator".
 * This class map a List to a single Object when using putAll, in this case it will call the put method on the contained data source
 * This class map a Object to a List when using getAll, int his case it will call the get method on the contained data source
 *
 * @param getDataSource Data source with get operations
 * @param putDataSource Data source with put operations
 * @param deleteDataSource Data source with delete operations
 * @param toOutMapper Mapper to map data source objects to repository objects
 * @param toOutListMapper Mapper to map data source objects to repository object lists
 * @param toInMapper Mapper to map repository objects to data source objects
 * @param toInMapperFromList Mapper to map repository object lists to data source objects
 */
@Deprecated("Not needed after getAll & putAll deprecation. Use a normal FlowDataSourceMapper")
class FlowSerializationDataSourceMapper(
  private val getDataSource: FlowGetDataSource,
  private val putDataSource: FlowPutDataSource,
  private val deleteDataSource: FlowDeleteDataSource,
  private val toOutMapper: Mapper,
  private val toOutListMapper: Mapper>,
  private val toInMapper: Mapper,
  private val toInMapperFromList: Mapper, SerializedIn>
) : FlowGetDataSource, FlowPutDataSource, FlowDeleteDataSource {

  override fun get(query: Query) = getDataSource.get(query).map { toOutMapper.map(it) }

  @Deprecated("Use get instead")
  override fun getAll(query: Query): Flow> = getDataSource.get(query).map { toOutListMapper.map(it) }

  override fun put(query: Query, value: Out?): Flow {
    val mapped = value?.let { toInMapper.map(it) }
    return putDataSource.put(query, mapped)
      .map { toOutMapper.map(it) }
  }

  @Deprecated("Use put instead")
  override fun putAll(query: Query, value: List?): Flow> {
    val mapped = value?.let { toInMapperFromList.map(it) }
    return putDataSource.put(query, mapped)
      .map { toOutListMapper.map(it) }
  }

  override fun delete(query: Query) = deleteDataSource.delete(query)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy