Implements registerStringCodec

Uses TypeTag to recover the full name of type parameter, which is calculated by StringTypeTag. This is sent along in ObjectEvent.
On the other end, we can lookup typeclass instances using the tag key.
This commit is contained in:
Eugene Yokota 2017-01-30 19:20:06 -05:00 committed by Dale Wijnand
parent 52bb767bc8
commit 73b24681c5
No known key found for this signature in database
GPG Key ID: 4F256E3D151DF5EF
6 changed files with 26 additions and 18 deletions

View File

@ -95,7 +95,7 @@ abstract class EvaluateSettings[Scope] {
keyString keyString
private[this] def keyString = private[this] def keyString =
(static.toSeq.flatMap { case (key, value) => if (value eq this) init.showFullKey(key) :: Nil else Nil }).headOption getOrElse "non-static" (static.toSeq.flatMap { case (key, value) => if (value eq this) init.showFullKey.show(key) :: Nil else Nil }).headOption getOrElse "non-static"
final def get: T = synchronized { final def get: T = synchronized {
assert(value != null, toString + " not evaluated") assert(value != null, toString + " not evaluated")

View File

@ -6,6 +6,7 @@ package sbt.internal.util
import scala.language.existentials import scala.language.existentials
import Types._ import Types._
import sbt.util.Show
sealed trait Settings[Scope] { sealed trait Settings[Scope] {
def data: Map[Scope, AttributeMap] def data: Map[Scope, AttributeMap]
@ -119,7 +120,7 @@ trait Init[Scope] {
def mapScope(f: Scope => Scope): MapScoped = new MapScoped { def mapScope(f: Scope => Scope): MapScoped = new MapScoped {
def apply[T](k: ScopedKey[T]): ScopedKey[T] = k.copy(scope = f(k.scope)) def apply[T](k: ScopedKey[T]): ScopedKey[T] = k.copy(scope = f(k.scope))
} }
private final class InvalidReference(val key: ScopedKey[_]) extends RuntimeException("Internal settings error: invalid reference to " + showFullKey(key)) private final class InvalidReference(val key: ScopedKey[_]) extends RuntimeException("Internal settings error: invalid reference to " + showFullKey.show(key))
private[this] def applyDefaults(ss: Seq[Setting[_]]): Seq[Setting[_]] = private[this] def applyDefaults(ss: Seq[Setting[_]]): Seq[Setting[_]] =
{ {
@ -215,11 +216,11 @@ trait Init[Scope] {
{ {
val guessed = guessIntendedScope(validKeys, delegates, u.referencedKey) val guessed = guessIntendedScope(validKeys, delegates, u.referencedKey)
val derived = u.defining.isDerived val derived = u.defining.isDerived
val refString = display(u.defining.key) val refString = display.show(u.defining.key)
val sourceString = if (derived) "" else parenPosString(u.defining) val sourceString = if (derived) "" else parenPosString(u.defining)
val guessedString = if (derived) "" else guessed.map(g => "\n Did you mean " + display(g) + " ?").toList.mkString val guessedString = if (derived) "" else guessed.map(g => "\n Did you mean " + display.show(g) + " ?").toList.mkString
val derivedString = if (derived) ", which is a derived setting that needs this key to be defined in this scope." else "" val derivedString = if (derived) ", which is a derived setting that needs this key to be defined in this scope." else ""
display(u.referencedKey) + " from " + refString + sourceString + derivedString + guessedString display.show(u.referencedKey) + " from " + refString + sourceString + derivedString + guessedString
} }
private[this] def parenPosString(s: Setting[_]): String = private[this] def parenPosString(s: Setting[_]): String =
s.positionString match { case None => ""; case Some(s) => " (" + s + ")" } s.positionString match { case None => ""; case Some(s) => " (" + s + ")" }
@ -255,7 +256,7 @@ trait Init[Scope] {
new Uninitialized(keys, prefix + suffix + " to undefined setting" + suffix + ": " + keysString + "\n ") new Uninitialized(keys, prefix + suffix + " to undefined setting" + suffix + ": " + keysString + "\n ")
} }
final class Compiled[T](val key: ScopedKey[T], val dependencies: Iterable[ScopedKey[_]], val settings: Seq[Setting[T]]) { final class Compiled[T](val key: ScopedKey[T], val dependencies: Iterable[ScopedKey[_]], val settings: Seq[Setting[T]]) {
override def toString = showFullKey(key) override def toString = showFullKey.show(key)
} }
final class Flattened(val key: ScopedKey[_], val dependencies: Iterable[ScopedKey[_]]) final class Flattened(val key: ScopedKey[_], val dependencies: Iterable[ScopedKey[_]])

View File

@ -1,8 +0,0 @@
package sbt.internal.util
trait Show[T] {
def apply(t: T): String
}
object Show {
def apply[T](f: T => String): Show[T] = new Show[T] { def apply(t: T): String = f(t) }
}

View File

@ -0,0 +1,12 @@
package sbt.util
trait Show[A] {
def show(a: A): String
}
object Show {
def apply[A](f: A => String): Show[A] = new Show[A] { def show(a: A): String = f(a) }
def fromToString[A]: Show[A] = new Show[A] {
def show(a: A): String = a.toString
}
}

View File

@ -1,4 +1,4 @@
package sbt.internal.util package sbt.util
trait ShowLines[A] { trait ShowLines[A] {
def showLines(a: A): Seq[String] def showLines(a: A): Seq[String]

View File

@ -1,5 +1,7 @@
package sbt.internal.util package sbt.internal.util
import sbt.util.Show
/** Define our settings system */ /** Define our settings system */
// A basic scope indexed by an integer. // A basic scope indexed by an integer.
@ -12,9 +14,10 @@ final case class Scope(nestIndex: Int, idAtIndex: Int = 0)
// That would be a general pain.) // That would be a general pain.)
case class SettingsExample() extends Init[Scope] { case class SettingsExample() extends Init[Scope] {
// Provides a way of showing a Scope+AttributeKey[_] // Provides a way of showing a Scope+AttributeKey[_]
val showFullKey: Show[ScopedKey[_]] = new Show[ScopedKey[_]] { val showFullKey: Show[ScopedKey[_]] = Show[ScopedKey[_]]((key: ScopedKey[_]) =>
def apply(key: ScopedKey[_]) = s"${key.scope.nestIndex}(${key.scope.idAtIndex})/${key.key.label}" {
} s"${key.scope.nestIndex}(${key.scope.idAtIndex})/${key.key.label}"
})
// A sample delegation function that delegates to a Scope with a lower index. // A sample delegation function that delegates to a Scope with a lower index.
val delegates: Scope => Seq[Scope] = { val delegates: Scope => Seq[Scope] = {