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

Commit

Permalink
Support case classes with private constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
kciesielski committed Jun 23, 2016
1 parent 8dcca02 commit da94f11
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class CustomizeImpl(val c: whitebox.Context) {
def extractCaseClassesParts(classDecl: ClassDef) = classDecl match {
case q"""case class $className(..$fields) extends ..$parents { ..$body }""" =>
(className, fields, parents, body)
case q"""case class $className private(..$fields) extends ..$parents { ..$body }""" =>
(className, fields, parents, body)
case _ => c.abort(c.enclosingPosition, "Unsupported case class type. Cannot rewrite toString().")
}

def extractNewToString(typeName: TypeName, allFields: List[Tree]) = {
Expand Down
16 changes: 15 additions & 1 deletion src/test/scala/ToStringMaskTest.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import java.time.ZonedDateTime
import java.util.Date

import com.softwaremill.macros.customize.{customize, mask}
import org.scalatest.{FlatSpec, Matchers}
Expand All @@ -26,11 +25,26 @@ class ToStringMaskTest extends FlatSpec with Matchers {
// then
u.toString should be("User30(2,***)")
}

it should "work on case classes with private constructors" in {
// given
val u = UserPrivate("[email protected]")

// then
u.toString should be("UserPrivate(1,***)")
}
}

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

@customize
case class UserPrivate private(id: Int, @mask email: String)

object UserPrivate {
def apply(email: String) = new UserPrivate(1, email)
}

object User30 {
def apply(id: Int) = new User30(id, "[email protected]")
}

0 comments on commit da94f11

Please sign in to comment.