提交 67d95192 编写于 作者: 梦境迷离's avatar 梦境迷离

check field size

上级 4c5fd03e
......@@ -114,8 +114,16 @@ class TransformerMacro(override val c: whitebox.Context) extends AbstractMacroPr
private def getTransformBody[From: WeakTypeTag, To: WeakTypeTag]: Tree = {
val toClassName = resolveClassTypeName[To]
val fromClassName = resolveClassTypeName[From]
val toClassInfo = getCaseClassFieldInfo[To]()
val fromClassInfo = getCaseClassFieldInfo[From]()
if (fromClassInfo.size < toClassInfo.size) {
c.abort(
c.enclosingPosition,
s"From type: `$fromClassName` has fewer fields than To type: `$toClassName` and cannot be transformed"
)
}
val customFieldNameMapping =
MacroCache.classFieldNameMapping.getOrElse(getBuilderId(annoBuilderPrefix), mutable.Map.empty)
val customFieldValueMapping =
......
......@@ -156,8 +156,6 @@ class TransformableTest extends AnyFlatSpec with Matchers {
| val a1 = A1("hello", 1, 2, None)
| val a2 = Transformer[A1, A2].transform(a1)
| a2.toString shouldBe "A2(hello,1,2,None)"
|
|
|""".stripMargin shouldNot compile
}
......@@ -182,4 +180,20 @@ class TransformableTest extends AnyFlatSpec with Matchers {
actualToQueryResult shouldBe expectToQueryResult
}
"TransformableTest From have fewer fields than To" should "compile error" in {
"""
| case class B1(a: List[String])
| case class B2(a: List[String], b: Int)
| val b2 = Transformable[B1, B2].instance.transform(B1(List.empty))
| println(b2)
|""".stripMargin shouldNot compile
}
"TransformableTest From have more fields than To" should "ok" in {
case class B1(a: List[String], b: Int)
case class B2(a: List[String])
val b2 = Transformable[B1, B2].instance.transform(B1(List.empty, 1))
println(b2)
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册