Skip to content

Commit

Permalink
Rename CodeOf to Expr to make things consistent with how Type is used…
Browse files Browse the repository at this point in the history
… in macros
  • Loading branch information
MateuszKubuszok committed Sep 19, 2022
1 parent 7d7bd78 commit 3014ba3
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 180 deletions.
6 changes: 3 additions & 3 deletions pipez/src/main/scala-2/pipez/internal/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ final class MacrosImpl[Pipe[_, _], In, Out](val c: blackbox.Context)(

import c.universe.*

val pipeDerivation: CodeOf[PipeDerivation.Aux[Pipe, Context, Result]] = {
val expr = pd.asInstanceOf[CodeOf[PipeDerivation[Pipe]]]
val pipeDerivation: Expr[PipeDerivation.Aux[Pipe, Context, Result]] = {
val expr = pd.asInstanceOf[Expr[PipeDerivation[Pipe]]]
val Pipe = pipeTpe.asInstanceOf[c.Type]
c.Expr(
q"""$expr.asInstanceOf[_root_.pipez.PipeDerivation.Aux[$Pipe, $Context, ${Result[Any].typeConstructor}]]"""
)
}
val previewPipeDerivation: String = previewCode(pd.asInstanceOf[CodeOf[PipeDerivation[Pipe]]])
val previewPipeDerivation: String = previewCode(pd.asInstanceOf[Expr[PipeDerivation[Pipe]]])
}

final class Macro(val c: blackbox.Context) {
Expand Down
14 changes: 7 additions & 7 deletions pipez/src/main/scala-2/pipez/internal/PlatformDefinitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ trait PlatformDefinitions[Pipe[_, _], In, Out] extends Definitions[Pipe, In, Out
type Tagged[U] = { type Tag = U }
type @@[T, U] = T & Tagged[U]

override type Type[A] = c.Type @@ A
override type CodeOf[A] = Expr[A]
override type Type[A] = c.Type @@ A
override type Expr[A] = c.Expr[A]

final def previewType[A: Type]: String = typeOf[A].toString

final def previewCode[A](code: CodeOf[A]): String = showCode(code.tree)
final def previewCode[A](code: Expr[A]): String = showCode(code.tree)

final def pathCode(path: Path): CodeOf[pipez.Path] = path match {
final def pathCode(path: Path): Expr[pipez.Path] = path match {
case Path.Root => c.Expr[pipez.Path](q"_root_.pipez.Path.root")
case Path.Field(from, name) => c.Expr[pipez.Path](q"${pathCode(from)}.field(${Constant(name)})")
case Path.Subtype(from, name) => c.Expr[pipez.Path](q"${pathCode(from)}.subtype(${Constant(name)})")
}

final def summonPipe[Input: Type, Output: Type]: DerivationResult[CodeOf[Pipe[Input, Output]]] =
final def summonPipe[Input: Type, Output: Type]: DerivationResult[Expr[Pipe[Input, Output]]] =
DerivationResult
.unsafe(c.Expr[Pipe[Input, Output]](c.inferImplicitValue(PipeOf[Input, Output], silent = false)))(_ =>
DerivationError.RequiredImplicitNotFound(typeOf[Input], typeOf[Output])
)
.logSuccess(i => s"Summoned implicit value: ${previewCode(i)}")

final def singleAbstractMethodExpansion[SAM: Type](code: CodeOf[SAM]): CodeOf[SAM] =
final def singleAbstractMethodExpansion[SAM: Type](code: Expr[SAM]): Expr[SAM] =
c.Expr(q"_root_.scala.Predef.identity[${typeOf[SAM]}]($code)")

final def readConfig(code: CodeOf[PipeDerivationConfig[Pipe, In, Out]]): DerivationResult[Settings] = {
final def readConfig(code: Expr[PipeDerivationConfig[Pipe, In, Out]]): DerivationResult[Settings] = {
@nowarn("cat=unused")
def extractPath(in: Tree): Either[String, Path] = in match {
case Function(_, expr) => extractPath(expr)
Expand Down
30 changes: 15 additions & 15 deletions pipez/src/main/scala-2/pipez/internal/PlatformGenerators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,27 @@ trait PlatformGenerators[Pipe[_, _], In, Out]
c.abort(c.enclosingPosition, errorMessage(errors))

final def lift[I: Type, O: Type](
call: CodeOf[(I, Context) => Result[O]]
): CodeOf[Pipe[I, O]] = c.Expr[Pipe[I, O]](q"""$pipeDerivation.lift($call)""")
call: Expr[(I, Context) => Result[O]]
): Expr[Pipe[I, O]] = c.Expr[Pipe[I, O]](q"""$pipeDerivation.lift($call)""")

final def unlift[I: Type, O: Type](
pipe: CodeOf[Pipe[I, O]],
in: CodeOf[I],
ctx: CodeOf[Context]
): CodeOf[Result[O]] = c.Expr[Result[O]](q"""$pipeDerivation.unlift($pipe, $in, $ctx)""")
pipe: Expr[Pipe[I, O]],
in: Expr[I],
ctx: Expr[Context]
): Expr[Result[O]] = c.Expr[Result[O]](q"""$pipeDerivation.unlift($pipe, $in, $ctx)""")

final def updateContext(
ctx: CodeOf[Context],
path: CodeOf[pipez.Path]
): CodeOf[Context] = c.Expr[Context](q"""$pipeDerivation.updateContext($ctx, $path)""")
ctx: Expr[Context],
path: Expr[pipez.Path]
): Expr[Context] = c.Expr[Context](q"""$pipeDerivation.updateContext($ctx, $path)""")

final def pureResult[A: Type](a: CodeOf[A]): CodeOf[Result[A]] =
final def pureResult[A: Type](a: Expr[A]): Expr[Result[A]] =
c.Expr[Result[A]](q"""$pipeDerivation.pureResult($a)""")

final def mergeResults[A: Type, B: Type, C: Type](
ctx: CodeOf[Context],
ra: CodeOf[Result[A]],
rb: CodeOf[Result[B]],
f: CodeOf[(A, B) => C]
): CodeOf[Result[C]] = c.Expr[Result[C]](q"""$pipeDerivation.mergeResults($ctx, $ra, $rb, $f)""")
ctx: Expr[Context],
ra: Expr[Result[A]],
rb: Expr[Result[B]],
f: Expr[(A, B) => C]
): Expr[Result[C]] = c.Expr[Result[C]](q"""$pipeDerivation.mergeResults($ctx, $ra, $rb, $f)""")
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ trait PlatformProductCaseGeneration[Pipe[_, _], In, Out] extends ProductCaseGene
tpe = member.asMethod.returnType.asInstanceOf[Type[Any]],
get =
if (member.asMethod.paramLists.isEmpty)
(in: CodeOf[In]) => c.Expr[Any](q"$in.${member.asMethod.name.toTermName}")
else (in: CodeOf[In]) => c.Expr[Any](q"$in.${member.asMethod.name.toTermName}()"),
(in: Expr[In]) => c.Expr[Any](q"$in.${member.asMethod.name.toTermName}")
else (in: Expr[In]) => c.Expr[Any](q"$in.${member.asMethod.name.toTermName}()"),
path = Path.Field(Path.Root, member.name.toString)
)
}
Expand Down Expand Up @@ -73,8 +73,8 @@ trait PlatformProductCaseGeneration[Pipe[_, _], In, Out] extends ProductCaseGene
method.name.toString -> ProductOutData.Setter(
name = method.name.toString,
tpe = method.asMethod.paramLists.flatten.head.typeSignature.asInstanceOf[Type[Any]],
set = (out: CodeOf[Out], value: CodeOf[Any]) =>
c.Expr[Unit](q"$out.${method.asMethod.name.toTermName}($value)")
set =
(out: Expr[Out], value: Expr[Any]) => c.Expr[Unit](q"$out.${method.asMethod.name.toTermName}($value)")
)
}
.to(ListMap)
Expand Down Expand Up @@ -119,7 +119,7 @@ trait PlatformProductCaseGeneration[Pipe[_, _], In, Out] extends ProductCaseGene
.logSuccess(data => s"Resolved case class output: $data")
}

final def generateProductCode(generatorData: ProductGeneratorData): DerivationResult[CodeOf[Pipe[In, Out]]] =
final def generateProductCode(generatorData: ProductGeneratorData): DerivationResult[Expr[Pipe[In, Out]]] =
generatorData match {
case ProductGeneratorData.CaseClass(caller, results) => generateCaseClass(caller, results)
case ProductGeneratorData.JavaBean(defaultConstructor, results) => generateJavaBean(defaultConstructor, results)
Expand All @@ -135,7 +135,7 @@ trait PlatformProductCaseGeneration[Pipe[_, _], In, Out] extends ProductCaseGene
.map { case (result, idx) => result -> Constant(idx) }
.toMap

def constructorParams(in: CodeOf[In], ctx: CodeOf[Context], arr: CodeOf[Array[Any]]): List[List[CodeOf[?]]] =
def constructorParams(in: Expr[In], ctx: Expr[Context], arr: Expr[Array[Any]]): List[List[Expr[?]]] =
outputParameterLists.map(
_.map {
case ProductGeneratorData.OutputValue.Pure(_, caller) =>
Expand All @@ -146,27 +146,27 @@ trait PlatformProductCaseGeneration[Pipe[_, _], In, Out] extends ProductCaseGene
)

val arrSize = Constant(paramToIdx.size)
val initialValue: CodeOf[Result[Array[Any]]] = pureResult(c.Expr(q"scala.Array[scala.Any]($arrSize)"))
val initialValue: Expr[Result[Array[Any]]] = pureResult(c.Expr(q"scala.Array[scala.Any]($arrSize)"))

@scala.annotation.tailrec
def generateBody(
in: CodeOf[In],
ctx: CodeOf[Context],
arrayResult: CodeOf[Result[Array[Any]]],
in: Expr[In],
ctx: Expr[Context],
arrayResult: Expr[Result[Array[Any]]],
params: List[(ProductGeneratorData.OutputValue.Result[?], Constant)]
): CodeOf[Result[Out]] =
): Expr[Result[Out]] =
params match {
// all values are taken directly from input and wrapped in Result
case Nil =>
pureResult(constructor(constructorParams(in, ctx, null)))

// last param - after adding the last value to array we extract all values from it into constructor
case (param, idx) :: Nil =>
val rightCode = param.caller(in, ctx).asInstanceOf[CodeOf[Result[Any]]]
val rightCode = param.caller(in, ctx).asInstanceOf[Expr[Result[Any]]]

val left = c.freshName(TermName("left"))
val right = c.freshName(TermName("right"))
val fun: CodeOf[(Array[Any], Any) => Out] = c.Expr[(Array[Any], Any) => Out](
val fun: Expr[(Array[Any], Any) => Out] = c.Expr[(Array[Any], Any) => Out](
q"""
($left : scala.Array[scala.Any], $right : ${param.tpe}) => {
$left($idx) = $right
Expand All @@ -179,7 +179,7 @@ trait PlatformProductCaseGeneration[Pipe[_, _], In, Out] extends ProductCaseGene

// we combine Array's Result with a param's Result, store param in array and iterate further
case (param, idx) :: tail =>
val rightCode = param.caller(in, ctx).asInstanceOf[CodeOf[Result[Any]]]
val rightCode = param.caller(in, ctx).asInstanceOf[Expr[Result[Any]]]

val left = c.freshName(TermName("left"))
val right = c.freshName(TermName("right"))
Expand All @@ -195,7 +195,7 @@ trait PlatformProductCaseGeneration[Pipe[_, _], In, Out] extends ProductCaseGene
generateBody(in, ctx, mergeResults(ctx, arrayResult, rightCode, fun), tail)
}

val body: CodeOf[Pipe[In, Out]] = {
val body: Expr[Pipe[In, Out]] = {
val in = c.freshName(TermName("in"))
val ctx = c.freshName(TermName("ctx"))
lift[In, Out](
Expand All @@ -215,10 +215,10 @@ trait PlatformProductCaseGeneration[Pipe[_, _], In, Out] extends ProductCaseGene
}

private def generateJavaBean(
defaultConstructor: CodeOf[Out],
defaultConstructor: Expr[Out],
outputSettersList: List[(ProductGeneratorData.OutputValue, ProductOutData.Setter[?])]
) = {
def pureValues(in: CodeOf[In], ctx: CodeOf[Context], result: CodeOf[Out]): List[CodeOf[Unit]] =
def pureValues(in: Expr[In], ctx: Expr[Context], result: Expr[Out]): List[Expr[Unit]] =
outputSettersList.collect { case (ProductGeneratorData.OutputValue.Pure(_, caller), setter) =>
setter.asInstanceOf[ProductOutData.Setter[Any]].set(result, caller(in, ctx))
}
Expand All @@ -228,7 +228,7 @@ trait PlatformProductCaseGeneration[Pipe[_, _], In, Out] extends ProductCaseGene
r -> s
}

def initialValue(in: CodeOf[In], ctx: CodeOf[Context]): CodeOf[Result[Out]] = {
def initialValue(in: Expr[In], ctx: Expr[Context]): Expr[Result[Out]] = {
val result = c.freshName(TermName("result"))
pureResult(
c.Expr[Out](
Expand All @@ -245,19 +245,19 @@ trait PlatformProductCaseGeneration[Pipe[_, _], In, Out] extends ProductCaseGene

@scala.annotation.tailrec
def generateBody(
in: CodeOf[In],
ctx: CodeOf[Context],
outResult: CodeOf[Result[Out]],
in: Expr[In],
ctx: Expr[Context],
outResult: Expr[Result[Out]],
params: List[(ProductGeneratorData.OutputValue.Result[?], ProductOutData.Setter[?])]
): CodeOf[Result[Out]] =
): Expr[Result[Out]] =
params match {
// all values are taken directly from input and wrapped in Result
case Nil =>
outResult

// we have Out object on left and value to put into setter on right
case (param, setter) :: tail =>
val rightCode = param.caller(in, ctx).asInstanceOf[CodeOf[Result[Any]]]
val rightCode = param.caller(in, ctx).asInstanceOf[Expr[Result[Any]]]

val left = c.freshName(TermName("left"))
val right = c.freshName(TermName("right"))
Expand All @@ -273,7 +273,7 @@ trait PlatformProductCaseGeneration[Pipe[_, _], In, Out] extends ProductCaseGene
generateBody(in, ctx, mergeResults(ctx, outResult, rightCode, fun), tail)
}

val body: CodeOf[Pipe[In, Out]] = {
val body: Expr[Pipe[In, Out]] = {
val in = c.freshName(TermName("in"))
val ctx = c.freshName(TermName("ctx"))
lift[In, Out](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ trait PlatformSumCaseGeneration[Pipe[_, _], In, Out] extends SumCaseGeneration[P
)
}

final def generateEnumCode(generatorData: EnumGeneratorData): DerivationResult[CodeOf[Pipe[In, Out]]] =
final def generateEnumCode(generatorData: EnumGeneratorData): DerivationResult[Expr[Pipe[In, Out]]] =
generateSubtypes(generatorData.subtypes.values.toList)

private def generateSubtypes(subtypes: List[EnumGeneratorData.InputSubtype]) = {
def cases(in: CodeOf[In], ctx: CodeOf[Context]) = subtypes
def cases(in: Expr[In], ctx: Expr[Context]) = subtypes
.map {
case EnumGeneratorData.InputSubtype.Convert(inSubtype, _, pipe, path) =>
val arg = c.freshName(TermName("arg"))
Expand Down
2 changes: 1 addition & 1 deletion pipez/src/main/scala-3/pipez/internal/Macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MacrosImpl[Pipe[_, _], In, Out](q: Quotes)(
def PipeOf[I: Type, O: Type]: Type[Pipe[I, O]] =
TypeRepr.of(using Pipe).appliedTo(List(TypeRepr.of[I], TypeRepr.of[O])).asType.asInstanceOf[Type[Pipe[I, O]]]

val pipeDerivation: CodeOf[PipeDerivation.Aux[Pipe, Context, Result]] = {
val pipeDerivation: Expr[PipeDerivation.Aux[Pipe, Context, Result]] = {
given p: scala.quoted.Type[Pipe] = Pipe
'{ ${ pd }.asInstanceOf[PipeDerivation.Aux[Pipe, Context, Result]] }
}
Expand Down
22 changes: 11 additions & 11 deletions pipez/src/main/scala-3/pipez/internal/PlatformDefinitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,32 @@ trait PlatformDefinitions[Pipe[_, _], In, Out](using val quotes: Quotes) extends
import quotes.*
import quotes.reflect.*

override type Type[A] = scala.quoted.Type[A]
override type CodeOf[A] = Expr[A]
override type Type[A] = scala.quoted.Type[A]
override type Expr[A] = scala.quoted.Expr[A]

final def previewType[A: Type]: String =
val repr = TypeRepr.of[A]
scala.util.Try(repr.show).getOrElse(repr.toString)

final def previewCode[A](code: CodeOf[A]): String = code.show
final def previewCode[A](code: Expr[A]): String = code.show

final def pathCode(path: Path): CodeOf[pipez.Path] = path match
final def pathCode(path: Path): Expr[pipez.Path] = path match
case Path.Root => '{ pipez.Path.root }
case Path.Field(from, name) => '{ ${ pathCode(from) }.field(${ Expr(name) }) }
case Path.Subtype(from, name) => '{ ${ pathCode(from) }.subtype(${ Expr(name) }) }

final def summonPipe[Input: Type, Output: Type]: DerivationResult[CodeOf[Pipe[Input, Output]]] =
final def summonPipe[Input: Type, Output: Type]: DerivationResult[Expr[Pipe[Input, Output]]] =
DerivationResult
.fromOption(scala.quoted.Expr.summon[Pipe[Input, Output]])(
DerivationError.RequiredImplicitNotFound(typeOf[Input], typeOf[Output])
)
.logSuccess(i => s"Summoned implicit value: ${previewCode(i)}")

final def singleAbstractMethodExpansion[SAM: Type](code: CodeOf[SAM]): CodeOf[SAM] =
final def singleAbstractMethodExpansion[SAM: Type](code: Expr[SAM]): Expr[SAM] =
val SAM = typeOf[SAM]
'{ scala.Predef.identity[SAM.Underlying](${ code }) }

final def readConfig(code: CodeOf[PipeDerivationConfig[Pipe, In, Out]]): DerivationResult[Settings] = {
final def readConfig(code: Expr[PipeDerivationConfig[Pipe, In, Out]]): DerivationResult[Settings] = {
def extractPath(in: Tree): Either[String, Path] = in match {
case Block(List(DefDef(_, _, _, Some(term))), _) => extractPath(term)
case Select(term, field) => extractPath(term).map(Path.Field(_, field)) // extract .field
Expand Down Expand Up @@ -70,7 +70,7 @@ trait PlatformDefinitions[Pipe[_, _], In, Out](using val quotes: Quotes) extends
ConfigEntry.AddField(
outFieldPath,
outFieldType,
singleAbstractMethodExpansion(pipe.asExpr.asInstanceOf[CodeOf[Pipe[In, Any]]])(
singleAbstractMethodExpansion(pipe.asExpr.asInstanceOf[Expr[Pipe[In, Any]]])(
PipeOf[In, Any](In, outFieldType)
)
) :: acc
Expand Down Expand Up @@ -104,7 +104,7 @@ trait PlatformDefinitions[Pipe[_, _], In, Out](using val quotes: Quotes) extends
inputFieldType,
outPath,
outputFieldType,
singleAbstractMethodExpansion(pipe.asExpr.asInstanceOf[CodeOf[Pipe[Any, Any]]])(
singleAbstractMethodExpansion(pipe.asExpr.asInstanceOf[Expr[Pipe[Any, Any]]])(
PipeOf[Any, Any](inputFieldType, outputFieldType)
)
) :: acc
Expand All @@ -123,7 +123,7 @@ trait PlatformDefinitions[Pipe[_, _], In, Out](using val quotes: Quotes) extends
ConfigEntry.RemoveSubtype(
inputSubtypePath,
inputSubtypeType,
singleAbstractMethodExpansion(pipe.asExpr.asInstanceOf[CodeOf[Pipe[In, Out]]])(
singleAbstractMethodExpansion(pipe.asExpr.asInstanceOf[Expr[Pipe[In, Out]]])(
PipeOf[In, Out](inputSubtypeType, Out)
)
) :: acc
Expand Down Expand Up @@ -160,7 +160,7 @@ trait PlatformDefinitions[Pipe[_, _], In, Out](using val quotes: Quotes) extends
inputSubtypeType,
outputSubtypePath,
outputSubtypeType,
singleAbstractMethodExpansion(pipe.asExpr.asInstanceOf[CodeOf[Pipe[In, Out]]])(
singleAbstractMethodExpansion(pipe.asExpr.asInstanceOf[Expr[Pipe[In, Out]]])(
PipeOf[In, Out](inputSubtypeType, outputSubtypeType)
)
) :: acc
Expand Down
30 changes: 15 additions & 15 deletions pipez/src/main/scala-3/pipez/internal/PlatformGenerators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ trait PlatformGenerators[Pipe[_, _], In, Out]
report.errorAndAbort(errorMessage(errors))

final def lift[I: Type, O: Type](
call: CodeOf[(I, Context) => Result[O]]
): CodeOf[Pipe[I, O]] = '{ ${ pipeDerivation }.lift(${ call }) }
call: Expr[(I, Context) => Result[O]]
): Expr[Pipe[I, O]] = '{ ${ pipeDerivation }.lift(${ call }) }

final def unlift[I: Type, O: Type](
pipe: CodeOf[Pipe[I, O]],
in: CodeOf[I],
ctx: CodeOf[Context]
): CodeOf[Result[O]] = '{ ${ pipeDerivation }.unlift(${ pipe }, ${ in }, ${ ctx }) }
pipe: Expr[Pipe[I, O]],
in: Expr[I],
ctx: Expr[Context]
): Expr[Result[O]] = '{ ${ pipeDerivation }.unlift(${ pipe }, ${ in }, ${ ctx }) }

final def updateContext(
ctx: CodeOf[Context],
path: CodeOf[pipez.Path]
): CodeOf[Context] = '{ ${ pipeDerivation }.updateContext(${ ctx }, ${ path }) }
ctx: Expr[Context],
path: Expr[pipez.Path]
): Expr[Context] = '{ ${ pipeDerivation }.updateContext(${ ctx }, ${ path }) }

final def pureResult[A: Type](a: CodeOf[A]): CodeOf[Result[A]] = '{ ${ pipeDerivation }.pureResult(${ a }) }
final def pureResult[A: Type](a: Expr[A]): Expr[Result[A]] = '{ ${ pipeDerivation }.pureResult(${ a }) }

final def mergeResults[A: Type, B: Type, C: Type](
ctx: CodeOf[Context],
ra: CodeOf[Result[A]],
rb: CodeOf[Result[B]],
f: CodeOf[(A, B) => C]
): CodeOf[Result[C]] = '{ ${ pipeDerivation }.mergeResults(${ ctx }, ${ ra }, ${ rb }, ${ f }) }
ctx: Expr[Context],
ra: Expr[Result[A]],
rb: Expr[Result[B]],
f: Expr[(A, B) => C]
): Expr[Result[C]] = '{ ${ pipeDerivation }.mergeResults(${ ctx }, ${ ra }, ${ rb }, ${ f }) }
}
Loading

0 comments on commit 3014ba3

Please sign in to comment.