Skip to content
This repository was archived by the owner on Apr 27, 2023. It is now read-only.

Commit da94f11

Browse files
committed
Support case classes with private constructors
1 parent 8dcca02 commit da94f11

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/main/scala/com/softwaremill/macros/customize/CustomizeImpl.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class CustomizeImpl(val c: whitebox.Context) {
2020
def extractCaseClassesParts(classDecl: ClassDef) = classDecl match {
2121
case q"""case class $className(..$fields) extends ..$parents { ..$body }""" =>
2222
(className, fields, parents, body)
23+
case q"""case class $className private(..$fields) extends ..$parents { ..$body }""" =>
24+
(className, fields, parents, body)
25+
case _ => c.abort(c.enclosingPosition, "Unsupported case class type. Cannot rewrite toString().")
2326
}
2427

2528
def extractNewToString(typeName: TypeName, allFields: List[Tree]) = {

src/test/scala/ToStringMaskTest.scala

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import java.time.ZonedDateTime
2-
import java.util.Date
32

43
import com.softwaremill.macros.customize.{customize, mask}
54
import org.scalatest.{FlatSpec, Matchers}
@@ -26,11 +25,26 @@ class ToStringMaskTest extends FlatSpec with Matchers {
2625
// then
2726
u.toString should be("User30(2,***)")
2827
}
28+
29+
it should "work on case classes with private constructors" in {
30+
// given
31+
val u = UserPrivate("some@email2.com")
32+
33+
// then
34+
u.toString should be("UserPrivate(1,***)")
35+
}
2936
}
3037

3138
@customize
3239
case class User30(id: Int, @mask email: String)
3340

41+
@customize
42+
case class UserPrivate private(id: Int, @mask email: String)
43+
44+
object UserPrivate {
45+
def apply(email: String) = new UserPrivate(1, email)
46+
}
47+
3448
object User30 {
3549
def apply(id: Int) = new User30(id, "default@email.com")
3650
}

0 commit comments

Comments
 (0)