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

commonMain.com.harmony.kotlin.data.mapper.Mapper.kt Maven / Gradle / Ivy

package com.harmony.kotlin.data.mapper

/**
 * Interface to map an object type to another object type
 */
interface Mapper {

  fun map(from: From): To
}

/**
 * Mapper for Lists
 */
class ListMapper(private val singleValueMapper: Mapper) : Mapper, List> {
  override fun map(from: List): List {
    return from.map { singleValueMapper.map(it) }
  }
}

/**
 * Create a mapper for Lists
 */
fun  Mapper.toListMapper(): Mapper, List> {
  val singleValueMapper = this
  return ListMapper(singleValueMapper)
}

class ClosureMapper(val closure: (from: From) -> To) : Mapper {

  override fun map(from: From): To = closure(from)
}

/**
 * Mapping method for lists
 */
fun  Mapper.map(values: List): List = values.map { map(it) }

/**
 * Mapping method for Maps
 *
 * @param value A Map of ket-value, where value is typed as "From"
 * @return A Map of mapped values
 */
fun  Mapper.map(value: Map): Map {
  return value.mapValues { map(it.value) }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy