Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
tarao committed Nov 6, 2023
1 parent 6a7f9a6 commit 44ac279
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,23 @@ private[record4s] class InternalMacros(using
(Expr.ofSeq(fields), tupledFieldTypes)
}

def requireApply[C, T](context: Expr[C], method: Expr[String])(
block: => T,
): T = {
method.asTerm match {
case Inlined(_, _, Literal(StringConstant(name))) if name == "apply" =>
block
case Inlined(_, _, Literal(StringConstant(name))) =>
errorAndAbort(
s"'${name}' is not a member of ${context.asTerm.tpe.widen.show} constructor",
)
case _ =>
errorAndAbort(
s"Invalid method invocation on ${context.asTerm.tpe.widen.show} constructor",
)
}
}

def fieldSelectionsOf[S: Type](
schema: Schema,
): Seq[(String, String, Type[?])] = {
Expand Down Expand Up @@ -399,26 +416,6 @@ private[record4s] class InternalMacros(using

schema.fieldTypes.filterNot((label, _) => unselected.contains(label))
}

def newMapRecord[R: Type](record: Expr[Iterable[(String, Any)]]): Expr[R] =
'{ new MapRecord(${ record }.toMap).asInstanceOf[R] }

def requireApply[C, T](context: Expr[C], method: Expr[String])(
block: => T,
): T = {
method.asTerm match {
case Inlined(_, _, Literal(StringConstant(name))) if name == "apply" =>
block
case Inlined(_, _, Literal(StringConstant(name))) =>
errorAndAbort(
s"'${name}' is not a member of ${context.asTerm.tpe.widen.show} constructor",
)
case _ =>
errorAndAbort(
s"Invalid method invocation on ${context.asTerm.tpe.widen.show} constructor",
)
}
}
}

private[record4s] object InternalMacros {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ object Macros {
case '[tpe] =>
evidenceOf[Concat[R, tpe]] match {
case '{ ${ _ }: Concat[R, tpe] { type Out = returnType } } =>
newMapRecord[returnType]('{
${ rec }.toMap.concat(${ fields })
})
'{
new MapRecord(${ rec }.toMap.concat(${ fields }))
.asInstanceOf[returnType]
}
}
}
}
Expand Down

0 comments on commit 44ac279

Please sign in to comment.