aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Pretty <jon.pretty@propensive.com>2018-01-07 13:11:26 +0100
committerGitHub <noreply@github.com>2018-01-07 13:11:26 +0100
commit4fe64bd5ba7bdc1ea0e29923b27463c62a3c4052 (patch)
tree16b3d535475f73a9aaa1da4dfa6a08429f8cbe70
parenta5a3f0487a4da5ec18a7f8afe83bc2255a574126 (diff)
parentee6e6e7cdd390736900c998a84b20366f76d1043 (diff)
downloadmagnolia-4fe64bd5ba7bdc1ea0e29923b27463c62a3c4052.tar.gz
magnolia-4fe64bd5ba7bdc1ea0e29923b27463c62a3c4052.tar.bz2
magnolia-4fe64bd5ba7bdc1ea0e29923b27463c62a3c4052.zip
Merge pull request #72 from propensive/scalafmt
Run Scala-fmt
-rw-r--r--benchmarks/2.12/src/adt1.scala63
-rw-r--r--core/shared/src/main/scala/globalutil.scala9
-rw-r--r--core/shared/src/main/scala/interface.scala5
-rw-r--r--core/shared/src/main/scala/magnolia.scala100
-rw-r--r--examples/shared/src/main/scala/patch.scala11
-rw-r--r--examples/shared/src/main/scala/typename.scala1
-rw-r--r--tests/src/main/scala/tests.scala35
7 files changed, 124 insertions, 100 deletions
diff --git a/benchmarks/2.12/src/adt1.scala b/benchmarks/2.12/src/adt1.scala
index f9ab65f..2efbdd4 100644
--- a/benchmarks/2.12/src/adt1.scala
+++ b/benchmarks/2.12/src/adt1.scala
@@ -20,40 +20,45 @@ import Scalaz._
@deriving(Show, Equal) sealed trait Alphabet
@deriving(Show, Equal) case class Greek(άλφα: Letter,
- βήτα: Letter,
- γάμα: Letter,
- δέλτα: Letter,
- έψιλον: Letter,
- ζήτα: Letter,
- ήτα: Letter,
- θήτα: Letter)
+ βήτα: Letter,
+ γάμα: Letter,
+ δέλτα: Letter,
+ έψιλον: Letter,
+ ζήτα: Letter,
+ ήτα: Letter,
+ θήτα: Letter)
extends Alphabet
-@deriving(Show, Equal) case class Cyrillic(б: Letter, в: Letter, г: Letter, д: Letter, ж: Letter, з: Letter)
+@deriving(Show, Equal) case class Cyrillic(б: Letter,
+ в: Letter,
+ г: Letter,
+ д: Letter,
+ ж: Letter,
+ з: Letter)
extends Alphabet
@deriving(Show, Equal) case class Latin(a: Letter,
- b: Letter,
- c: Letter,
- d: Letter,
- e: Letter,
- f: Letter,
- g: Letter,
- h: Letter,
- i: Letter,
- j: Letter,
- k: Letter,
- l: Letter,
- m: Letter,
- n: Letter,
- o: Letter,
- p: Letter,
- q: Letter,
- r: Letter,
- s: Letter,
- t: Letter,
- u: Letter,
- v: Letter)
+ b: Letter,
+ c: Letter,
+ d: Letter,
+ e: Letter,
+ f: Letter,
+ g: Letter,
+ h: Letter,
+ i: Letter,
+ j: Letter,
+ k: Letter,
+ l: Letter,
+ m: Letter,
+ n: Letter,
+ o: Letter,
+ p: Letter,
+ q: Letter,
+ r: Letter,
+ s: Letter,
+ t: Letter,
+ u: Letter,
+ v: Letter)
extends Alphabet
//@deriving(Show, Equal) case class Letter(name: String, phonetic: String)
diff --git a/core/shared/src/main/scala/globalutil.scala b/core/shared/src/main/scala/globalutil.scala
index 99dee77..5c11f79 100644
--- a/core/shared/src/main/scala/globalutil.scala
+++ b/core/shared/src/main/scala/globalutil.scala
@@ -20,7 +20,7 @@ private[magnolia] object GlobalUtil {
val original = globalType.typeSymbol
val owner = original.owner
val companion = original.companion.orElse {
- import global.{ abort => aabort, _ }
+ import global.{abort => aabort, _}
implicit class PatchedContext(ctx: global.analyzer.Context) {
trait PatchedLookupResult { def suchThat(criterion: Symbol => Boolean): Symbol }
def patchedLookup(name: Name, expectedOwner: Symbol) = new PatchedLookupResult {
@@ -34,9 +34,12 @@ private[magnolia] object GlobalUtil {
val s = {
val lookupResult = ctx.scope.lookupAll(name).filter(criterion).toList
lookupResult match {
- case Nil => NoSymbol
+ case Nil => NoSymbol
case List(unique) => unique
- case _ => aabort(s"unexpected multiple results for a companion symbol lookup for $original#{$original.id}")
+ case _ =>
+ aabort(
+ s"unexpected multiple results for a companion symbol lookup for $original#{$original.id}"
+ )
}
}
if (s != NoSymbol && s.owner == expectedOwner)
diff --git a/core/shared/src/main/scala/interface.scala b/core/shared/src/main/scala/interface.scala
index 51abc22..846eee9 100644
--- a/core/shared/src/main/scala/interface.scala
+++ b/core/shared/src/main/scala/interface.scala
@@ -178,7 +178,10 @@ final class SealedTrait[Typeclass[_], Type](val typeName: TypeName,
if (ix < subtypesArray.length) {
val sub = subtypesArray(ix)
if (sub.cast.isDefinedAt(value)) handle(sub) else rec(ix + 1)
- } else throw new IllegalArgumentException(s"The given value `$value` is not a sub type of `$typeName`")
+ } else
+ throw new IllegalArgumentException(
+ s"The given value `$value` is not a sub type of `$typeName`"
+ )
rec(0)
}
}
diff --git a/core/shared/src/main/scala/magnolia.scala b/core/shared/src/main/scala/magnolia.scala
index 3135b57..5318aa2 100644
--- a/core/shared/src/main/scala/magnolia.scala
+++ b/core/shared/src/main/scala/magnolia.scala
@@ -88,9 +88,11 @@ object Magnolia {
}
val typeConstructor = typeDefs.headOption.fold {
- c.abort(c.enclosingPosition,
- s"magnolia: the derivation $prefixObject does not define the Typeclass type constructor")
- } (_.typeConstructor)
+ c.abort(
+ c.enclosingPosition,
+ s"magnolia: the derivation $prefixObject does not define the Typeclass type constructor"
+ )
+ }(_.typeConstructor)
def checkMethod(termName: String, category: String, expected: String): Unit = {
val term = TermName(termName)
@@ -117,7 +119,8 @@ object Magnolia {
val removeDeferred = new Transformer {
override def transform(tree: Tree) = tree match {
case q"$magnolia.Deferred.apply[$_](${Literal(Constant(method: String))})"
- if magnolia.symbol == magnoliaPkg => q"${TermName(method)}"
+ if magnolia.symbol == magnoliaPkg =>
+ q"${TermName(method)}"
case _ =>
super.transform(tree)
}
@@ -134,7 +137,8 @@ object Magnolia {
val path = ChainedImplicit(s"$prefixName.Typeclass", genericType.toString)
val frame = stack.Frame(path, searchType, termNames.EMPTY)
stack.recurse(frame, searchType) {
- Option(c.inferImplicitValue(searchType)).filterNot(_.isEmpty)
+ Option(c.inferImplicitValue(searchType))
+ .filterNot(_.isEmpty)
.orElse(directInferImplicit(genericType, typeConstructor))
.getOrElse {
val missingType = stack.top.fold(searchType)(_.searchType.asInstanceOf[Type])
@@ -142,7 +146,7 @@ object Magnolia {
val genericType = missingType.typeArgs.head
val trace = stack.trace.mkString(" in ", "\n in ", "\n")
c.abort(c.enclosingPosition,
- s"magnolia: could not find $typeClassName for type $genericType\n$trace")
+ s"magnolia: could not find $typeClassName for type $genericType\n$trace")
}
}
}
@@ -170,7 +174,7 @@ object Magnolia {
val isValueClass = genericType <:< typeOf[AnyVal] && !primitives.exists(_ =:= genericType)
val resultType = appliedType(typeConstructor, genericType)
-
+
val typeName = TermName(c.freshName("typeName"))
val typeNameDef = {
val ts = genericType.typeSymbol
@@ -209,19 +213,22 @@ object Magnolia {
false -> tpe
}
- acc.find(_.paramType =:= paramType).fold {
- val path = ProductType(paramName, genericType.toString)
- val frame = stack.Frame(path, resultType, assignedName)
- val derivedImplicit = stack.recurse(frame, appliedType(typeConstructor, paramType)) {
- typeclassTree(paramType, typeConstructor)
- }
+ acc
+ .find(_.paramType =:= paramType)
+ .fold {
+ val path = ProductType(paramName, genericType.toString)
+ val frame = stack.Frame(path, resultType, assignedName)
+ val derivedImplicit =
+ stack.recurse(frame, appliedType(typeConstructor, paramType)) {
+ typeclassTree(paramType, typeConstructor)
+ }
- val ref = TermName(c.freshName("paramTypeclass"))
- val assigned = q"""lazy val $ref = $derivedImplicit"""
- CaseParam(param, repeated, assigned, paramType, ref) :: acc
- } { backRef =>
- CaseParam(param, repeated, q"()", paramType, backRef.ref) :: acc
- }
+ val ref = TermName(c.freshName("paramTypeclass"))
+ val assigned = q"""lazy val $ref = $derivedImplicit"""
+ CaseParam(param, repeated, assigned, paramType, ref) :: acc
+ } { backRef =>
+ CaseParam(param, repeated, q"()", paramType, backRef.ref) :: acc
+ }
}
val caseParams = caseParamsReversed.reverse
@@ -238,7 +245,7 @@ object Magnolia {
// If a companion object is defined with alternative apply methods
// it is needed get all the alternatives
val constructorMethods =
- companionSym.decl(TermName("apply")).alternatives.map(_.asMethod)
+ companionSym.decl(TermName("apply")).alternatives.map(_.asMethod)
// The last apply method in the alternatives is the one that belongs
// to the case class, not the user defined companion object
@@ -280,12 +287,11 @@ object Magnolia {
val msg = "`" + $typeName.full + "` has " + $paramsVal.length + " fields, not " + $fieldValues.size
throw new java.lang.IllegalArgumentException(msg)
}
- new $genericType(..${
- caseParams.zipWithIndex.map { case (typeclass, idx) =>
- val arg = q"$fieldValues($idx).asInstanceOf[${typeclass.paramType}]"
- if (typeclass.repeated) q"$arg: _*" else arg
- }
- })}))
+ new $genericType(..${caseParams.zipWithIndex.map {
+ case (typeclass, idx) =>
+ val arg = q"$fieldValues($idx).asInstanceOf[${typeclass.paramType}]"
+ if (typeclass.repeated) q"$arg: _*" else arg
+ }})}))
}""")
} else if (isSealedTrait) {
val genericSubtypes = classType.get.knownDirectSubclasses.to[List]
@@ -353,23 +359,27 @@ object Magnolia {
val directlyReentrant = stack.top.exists(_.searchType =:= searchType)
if (directlyReentrant) throw DirectlyReentrantException()
- val result = stack.find(searchType).map { enclosingRef =>
- q"$magnoliaPkg.Deferred[$searchType](${enclosingRef.toString})"
- }.orElse {
- directInferImplicit(genericType, typeConstructor)
- }
+ val result = stack
+ .find(searchType)
+ .map { enclosingRef =>
+ q"$magnoliaPkg.Deferred[$searchType](${enclosingRef.toString})"
+ }
+ .orElse {
+ directInferImplicit(genericType, typeConstructor)
+ }
for (tree <- result) if (debug.isDefined && genericType.toString.contains(debug.get)) {
c.echo(c.enclosingPosition, s"Magnolia macro expansion for $genericType")
c.echo(NoPosition, s"... = ${showCode(tree)}\n\n")
}
- val dereferencedResult = if (stack.nonEmpty) result
+ val dereferencedResult =
+ if (stack.nonEmpty) result
else for (tree <- result) yield c.untypecheck(removeDeferred.transform(tree))
dereferencedResult.getOrElse {
c.abort(c.enclosingPosition,
- s"magnolia: could not infer $prefixName.Typeclass for type $genericType")
+ s"magnolia: could not infer $prefixName.Typeclass for type $genericType")
}
}
@@ -377,7 +387,10 @@ object Magnolia {
*
* This method is intended to be called only from code generated by the Magnolia macro, and
* should not be called directly from users' code. */
- def subtype[Tc[_], T, S <: T](name: TypeName, tc: => Tc[S], isType: T => Boolean, asType: T => S): Subtype[Tc, T] =
+ def subtype[Tc[_], T, S <: T](name: TypeName,
+ tc: => Tc[S],
+ isType: T => Boolean,
+ asType: T => S): Subtype[Tc, T] =
new Subtype[Tc, T] with PartialFunction[T, S] {
type SType = S
def typeName: TypeName = name
@@ -460,13 +473,18 @@ private[magnolia] object CompileTimeState {
result.asInstanceOf[T]
}
- def trace: List[TypePath] = frames.drop(1).foldLeft[(C#Type, List[TypePath])]((null, Nil)) {
- case ((_, Nil), frame) =>
- (frame.searchType, frame.path :: Nil)
- case (continue @ (tpe, acc), frame) =>
- if (tpe =:= frame.searchType) continue
- else (frame.searchType, frame.path :: acc)
- }._2.reverse
+ def trace: List[TypePath] =
+ frames
+ .drop(1)
+ .foldLeft[(C#Type, List[TypePath])]((null, Nil)) {
+ case ((_, Nil), frame) =>
+ (frame.searchType, frame.path :: Nil)
+ case (continue @ (tpe, acc), frame) =>
+ if (tpe =:= frame.searchType) continue
+ else (frame.searchType, frame.path :: acc)
+ }
+ ._2
+ .reverse
override def toString: String =
frames.mkString("magnolia stack:\n", "\n", "\n")
diff --git a/examples/shared/src/main/scala/patch.scala b/examples/shared/src/main/scala/patch.scala
index 668a2e2..769b214 100644
--- a/examples/shared/src/main/scala/patch.scala
+++ b/examples/shared/src/main/scala/patch.scala
@@ -30,7 +30,8 @@ object Patcher extends LowerPriorityPatcher {
def patch(value: T, fieldValues: Seq[Any]): T = {
if (fieldValues.lengthCompare(ctx.parameters.size) != 0) {
throw new IllegalArgumentException(
- s"Cannot patch value `$value`, expected ${ctx.parameters.size} fields but got ${fieldValues.size}")
+ s"Cannot patch value `$value`, expected ${ctx.parameters.size} fields but got ${fieldValues.size}"
+ )
}
val effectiveFields = ctx.parameters.zip(fieldValues).map {
case (param, x) => if (x.asInstanceOf[AnyRef] ne null) x else param dereference value
@@ -55,15 +56,17 @@ sealed abstract class LowerPriorityPatcher {
def patch(value: Any, fieldValues: Seq[Any]): Any = {
if (fieldValues.lengthCompare(1) != 0)
throw new IllegalArgumentException(
- s"Cannot patch single value `$value` with patch sequence of size ${fieldValues.size}")
+ s"Cannot patch single value `$value` with patch sequence of size ${fieldValues.size}"
+ )
val head = fieldValues.head
if (head.getClass != value.getClass)
throw new IllegalArgumentException(
- s"Illegal patch value type. Expected `${value.getClass}` but got `${head.getClass}`")
+ s"Illegal patch value type. Expected `${value.getClass}` but got `${head.getClass}`"
+ )
head
}
}
// once https://github.com/propensive/magnolia/issues/58 is fixed this can be marked `implicit`
def forSingleValue[T]: Patcher[T] = _forSingleValue.asInstanceOf[Patcher[T]]
-} \ No newline at end of file
+}
diff --git a/examples/shared/src/main/scala/typename.scala b/examples/shared/src/main/scala/typename.scala
index 61fab1d..2030cda 100644
--- a/examples/shared/src/main/scala/typename.scala
+++ b/examples/shared/src/main/scala/typename.scala
@@ -16,4 +16,3 @@ object TypeNameInfo {
implicit def gen[T]: TypeNameInfo[T] = macro Magnolia.gen[T]
}
-
diff --git a/tests/src/main/scala/tests.scala b/tests/src/main/scala/tests.scala
index 7840133..a311291 100644
--- a/tests/src/main/scala/tests.scala
+++ b/tests/src/main/scala/tests.scala
@@ -67,7 +67,6 @@ case class Portfolio(companies: Company*)
case class Recursive(children: Seq[Recursive])
-
object Tests extends TestApp {
def tests(): Unit = for (_ <- 1 to 1) {
@@ -181,8 +180,7 @@ object Tests extends TestApp {
case class Beta(alpha: Alpha)
Show.gen[Beta]
"""
- }.assert(_ == TypecheckError(
- txt"""magnolia: could not find Show.Typeclass for type Double
+ }.assert(_ == TypecheckError(txt"""magnolia: could not find Show.Typeclass for type Double
| in parameter 'integer' of product type Alpha
| in parameter 'alpha' of product type Beta
|"""))
@@ -192,8 +190,7 @@ object Tests extends TestApp {
case class Gamma(unit: Unit)
Show.gen[Gamma]
"""
- }.assert(_ == TypecheckError(
- txt"""magnolia: could not find Show.Typeclass for type Unit
+ }.assert(_ == TypecheckError(txt"""magnolia: could not find Show.Typeclass for type Unit
| in parameter 'unit' of product type Gamma
|"""))
@@ -221,12 +218,10 @@ object Tests extends TestApp {
// LabelledBox being invariant in L <: String prohibits the derivation for LabelledBox[Int, _]
test("can't show a Box with invariant label") {
scalac"Show.gen[Box[Int]]"
- }.assert { _ == TypecheckError(
- txt"""magnolia: could not find Show.Typeclass for type L
+ }.assert { _ == TypecheckError(txt"""magnolia: could not find Show.Typeclass for type L
| in parameter 'label' of product type magnolia.tests.LabelledBox[Int, _ <: String]
| in coproduct type magnolia.tests.Box[Int]
- |""")
- }
+ |""") }
test("patch a Person via a Patcher[Entity]") {
// these two implicits can be removed once https://github.com/propensive/magnolia/issues/58 is closed
@@ -234,8 +229,7 @@ object Tests extends TestApp {
implicit val intPatcher = Patcher.forSingleValue[Int]
val person = Person("Bob", 42)
- implicitly[Patcher[Entity]]
- .patch(person, Seq(null, 21))
+ implicitly[Patcher[Entity]].patch(person, Seq(null, 21))
}.assert(_ == Person("Bob", 21))
test("throw on an illegal patch attempt with field count mismatch") {
@@ -245,8 +239,7 @@ object Tests extends TestApp {
try {
val person = Person("Bob", 42)
- implicitly[Patcher[Entity]]
- .patch(person, Seq(null, 21, 'killer))
+ implicitly[Patcher[Entity]].patch(person, Seq(null, 21, 'killer))
} catch {
case NonFatal(e) => e.getMessage
}
@@ -259,8 +252,7 @@ object Tests extends TestApp {
try {
val person = Person("Bob", 42)
- implicitly[Patcher[Entity]]
- .patch(person, Seq(null, 'killer))
+ implicitly[Patcher[Entity]].patch(person, Seq(null, 'killer))
} catch {
case NonFatal(e) => e.getMessage
}
@@ -323,7 +315,9 @@ object Tests extends TestApp {
}.assert(_.full == "magnolia.tests.Color")
test("case class typeName should be complete and unchanged") {
- implicit val stringTypeName: TypeNameInfo[String] = new TypeNameInfo[String] { def name = ??? }
+ implicit val stringTypeName: TypeNameInfo[String] = new TypeNameInfo[String] {
+ def name = ???
+ }
TypeNameInfo.gen[Fruit].name
}.assert(_.full == "magnolia.tests.Fruit")
@@ -331,13 +325,11 @@ object Tests extends TestApp {
scalac"""
Show.gen[(Int, Seq[(Long, String)])]
"""
- } assert { _ == TypecheckError(
- txt"""magnolia: could not find Show.Typeclass for type Long
+ } assert { _ == TypecheckError(txt"""magnolia: could not find Show.Typeclass for type Long
| in parameter '_1' of product type (Long, String)
| in chained implicit Show.Typeclass for type Seq[(Long, String)]
| in parameter '_2' of product type (Int, Seq[(Long, String)])
- |""")
- }
+ |""") }
test("show a recursive case class") {
Show.gen[Recursive].show(Recursive(Seq(Recursive(Nil))))
@@ -355,7 +347,8 @@ object Tests extends TestApp {
test("dependencies between derived type classes") {
implicit def showDefaultOption[A](
- implicit showA: Show[String, A], defaultA: Default[A]
+ implicit showA: Show[String, A],
+ defaultA: Default[A]
): Show[String, Option[A]] = new Show[String, Option[A]] {
def show(optA: Option[A]) = showA.show(optA.getOrElse(defaultA.default))
}