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

codec (internal): Use inline methods instead of macros #3110

Merged
merged 1 commit into from
Aug 6, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,9 @@ import wvlet.airframe.surface.Surface
import wvlet.airframe.surface.reflect.ReflectSurfaceFactory

trait CompatBase {
inline def codecOf[A]: MessageCodec[A] = ${ CompatBase.codecOf[A] }
// TODO Implementation
def surfaceOfClass(cl: Class[_]): Surface = ReflectSurfaceFactory.ofClass(cl)
}

private[codec] object CompatBase {
import scala.quoted._

def codecOf[A](using t: Type[A], quotes: Quotes): Expr[MessageCodec[A]] = {
import quotes._
'{ MessageCodecFactory.defaultFactory.of[A] }
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 @@ -17,39 +17,14 @@ object ScalaCompat {
}

trait MessageCodecFactoryBase { self: MessageCodecFactory =>
inline def of[A]: MessageCodec[A] = ${ CodecMacros.factoryOf[A]('self) }
inline def fromJson[A](json: String): A = ${ CodecMacros.factoryFromJson[A]('json) }
inline def toJson[A](obj: A): String = ${ CodecMacros.factoryToJson[A]('obj) }
}

import scala.quoted._

private[codec] object CodecMacros {
def codecOf[A](using t: Type[A], quotes: Quotes): Expr[MessageCodec[A]] = {
import quotes._
'{ MessageCodec.ofSurface(Surface.of[A]).asInstanceOf[MessageCodec[A]] }
}
def codecFromJson[A](json: Expr[String])(using t: Type[A], quotes: Quotes): Expr[A] = {
import quotes._
'{ MessageCodecFactory.defaultFactory.fromJson[A](${ json }) }
}
def codecToJson[A](obj: Expr[A])(using t: Type[A], quotes: Quotes): Expr[String] = {
import quotes._
'{ MessageCodecFactory.defaultFactory.toJson[A](${ obj }) }
}

def factoryOf[A](self: Expr[MessageCodecFactory])(using t: Type[A], quote: Quotes): Expr[MessageCodec[A]] = {
import quotes._
'{ ${ self }.ofSurface(Surface.of[A]).asInstanceOf[MessageCodec[A]] }
inline def of[A]: MessageCodec[A] = {
self.ofSurface(Surface.of[A]).asInstanceOf[MessageCodec[A]]
}
def factoryFromJson[A](json: Expr[String])(using t: Type[A], quotes: Quotes): Expr[A] = {
import quotes._
'{ MessageCodec.of[A].fromJson(${ json }) }
inline def fromJson[A](json: String): A = {
MessageCodec.of[A].fromJson(json)
}
def factoryToJson[A](obj: Expr[A])(using t: Type[A], quotes: Quotes): Expr[String] = {
import quotes._
'{ MessageCodec.of[A].toJson(${ obj }) }
inline def toJson[A](obj: A): String = {
MessageCodec.of[A].toJson(obj)
}
}

}
Loading