From 51771b77b786c89983e8d345219975c76a70426a Mon Sep 17 00:00:00 2001 From: David Geirola Date: Wed, 28 Dec 2022 00:37:49 +0100 Subject: [PATCH] Drop macros module using fluent-copy library --- build.sbt | 20 ++--- .../scala-2/fly4s/core/data/Fly4sConfig.scala | 4 +- .../macros/fluentcopy/FluentCopyMacros.scala | 84 ------------------- project/ProjectDependencies.scala | 7 +- 4 files changed, 12 insertions(+), 103 deletions(-) delete mode 100644 macros/src/main/scala-2/com/geirolz/macros/fluentcopy/FluentCopyMacros.scala diff --git a/build.sbt b/build.sbt index 46722a0..e979f86 100644 --- a/build.sbt +++ b/build.sbt @@ -34,25 +34,21 @@ lazy val fly4s: Project = project .settings( crossScalaVersions := Nil ) - .aggregate(core, macros) + .aggregate(core) lazy val core: Project = buildModule( prjModuleName = "core", toPublish = true, folder = "." - ).dependsOn(macros) - .settings( - libraryDependencies ++= ProjectDependencies.Core.dedicated - ) - -lazy val macros: Project = - buildModule( - prjModuleName = "macros", - toPublish = true, - folder = "." ).settings( - libraryDependencies ++= ProjectDependencies.Macros.dedicated + libraryDependencies ++= ProjectDependencies.Core.dedicated, + libraryDependencies ++= { + CrossVersion.partialVersion(Keys.scalaVersion.value) match { + case Some((2, _)) => ProjectDependencies.Core.for2_13_Only + case _ => Nil + } + } ) //=============================== MODULES UTILS =============================== diff --git a/core/src/main/scala-2/fly4s/core/data/Fly4sConfig.scala b/core/src/main/scala-2/fly4s/core/data/Fly4sConfig.scala index da7edec..acd49fe 100644 --- a/core/src/main/scala-2/fly4s/core/data/Fly4sConfig.scala +++ b/core/src/main/scala-2/fly4s/core/data/Fly4sConfig.scala @@ -1,12 +1,12 @@ package fly4s.core.data import cats.data.NonEmptyList -import com.geirolz.macros.fluentcopy.FluentCopyMacros.FluentCopy +import com.geirolz.macros.fluent.copy.FluentCopy import fly4s.core.data.Fly4sConfigDefaults.* import java.nio.charset.Charset -@FluentCopy +@FluentCopy(collection = true) case class Fly4sConfig( connectRetries: Int = defaultConnectRetries, initSql: Option[String] = defaultInitSql, diff --git a/macros/src/main/scala-2/com/geirolz/macros/fluentcopy/FluentCopyMacros.scala b/macros/src/main/scala-2/com/geirolz/macros/fluentcopy/FluentCopyMacros.scala deleted file mode 100644 index 5cbf28d..0000000 --- a/macros/src/main/scala-2/com/geirolz/macros/fluentcopy/FluentCopyMacros.scala +++ /dev/null @@ -1,84 +0,0 @@ -package com.geirolz.macros.fluentcopy - -import scala.annotation.{compileTimeOnly, StaticAnnotation} -import scala.reflect.api.Trees -import scala.reflect.macros.whitebox - -object FluentCopyMacros { - - @compileTimeOnly("enable macro paradise") - class FluentCopy extends StaticAnnotation { - def macroTransform(annottees: Any*): Any = macro Macro.impl - } - - object Macro { - def impl(c: whitebox.Context)(annottees: c.Tree*): c.Expr[Any] = { - import c.universe.* - - def extractClassNameAndFields(classDecl: ClassDef): (c.TypeName, List[Trees#Tree]) = - try { - val q"case class $className(..$fields) extends ..$_ { ..$_ }" = classDecl - (className, fields.toList) - } catch { - case _: MatchError => - c.abort(c.enclosingPosition, "Annotation is only supported on case class") - } - - def modifiedCompanion( - compDeclOpt: Option[ModuleDef], - toAdd: c.Tree, - className: TypeName - ): c.universe.Tree = { - compDeclOpt map { compDecl => - val q"object $obj extends ..$bases { ..$body }" = compDecl - q""" - object $obj extends ..$bases { - ..$body - $toAdd - } - """ - } getOrElse { - // Create a companion object with the formatter - q"object ${className.toTermName} { $toAdd }" - } - } - - def implicitFluentCopyOps(className: c.TypeName, fields: List[Trees#Tree]): c.Tree = { - - val newMethods = fields.map { case q"$_ val $tname: $tpt = $_" => - val methodName = TermName(s"with${tname.toString().capitalize}") - q"def $methodName($tname: $tpt): $className = i.copy($tname = $tname)" - } - - val opsName: c.universe.TypeName = TypeName(s"${className.toTermName}FluentConfigOps") - q""" - implicit class $opsName(i: $className){ - ..$newMethods - } - """ - } - - def modifiedDeclaration( - classDecl: ClassDef, - compDeclOpt: Option[ModuleDef] = None - ): c.Expr[Any] = { - val (className, fields) = extractClassNameAndFields(classDecl) - val ops: c.Tree = implicitFluentCopyOps(className, fields) - val compDecl: c.Tree = modifiedCompanion(compDeclOpt, ops, className) - - // Return both the class and companion object declarations - c.Expr(q""" - $classDecl - $compDecl - """) - } - - annottees match { - case (classDecl: ClassDef) :: Nil => modifiedDeclaration(classDecl) - case (classDecl: ClassDef) :: (compDecl: ModuleDef) :: Nil => - modifiedDeclaration(classDecl, Some(compDecl)) - case _ => c.abort(c.enclosingPosition, "Invalid annottee") - } - } - } -} diff --git a/project/ProjectDependencies.scala b/project/ProjectDependencies.scala index 02ae773..4ce0740 100644 --- a/project/ProjectDependencies.scala +++ b/project/ProjectDependencies.scala @@ -6,7 +6,6 @@ object ProjectDependencies { "org.typelevel" %% "cats-core" % "2.9.0", "org.typelevel" %% "cats-effect" % "3.4.3", "org.flywaydb" % "flyway-core" % "9.1.5", - // test "org.scalameta" %% "munit" % "0.7.29" % Test, "org.typelevel" %% "munit-cats-effect-3" % "1.0.7" % Test @@ -25,11 +24,9 @@ object ProjectDependencies { lazy val dedicated: Seq[ModuleID] = Seq( "com.h2database" % "h2" % "2.1.214" % Test ) - } - object Macros { - lazy val dedicated: Seq[ModuleID] = Seq( - "org.scala-lang" % "scala-reflect" % "2.13.10" + lazy val for2_13_Only: Seq[ModuleID] = Seq( + "com.github.geirolz" %% "fluent-copy" % "0.0.1" ) } }