Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: Remove optional braces for Scala 3 code #3397

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ optIn.breaksInsideChains = true
docstrings.blankFirstLine = yes

rewrite.scala3.convertToNewSyntax = true
rewrite.scala3.removeOptionalBraces = yes
// Rewrite import _ to *
runner.dialectOverride.allowStarWildcardImport = true
// Disable rewrite import => to 'as'
Expand All @@ -22,4 +23,8 @@ fileOverride {
"glob:**/scala-2/**" {
runner.dialect = scala213source3
}
"glob:**/scala-3/wvlet/log/LoggerBase.scala" {
// optional brace didn't work for code with inline/macro methods
rewrite.scala3.removeOptionalBraces = no
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ package wvlet.airframe.codec
import wvlet.airframe.surface.Surface
import wvlet.airframe.surface.reflect.ReflectSurfaceFactory

trait CompatBase {
inline def codecOf[A]: MessageCodec[A] = {
trait CompatBase:
inline def codecOf[A]: MessageCodec[A] =
MessageCodecFactory.defaultFactory.of[A]
}
// TODO Remove this method usage as runtime-reflection in Scala 3 is unstable and slow
def surfaceOfClass(cl: Class[?]): Surface = ReflectSurfaceFactory.ofClass(cl)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
*/
package wvlet.airframe.codec

trait AnyCodecCompat {
protected def isEnum(o: Any): Boolean = {
o match {
trait AnyCodecCompat:
protected def isEnum(o: Any): Boolean =
o match
case _: scala.runtime.EnumValue => true
case _ => false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,21 @@ package wvlet.airframe.codec

import wvlet.airframe.surface.Surface

object ScalaCompat {
object ScalaCompat:

trait MessageCodecBase {
inline def of[A]: MessageCodec[A] = {
trait MessageCodecBase:
inline def of[A]: MessageCodec[A] =
MessageCodec.ofSurface(Surface.of[A]).asInstanceOf[MessageCodec[A]]
}
inline def fromJson[A](json: String): A = {
inline def fromJson[A](json: String): A =
MessageCodecFactory.defaultFactory.fromJson[A](json)
}
inline def toJson[A](obj: A): String = {
inline def toJson[A](obj: A): String =
MessageCodecFactory.defaultFactory.toJson[A](obj)
}
}

trait MessageCodecFactoryBase { self: MessageCodecFactory =>
inline def of[A]: MessageCodec[A] = {
trait MessageCodecFactoryBase:
self: MessageCodecFactory =>
inline def of[A]: MessageCodec[A] =
self.ofSurface(Surface.of[A]).asInstanceOf[MessageCodec[A]]
}
inline def fromJson[A](json: String): A = {
inline def fromJson[A](json: String): A =
MessageCodec.of[A].fromJson(json)
}
inline def toJson[A](obj: A): String = {
inline def toJson[A](obj: A): String =
MessageCodec.of[A].toJson(obj)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,70 +16,55 @@ package wvlet.airframe.config
import wvlet.airframe.{Design, SourceCode}
import wvlet.airframe.surface.Surface

trait ConfigPackageCompat { self: ConfigurableDesign =>
inline def bindConfig[A](config: A)(implicit sourceCode: SourceCode): Design = {
trait ConfigPackageCompat:
self: ConfigurableDesign =>
inline def bindConfig[A](config: A)(implicit sourceCode: SourceCode): Design =
self.bindConfigInternal[A](Surface.of[A], config)(sourceCode)
}

inline def bindConfigFromYaml[A](yamlFile: String)(implicit sourceCode: SourceCode): Design = {
inline def bindConfigFromYaml[A](yamlFile: String)(implicit sourceCode: SourceCode): Design =
self.bindConfigFromYamlInternal[A](Surface.of[A], yamlFile)(sourceCode)
}

inline def bindConfigFromYaml[A](yamlFile: String, defaultValue: => A)(implicit
sourceCode: SourceCode
): Design = {
): Design =
self.bindConfigFromYamlInternal[A](Surface.of[A], yamlFile, defaultValue)(sourceCode)
}
}

trait ConfigCompat {
trait ConfigCompat:
self: Config =>

inline def of[ConfigType]: ConfigType = {
inline def of[ConfigType]: ConfigType =
self.ofSurface[ConfigType](Surface.of[ConfigType])
}

inline def getOrElse[ConfigType](default: => ConfigType): ConfigType = {
inline def getOrElse[ConfigType](default: => ConfigType): ConfigType =
self.getOrElseOfSurface[ConfigType](Surface.of[ConfigType], default)
}

inline def defaultValueOf[ConfigType]: ConfigType = {
inline def defaultValueOf[ConfigType]: ConfigType =
self.defaultValueOfSurface[ConfigType](Surface.of[ConfigType])
}

inline def register[ConfigType](config: ConfigType): Config = {
inline def register[ConfigType](config: ConfigType): Config =
self.registerOfSurface[ConfigType](Surface.of[ConfigType], config)
}

/**
* Register the default value of the object as configuration
*
* @tparam ConfigType
* @return
*/
inline def registerDefault[ConfigType]: Config = {
inline def registerDefault[ConfigType]: Config =
self.registerDefaultOfSurface[ConfigType](Surface.of[ConfigType])
}

inline def registerFromYaml[ConfigType](yamlFile: String): Config = {
inline def registerFromYaml[ConfigType](yamlFile: String): Config =
self.registerFromYaml[ConfigType](Surface.of[ConfigType], yamlFile)
}

inline def registerFromYamlOrElse[ConfigType](yamlFile: String, defaultValue: => ConfigType): Config = {
inline def registerFromYamlOrElse[ConfigType](yamlFile: String, defaultValue: => ConfigType): Config =
self.registerFromYamlOrElse[ConfigType](Surface.of[ConfigType], yamlFile, defaultValue)
}
}

trait YamlReaderCompat {
inline def load[A](resourcePath: String, env: String): A = {
trait YamlReaderCompat:
inline def load[A](resourcePath: String, env: String): A =
YamlReader.load[A](Surface.of[A], resourcePath, env)
}

inline def loadMapOf[A](resourcePath: String): Map[String, A] = {
inline def loadMapOf[A](resourcePath: String): Map[String, A] =
YamlReader.loadMapOf[A](Surface.of[A], resourcePath)
}

inline def bind[A](prop: Map[AnyRef, AnyRef]): A = {
inline def bind[A](prop: Map[AnyRef, AnyRef]): A =
YamlReader.bind[A](Surface.of[A], prop)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ case class SourceCode(
fileName: String,
line: Int,
col: Int
) {
):
override def toString = s"${fileName}:${line}"
}

object SourceCode {
object SourceCode:
def apply()(implicit code: SourceCode) = code

import scala.quoted.*

inline implicit def generate: SourceCode = ${ generateImpl }

private def generateImpl(using q: Quotes): Expr[SourceCode] = {
private def generateImpl(using q: Quotes): Expr[SourceCode] =
import q.reflect.*
val pos = Position.ofMacroExpansion
val line = Expr(pos.startLine)
Expand All @@ -44,5 +43,3 @@ object SourceCode {
val srcPath: java.nio.file.Path = java.nio.file.Paths.get(src.path)
val fileName = Expr(srcPath.getFileName().toString)
'{ SourceCode("", ${ fileName }, ${ line } + 1, ${ column }) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ import wvlet.airframe.surface.Surface

import java.util.concurrent.ConcurrentHashMap

private[airframe] trait AirframeSessionImpl { self: AirframeSession =>
inline override def register[A](instance: A): Unit = {
{
val surface = Surface.of[A]
val owner = self.findOwnerSessionOf(surface).getOrElse(self)
owner.registerInjectee(surface, surface, instance)
()
}
}
}
private[airframe] trait AirframeSessionImpl:
self: AirframeSession =>
inline override def register[A](instance: A): Unit =
val surface = Surface.of[A]
val owner = self.findOwnerSessionOf(surface).getOrElse(self)
owner.registerInjectee(surface, surface, instance)
()
70 changes: 26 additions & 44 deletions airframe-di/src/main/scala-3/wvlet/airframe/BinderImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,31 @@ import wvlet.airframe.Binder.*

/**
*/
private[airframe] trait BinderImpl[A] extends LogSupport { self: Binder[A] =>
private[airframe] trait BinderImpl[A] extends LogSupport:
self: Binder[A] =>

/**
* Bind a singleton instance of B to A
*
* @tparam B
*/
inline def to[B <: A]: DesignWithContext[B] = {
{
// registerTraitFactory[B]
val to = Surface.of[B]
if self.from == to then {
wvlet.log.Logger("wvlet.airframe.Binder").warn("Binding to the same type is not allowed: " + to.toString)
throw new wvlet.airframe.AirframeException.CYCLIC_DEPENDENCY(List(to), SourceCode())
}
self.design.addBinding[B](SingletonBinding(self.from, to, false, self.sourceCode))
}
}
inline def to[B <: A]: DesignWithContext[B] =
// registerTraitFactory[B]
val to = Surface.of[B]
if self.from == to then
wvlet.log.Logger("wvlet.airframe.Binder").warn("Binding to the same type is not allowed: " + to.toString)
throw new wvlet.airframe.AirframeException.CYCLIC_DEPENDENCY(List(to), SourceCode())
self.design.addBinding[B](SingletonBinding(self.from, to, false, self.sourceCode))

inline def toEagerSingletonOf[B <: A]: DesignWithContext[B] = {
{
// registerTraitFactory[B]
val to = Surface.of[B]
if self.from == to then {
wvlet.log.Logger("wvlet.airframe.Binder").warn("Binding to the same type is not allowed: " + to.toString)
throw new wvlet.airframe.AirframeException.CYCLIC_DEPENDENCY(List(to), SourceCode())
}
self.design.addBinding[B](SingletonBinding(self.from, to, true, self.sourceCode))
}
}
inline def toEagerSingletonOf[B <: A]: DesignWithContext[B] =
// registerTraitFactory[B]
val to = Surface.of[B]
if self.from == to then
wvlet.log.Logger("wvlet.airframe.Binder").warn("Binding to the same type is not allowed: " + to.toString)
throw new wvlet.airframe.AirframeException.CYCLIC_DEPENDENCY(List(to), SourceCode())
self.design.addBinding[B](SingletonBinding(self.from, to, true, self.sourceCode))

inline def toProvider[D1](factory: D1 => A): DesignWithContext[A] = {
inline def toProvider[D1](factory: D1 => A): DesignWithContext[A] =
// registerTraitFactory[D1]
self.design.addBinding[A](
ProviderBinding(
Expand All @@ -48,8 +41,7 @@ private[airframe] trait BinderImpl[A] extends LogSupport { self: Binder[A] =>
SourceCode()
)
)
}
inline def toProvider[D1, D2](factory: (D1, D2) => A): DesignWithContext[A] = {
inline def toProvider[D1, D2](factory: (D1, D2) => A): DesignWithContext[A] =
// registerTraitFactory[D1]
// registerTraitFactory[D2]
self.design.addBinding[A](
Expand All @@ -60,8 +52,7 @@ private[airframe] trait BinderImpl[A] extends LogSupport { self: Binder[A] =>
SourceCode()
)
)
}
inline def toProvider[D1, D2, D3](factory: (D1, D2, D3) => A): DesignWithContext[A] = {
inline def toProvider[D1, D2, D3](factory: (D1, D2, D3) => A): DesignWithContext[A] =
// registerTraitFactory[D1]
// registerTraitFactory[D2]
// registerTraitFactory[D3]
Expand All @@ -73,8 +64,7 @@ private[airframe] trait BinderImpl[A] extends LogSupport { self: Binder[A] =>
SourceCode()
)
)
}
inline def toProvider[D1, D2, D3, D4](factory: (D1, D2, D3, D4) => A): DesignWithContext[A] = {
inline def toProvider[D1, D2, D3, D4](factory: (D1, D2, D3, D4) => A): DesignWithContext[A] =
// registerTraitFactory[D1]
// registerTraitFactory[D2]
// registerTraitFactory[D3]
Expand All @@ -87,8 +77,7 @@ private[airframe] trait BinderImpl[A] extends LogSupport { self: Binder[A] =>
SourceCode()
)
)
}
inline def toProvider[D1, D2, D3, D4, D5](factory: (D1, D2, D3, D4, D5) => A): DesignWithContext[A] = {
inline def toProvider[D1, D2, D3, D4, D5](factory: (D1, D2, D3, D4, D5) => A): DesignWithContext[A] =
// registerTraitFactory[D1]
// registerTraitFactory[D2]
// registerTraitFactory[D3]
Expand All @@ -106,9 +95,8 @@ private[airframe] trait BinderImpl[A] extends LogSupport { self: Binder[A] =>
SourceCode()
)
)
}

inline def toEagerSingletonProvider[D1](factory: D1 => A): DesignWithContext[A] = {
inline def toEagerSingletonProvider[D1](factory: D1 => A): DesignWithContext[A] =
// registerTraitFactory[D1]
self.design.addBinding[A](
ProviderBinding(
Expand All @@ -118,8 +106,7 @@ private[airframe] trait BinderImpl[A] extends LogSupport { self: Binder[A] =>
SourceCode()
)
)
}
inline def toEagerSingletonProvider[D1, D2](factory: (D1, D2) => A): DesignWithContext[A] = {
inline def toEagerSingletonProvider[D1, D2](factory: (D1, D2) => A): DesignWithContext[A] =
// registerTraitFactory[D1]
// registerTraitFactory[D2]
self.design.addBinding[A](
Expand All @@ -130,8 +117,7 @@ private[airframe] trait BinderImpl[A] extends LogSupport { self: Binder[A] =>
SourceCode()
)
)
}
inline def toEagerSingletonProvider[D1, D2, D3](factory: (D1, D2, D3) => A): DesignWithContext[A] = {
inline def toEagerSingletonProvider[D1, D2, D3](factory: (D1, D2, D3) => A): DesignWithContext[A] =
// registerTraitFactory[D1]
// registerTraitFactory[D2]
// registerTraitFactory[D3]
Expand All @@ -143,8 +129,7 @@ private[airframe] trait BinderImpl[A] extends LogSupport { self: Binder[A] =>
SourceCode()
)
)
}
inline def toEagerSingletonProvider[D1, D2, D3, D4](factory: (D1, D2, D3, D4) => A): DesignWithContext[A] = {
inline def toEagerSingletonProvider[D1, D2, D3, D4](factory: (D1, D2, D3, D4) => A): DesignWithContext[A] =
// registerTraitFactory[D1]
// registerTraitFactory[D2]
// registerTraitFactory[D3]
Expand All @@ -157,8 +142,7 @@ private[airframe] trait BinderImpl[A] extends LogSupport { self: Binder[A] =>
SourceCode()
)
)
}
inline def toEagerSingletonProvider[D1, D2, D3, D4, D5](factory: (D1, D2, D3, D4, D5) => A): DesignWithContext[A] = {
inline def toEagerSingletonProvider[D1, D2, D3, D4, D5](factory: (D1, D2, D3, D4, D5) => A): DesignWithContext[A] =
// registerTraitFactory[D1]
// registerTraitFactory[D2]
// registerTraitFactory[D3]
Expand All @@ -176,5 +160,3 @@ private[airframe] trait BinderImpl[A] extends LogSupport { self: Binder[A] =>
SourceCode()
)
)
}
}
Loading
Loading