Define OptJsonWriter & put it on AttributeKey

This commit is contained in:
Dale Wijnand 2017-03-21 00:09:07 +00:00
parent c81b8de966
commit ae4e53ceb0
No known key found for this signature in database
GPG Key ID: 4F256E3D151DF5EF
2 changed files with 26 additions and 8 deletions

View File

@ -39,6 +39,8 @@ sealed trait AttributeKey[T] {
/** Identifies the relative importance of a key among other keys.*/
def rank: Int
def optJsonWriter: OptJsonWriter[T]
}
private[sbt] abstract class SharedAttributeKey[T] extends AttributeKey[T] {
override final def toString = label
@ -50,32 +52,33 @@ private[sbt] abstract class SharedAttributeKey[T] extends AttributeKey[T] {
final def isLocal: Boolean = false
}
object AttributeKey {
def apply[T](name: String)(implicit mf: Manifest[T]): AttributeKey[T] =
def apply[T](name: String)(implicit mf: Manifest[T], ojw: OptJsonWriter[T]): AttributeKey[T] =
make(name, None, Nil, Int.MaxValue)
def apply[T](name: String, rank: Int)(implicit mf: Manifest[T]): AttributeKey[T] =
def apply[T](name: String, rank: Int)(implicit mf: Manifest[T], ojw: OptJsonWriter[T]): AttributeKey[T] =
make(name, None, Nil, rank)
def apply[T](name: String, description: String)(implicit mf: Manifest[T]): AttributeKey[T] =
def apply[T](name: String, description: String)(implicit mf: Manifest[T], ojw: OptJsonWriter[T]): AttributeKey[T] =
apply(name, description, Nil)
def apply[T](name: String, description: String, rank: Int)(implicit mf: Manifest[T]): AttributeKey[T] =
def apply[T](name: String, description: String, rank: Int)(implicit mf: Manifest[T], ojw: OptJsonWriter[T]): AttributeKey[T] =
apply(name, description, Nil, rank)
def apply[T](name: String, description: String, extend: Seq[AttributeKey[_]])(implicit mf: Manifest[T]): AttributeKey[T] =
def apply[T](name: String, description: String, extend: Seq[AttributeKey[_]])(implicit mf: Manifest[T], ojw: OptJsonWriter[T]): AttributeKey[T] =
apply(name, description, extend, Int.MaxValue)
def apply[T](name: String, description: String, extend: Seq[AttributeKey[_]], rank: Int)(implicit mf: Manifest[T]): AttributeKey[T] =
def apply[T](name: String, description: String, extend: Seq[AttributeKey[_]], rank: Int)(implicit mf: Manifest[T], ojw: OptJsonWriter[T]): AttributeKey[T] =
make(name, Some(description), extend, rank)
private[this] def make[T](name: String, description0: Option[String], extend0: Seq[AttributeKey[_]], rank0: Int)(implicit mf: Manifest[T]): AttributeKey[T] = new SharedAttributeKey[T] {
private[this] def make[T](name: String, description0: Option[String], extend0: Seq[AttributeKey[_]], rank0: Int)(implicit mf: Manifest[T], ojw: OptJsonWriter[T]): AttributeKey[T] = new SharedAttributeKey[T] {
def manifest = mf
val label = Util.hyphenToCamel(name)
def description = description0
def extend = extend0
def rank = rank0
def optJsonWriter = ojw
}
private[sbt] def local[T](implicit mf: Manifest[T]): AttributeKey[T] = new AttributeKey[T] {
private[sbt] def local[T](implicit mf: Manifest[T], ojw: OptJsonWriter[T]): AttributeKey[T] = new AttributeKey[T] {
def manifest = mf
def label = LocalLabel
def description = None
@ -83,6 +86,7 @@ object AttributeKey {
override def toString = label
def isLocal: Boolean = true
def rank = Int.MaxValue
val optJsonWriter = ojw
}
private[sbt] final val LocalLabel = "$" + "local"
}

View File

@ -0,0 +1,14 @@
package sbt.internal.util
import sjsonnew.JsonWriter
sealed trait OptJsonWriter[A]
final case class NoJsonWriter[A]() extends OptJsonWriter[A]
final case class SomeJsonWriter[A](value: JsonWriter[A]) extends OptJsonWriter[A]
trait OptJsonWriter0 {
implicit def fallback[A]: NoJsonWriter[A] = NoJsonWriter()
}
object OptJsonWriter extends OptJsonWriter0 {
implicit def lift[A](implicit z: JsonWriter[A]): SomeJsonWriter[A] = SomeJsonWriter(z)
}