Skip to content

Commit

Permalink
Merge pull request #207 from tgodzik/update-scalafmt
Browse files Browse the repository at this point in the history
chore: Update scalafmt to latest 3.8.1
  • Loading branch information
tgodzik authored Jul 25, 2024
2 parents 98e30c4 + ddca851 commit a7f2aa7
Show file tree
Hide file tree
Showing 54 changed files with 545 additions and 517 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# scalafmt changes
b128c8a617dfc391b17e0c6422bc07bf4ad9356b
15 changes: 9 additions & 6 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
version = "2.4.2"
version = "3.8.1"
assumeStandardLibraryStripMargin = true
align = none
align.preset = none
project.git = true
newlines.inInterpolation = avoid

project.excludeFilters = [
"(.*)/src/main/scala-3/(.*)",
"(.*)/src/test/scala-3/(.*)"
]
runner.dialect = scala213
fileOverride {
"glob:**/scala-3*/**" {
runner.dialect = scala3
}
}
6 changes: 4 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ commands += Command.command("taskready") { s =>
}

lazy val warnUnusedImport = Def.setting {
if (scalaVersion.value.startsWith("2.13") ||
scalaVersion.value.startsWith("3.")) "-Wunused:imports"
if (
scalaVersion.value.startsWith("2.13") ||
scalaVersion.value.startsWith("3.")
) "-Wunused:imports"
else "-Ywarn-unused-import"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ class Macros(val c: blackbox.Context) {
val fallback = q"tmp.$getter"
q"conf.getSettingOrElse[$P](settings.unsafeGet($name), $fallback)"
}
val product = params.foldLeft(next(head)) {
case (accum, param) => q"$accum.product(${next(param)})"
val product = params.foldLeft(next(head)) { case (accum, param) =>
q"$accum.product(${next(param)})"
}
val tupleExtract = 1.to(params.length).foldLeft(q"t": Tree) {
case (accum, _) => q"$accum._1"
Expand Down Expand Up @@ -168,8 +168,8 @@ class Macros(val c: blackbox.Context) {
val fallback = q"tmp.$getter"
q"Conf.getSettingEx[$P]($fallback, conf, settings.unsafeGet($name))"
}
val product = params.foldLeft(next(head)) {
case (accum, param) => q"$accum.product(${next(param)})"
val product = params.foldLeft(next(head)) { case (accum, param) =>
q"$accum.product(${next(param)})"
}
val tupleExtract = 1.to(params.length).foldLeft(q"t": Tree) {
case (accum, _) => q"$accum._1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ private[generic] def deriveCodecImpl[T: Type](default: Expr[T])(using Quotes) =
private[generic] def deriveEncoderImpl[T](using tp: Type[T])(using q: Quotes) =
import q.reflect.*
assumeCaseClass[T]
val str = Expr("Show: " + Type.show[T])
val encoders = params[T]
val fields = paramNames[T]
val str = Expr("Show: " + Type.show[T])
val encoders = params[T]
val fields = paramNames[T]
val ev: Expr[Mirror.Of[T]] = Expr.summon[Mirror.Of[T]].get
'{
new metaconfig.ConfEncoder[T]:
Expand All @@ -64,7 +64,7 @@ private[generic] def deriveConfDecoderImpl[T: Type](default: Expr[T])(using
import q.reflect.*
assumeCaseClass[T]

val cls = Expr(Type.show[T])
val cls = Expr(Type.show[T])
val clsTpt = TypeRepr.of[T]
val settings =
Expr.summon[Settings[T]] match
Expand All @@ -77,8 +77,8 @@ private[generic] def deriveConfDecoderImpl[T: Type](default: Expr[T])(using
def next(p: ValDef): Expr[Conf => Configured[Any]] =
p.tpt.tpe.asType match
case '[t] =>
val name = Expr(p.name)
val getter = clsTpt.classSymbol.get.declaredField(p.name)
val name = Expr(p.name)
val getter = clsTpt.classSymbol.get.declaredField(p.name)
val fallback = Select(default.asTerm, getter).asExprOf[t]
val dec = Expr
.summon[ConfDecoder[t]]
Expand All @@ -99,7 +99,7 @@ private[generic] def deriveConfDecoderImpl[T: Type](default: Expr[T])(using

if paramss.head.isEmpty then '{ ConfDecoder.constant($default) }
else
val (head :: params) :: Nil = paramss : @unchecked
val (head :: params) :: Nil = paramss: @unchecked
val vds = paramss.head.map(_.tree).collect { case vd: ValDef =>
next(vd)
}
Expand Down Expand Up @@ -134,7 +134,7 @@ private[generic] def deriveConfDecoderExImpl[T: Type](default: Expr[T])(using
import q.reflect.*
assumeCaseClass[T]

val cls = Expr(Type.show[T])
val cls = Expr(Type.show[T])
val clsTpt = TypeRepr.of[T]
val settings =
Expr.summon[Settings[T]] match
Expand All @@ -147,7 +147,7 @@ private[generic] def deriveConfDecoderExImpl[T: Type](default: Expr[T])(using
def next(p: ValDef): Expr[(Conf, T) => Configured[Any]] =
p.tpt.tpe.asType match
case '[t] =>
val name = Expr(p.name)
val name = Expr(p.name)
val getter = clsTpt.classSymbol.get.declaredField(p.name)
val fallback = (from: Expr[Any]) =>
Select(from.asTerm, getter).asExprOf[t]
Expand Down Expand Up @@ -176,7 +176,7 @@ private[generic] def deriveConfDecoderExImpl[T: Type](default: Expr[T])(using
Configured.Ok(state.getOrElse($default))
}
else
val (head :: params) :: Nil = paramss : @unchecked
val (head :: params) :: Nil = paramss: @unchecked
val vds = paramss.head.map(_.tree).collect { case vd: ValDef =>
next(vd)
}
Expand Down Expand Up @@ -211,7 +211,7 @@ private[generic] def deriveSurfaceImpl[T: Type](using q: Quotes) =
val target = TypeRepr.of[T] match
case at: AppliedType =>
at.tycon.asType match
case '[t] => assumeCaseClass[t]; at.tycon
case '[t] => assumeCaseClass[t]; at.tycon
case other => report.error(at.tycon.show); ???
case other => other

Expand All @@ -232,8 +232,8 @@ private[generic] def deriveSurfaceImpl[T: Type](using q: Quotes) =
) =>
annot.asExprOf[StaticAnnotation]
}
val isConf = derivesFrom[metaconfig.Conf]
val isMap = derivesFrom[Map[?, ?]]
val isConf = derivesFrom[metaconfig.Conf]
val isMap = derivesFrom[Map[?, ?]]
val isIterable = derivesFrom[Iterable[?]]
val repeated =
if isIterable && !isMap then
Expand Down Expand Up @@ -265,7 +265,7 @@ private[generic] def deriveSurfaceImpl[T: Type](using q: Quotes) =
val underlying: Expr[List[List[Field]]] = vd.tpt.tpe.asType match
case '[t] =>
Expr.summon[Surface[t]] match
case None => '{ Nil }
case None => '{ Nil }
case Some(e) => '{ $e.fields }

val fieldName = Expr(vd.name)
Expand Down Expand Up @@ -301,13 +301,13 @@ end deriveSurfaceImpl

private[generic] def assumeCaseClass[T: Type](using q: Quotes) =
import q.reflect.*
val sym = TypeTree.of[T].symbol
val sym = TypeTree.of[T].symbol
val isCaseClass = sym.isClassDef && sym.flags.is(Flags.Case)
if !isCaseClass then report.error(s"${Type.show[T]} must be a case class")

private[generic] def params[T: Type](using q: Quotes) =
import q.reflect.*
val fields = TypeTree.of[T].symbol.caseFields
val fields = TypeTree.of[T].symbol.caseFields
val encoder = TypeRepr.of[ConfEncoder]
Expr.ofList {
fields.map { f =>
Expand Down
54 changes: 26 additions & 28 deletions metaconfig-core/shared/src/main/scala/metaconfig/Conf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ sealed abstract class Conf extends Product with Serializable {
final override def toString: String = show
def as[T](implicit ev: ConfDecoder[T]): Configured[T] =
ev.read(this)
def getSettingOrElse[T](setting: Setting, default: T)(
implicit ev: ConfDecoder[T]
def getSettingOrElse[T](setting: Setting, default: T)(implicit
ev: ConfDecoder[T]
): Configured[T] =
ConfGet.getOrElse(this, default, setting.name, setting.alternativeNames: _*)
def get[T](path: String, extraNames: String*)(
implicit ev: ConfDecoder[T]
def get[T](path: String, extraNames: String*)(implicit
ev: ConfDecoder[T]
): Configured[T] =
ConfGet.get(this, path, extraNames: _*)
def getOrElse[T](path: String, extraNames: String*)(
Expand Down Expand Up @@ -64,8 +64,8 @@ object Conf {
string: String
)(implicit parser: MetaconfigParser): Configured[Conf] =
Input.String(string).parse
def parseString(filename: String, string: String)(
implicit parser: MetaconfigParser
def parseString(filename: String, string: String)(implicit
parser: MetaconfigParser
): Configured[Conf] =
Input.VirtualFile(filename, string).parse
def parseInput(
Expand All @@ -78,7 +78,7 @@ object Conf {
HoconPrinter.toHocon(value).renderTrim(100)
}

/** Produce a minimal Conf that when merged with original yields revised. **/
/** Produce a minimal Conf that when merged with original yields revised. * */
def patch(original: Conf, revised: Conf): Conf =
ConfPatch.patch(original, revised)

Expand All @@ -104,11 +104,11 @@ object Conf {
def field(key: String): Option[Conf] = map.get(key)
def keys: List[String] = values.map(_._1)
def mapValues(f: Conf => Conf): Obj =
Obj(values.map {
case (k, v) => k -> f(v)
Obj(values.map { case (k, v) =>
k -> f(v)
})
def getOption[T](path: String, extraNames: String*)(
implicit ev: ConfDecoder[T]
def getOption[T](path: String, extraNames: String*)(implicit
ev: ConfDecoder[T]
): Configured[Option[T]] =
ConfGet
.getKey(this, path +: extraNames)
Expand All @@ -120,27 +120,27 @@ object Conf {
def apply(values: (String, Conf)*): Obj = Obj(values.toList)
}

def getEx[A](state: A, conf: Conf, path: Seq[String])(
implicit ev: ConfDecoderEx[A]
def getEx[A](state: A, conf: Conf, path: Seq[String])(implicit
ev: ConfDecoderEx[A]
): Configured[A] =
ConfGet
.getKey(conf, path)
.fold(Configured.ok(state))(ev.read(Some(state), _))

def getSettingEx[A](state: A, conf: Conf, setting: Setting)(
implicit ev: ConfDecoderEx[A]
def getSettingEx[A](state: A, conf: Conf, setting: Setting)(implicit
ev: ConfDecoderEx[A]
): Configured[A] =
getEx(state, conf, setting.name +: setting.alternativeNames)

implicit class ConfImplicit(conf: Conf) {

def getEx[A](state: Option[A])(
implicit ev: ConfDecoderEx[A]
def getEx[A](state: Option[A])(implicit
ev: ConfDecoderEx[A]
): Configured[A] =
ev.read(state, conf)

def getExT[A, B](state: Option[A])(
implicit ev: ConfDecoderExT[A, B]
def getExT[A, B](state: Option[A])(implicit
ev: ConfDecoderExT[A, B]
): Configured[B] =
ev.read(state, conf)

Expand Down Expand Up @@ -195,17 +195,16 @@ object ConfOps {
else
v1.map(_._2)
.zip(v2.map(_._2))
.flatMap {
case (a, b) => diff(a, b)
.flatMap { case (a, b) =>
diff(a, b)
}
.headOption
case (Lst(l1), Lst(l2)) =>
if (l1.lengthCompare(l2.length) != 0) Some(a -> b)
else {
l1.zip(l1)
.flatMap {
case (c1, c2) =>
diff(c1, c2)
.flatMap { case (c1, c2) =>
diff(c1, c2)
}
.headOption
}
Expand Down Expand Up @@ -289,10 +288,9 @@ object ConfOps {
val merged = (v1 ++ v2).foldLeft(Vector.empty[(String, Conf)]) {
case (accumulated, pair @ (key, value2)) =>
accumulated
.collectFirst {
case (`key`, value1) =>
accumulated.filter(_._1 != key) ++
Vector((key, merge(value1, value2)))
.collectFirst { case (`key`, value1) =>
accumulated.filter(_._1 != key) ++
Vector((key, merge(value1, value2)))
}
.getOrElse(pair +: accumulated)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ trait ConfCodec[A] extends ConfDecoder[A] with ConfEncoder[A] { self =>

object ConfCodec {
def apply[A](implicit ev: ConfCodec[A]): ConfCodec[A] = ev
implicit def EncoderDecoderToCodec[A](
implicit
implicit def EncoderDecoderToCodec[A](implicit
encode: ConfEncoder[A],
decode: ConfDecoder[A]
): ConfCodec[A] = new ConfCodec[A] {
Expand Down
27 changes: 13 additions & 14 deletions metaconfig-core/shared/src/main/scala/metaconfig/ConfDecoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ trait ConfDecoder[A] { self =>
final def orElse(other: ConfDecoder[A]): ConfDecoder[A] =
ConfDecoder.orElse(this, other)

/**
* Fail this decoder on unknown fields.
/** Fail this decoder on unknown fields.
*
* By default, a decoder ignores unknown fields. With .noTypos, the decoder
* will fail if an object contains unknown fields, which typically hint the
Expand Down Expand Up @@ -86,8 +85,8 @@ object ConfDecoder {
case Conf.Str(Number(n)) => Ok(n.toInt)
}
implicit val bigDecimalConfDecoder: ConfDecoder[BigDecimal] =
fromPartial[BigDecimal]("Number") {
case Conf.Num(x) => Ok(x)
fromPartial[BigDecimal]("Number") { case Conf.Num(x) =>
Ok(x)
}
implicit val stringConfDecoder: ConfDecoder[String] =
fromPartial[String]("String") { case Conf.Str(x) => Ok(x) }
Expand All @@ -104,8 +103,8 @@ object ConfDecoder {
Configured.fromExceptionThrowing(Paths.get(path))
}

implicit def canBuildFromOption[A](
implicit ev: ConfDecoder[A],
implicit def canBuildFromOption[A](implicit
ev: ConfDecoder[A],
classTag: ClassTag[A]
): ConfDecoder[Option[A]] =
(conf: Conf) =>
Expand All @@ -114,29 +113,29 @@ object ConfDecoder {
case _ => ev.read(conf).map(Some(_))
}

implicit def canBuildEither[A, B](
implicit evA: ConfDecoder[A],
implicit def canBuildEither[A, B](implicit
evA: ConfDecoder[A],
evB: ConfDecoder[B]
): ConfDecoder[Either[A, B]] =
orElse(evA.map(x => Left(x)), evB.map(x => Right(x)))

// XXX: remove this method when MIMA no longer an issue
@deprecated("Use canBuildFromAnyMapWithStringKey instead", "0.9.2")
implicit def canBuildFromMapWithStringKey[A](
implicit ev: ConfDecoder[A],
implicit def canBuildFromMapWithStringKey[A](implicit
ev: ConfDecoder[A],
classTag: ClassTag[A]
): ConfDecoder[Map[String, A]] =
CanBuildFromDecoder.map[A, Map]

implicit def canBuildFromAnyMapWithStringKey[A, CC[_, _]](
implicit ev: ConfDecoder[A],
implicit def canBuildFromAnyMapWithStringKey[A, CC[_, _]](implicit
ev: ConfDecoder[A],
factory: Factory[(String, A), CC[String, A]],
classTag: ClassTag[A]
): ConfDecoder[CC[String, A]] =
CanBuildFromDecoder.map[A, CC]

implicit def canBuildFromConfDecoder[C[_], A](
implicit ev: ConfDecoder[A],
implicit def canBuildFromConfDecoder[C[_], A](implicit
ev: ConfDecoder[A],
factory: Factory[A, C[A]],
classTag: ClassTag[A]
): ConfDecoder[C[A]] =
Expand Down
Loading

0 comments on commit a7f2aa7

Please sign in to comment.