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

scala.build.options.HashedField.scala Maven / Gradle / Ivy

There is a newer version: 0.1.8
Show newest version
package scala.build.options

import shapeless._

trait HashedField[T] {
  def add(name: String, t: T, update: String => Unit): Unit
}

object HashedField extends LowPriorityHashedField {
  def apply[T](implicit instance: HashedField[T]): HashedField[T] = instance

  implicit def asIs[T](implicit hasher: Lazy[HashedType[T]]): HashedField[T] = {
    (name, t, update) =>
      update(s"$name=${hasher.value.hashedValue(t)}")
  }

  implicit def seq[T](implicit hasher: Lazy[HashedType[T]]): HashedField[Seq[T]] = {
    (name, seq, update) =>
      for (t <- seq)
        update(s"$name+=${hasher.value.hashedValue(t)}")
  }

  implicit def list[T](implicit hasher: Lazy[HashedType[T]]): HashedField[List[T]] = {
    (name, list, update) =>
      for (t <- list)
        update(s"$name+=${hasher.value.hashedValue(t)}")
  }

  implicit def option[T](implicit hasher: Lazy[HashedType[T]]): HashedField[Option[T]] = {
    (name, opt, update) =>
      for (t <- opt)
        update(s"$name=${hasher.value.hashedValue(t)}")
  }

  implicit def set[T](implicit
    hasher: Lazy[HashedType[T]],
    ordering: Ordering[T]
  ): HashedField[Set[T]] = {
    (name, set, update) =>
      for (t <- set.toVector.sorted(ordering))
        update(s"$name+=${hasher.value.hashedValue(t)}")
  }

  implicit def map[K, V](
    implicit
    hasherK: Lazy[HashedType[K]],
    hasherV: Lazy[HashedType[V]],
    ordering: Ordering[K]
  ): HashedField[Map[K, V]] = {
    (name, map0, update) =>
      for ((k, v) <- map0.toVector.sortBy(_._1)(ordering))
        update(s"$name+=${hasherK.value.hashedValue(k)}${hasherV.value.hashedValue(v)}")
  }

}

abstract class LowPriorityHashedField {

  implicit def recurse[T](implicit hasher: HasHashData[T]): HashedField[T] = {
    (name, t, update) =>
      hasher.add(name + ".", t, update)
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy