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

Commit bac47cf

Browse files
authored
Merge pull request #3 from mkubala/feature/support-more-than-22-fields
Added support for classes that have more than 22 fields.
2 parents 7afd791 + c817029 commit bac47cf

3 files changed

Lines changed: 52 additions & 5 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ class CustomizeImpl(val c: whitebox.Context) {
3838
case _ => c.abort(c.enclosingPosition, s"Cannot call .toString on field.")
3939
}
4040
}
41-
val treesAsTuple = Apply(Select(Ident(TermName("scala")), TermName("Tuple" + fieldListTree.length)), fieldListTree)
41+
val treesAsString = fieldListTree.reduceLeftOption { (commaSeparatedFieldsString, fieldString) =>
42+
q"$commaSeparatedFieldsString + ',' + $fieldString"
43+
}
4244
val typeNameStrTree = Literal(Constant(typeName.toString))
4345
q"""
4446
override def toString = {
45-
$typeNameStrTree + $treesAsTuple
47+
$typeNameStrTree + '(' + ${treesAsString.getOrElse(Literal(Constant("")))} + ')'
4648
}
4749
"""
4850
}
@@ -71,4 +73,4 @@ class CustomizeImpl(val c: whitebox.Context) {
7173
}
7274

7375
}
74-
}
76+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.softwaremill.macros.customize
2+
3+
import org.scalatest.{FlatSpec, Matchers}
4+
5+
class ToStringMaskOnBigClassesTest extends FlatSpec with Matchers {
6+
7+
behavior of "masking"
8+
9+
it should "work on classes with > 22 fields" in {
10+
// given
11+
val bc = BigClass(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ,14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25)
12+
13+
// then
14+
bc.toString should be("BigClass(***,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)")
15+
}
16+
17+
@customize
18+
case class BigClass(@mask a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int,
19+
a11: Int, a12: Int, a13: Int, a14: Int,a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int,
20+
a21: Int, a22: Int, a23: Int, a24: Int, a25: Int)
21+
22+
}

src/test/scala/ToStringMaskTest.scala renamed to src/test/scala/com/softwaremill/macros/customize/ToStringMaskTest.scala

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
package com.softwaremill.macros.customize
2+
13
import java.time.ZonedDateTime
24

3-
import com.softwaremill.macros.customize.{customize, mask}
45
import org.scalatest.{FlatSpec, Matchers}
56

67
class ToStringMaskTest extends FlatSpec with Matchers {
@@ -42,6 +43,22 @@ class ToStringMaskTest extends FlatSpec with Matchers {
4243
u.toString should be("User40(3,***,null)")
4344
}
4445

46+
it should "work on classes without any fields" in {
47+
// given
48+
val ec = EmptyClass()
49+
50+
// then
51+
ec.toString should be("EmptyClass()")
52+
}
53+
54+
it should "work on classes with 1 field" in {
55+
// given
56+
val sc = SmallClass(100)
57+
58+
// then
59+
sc.toString should be("SmallClass(***)")
60+
}
61+
4562
}
4663

4764
@customize
@@ -53,10 +70,16 @@ case class User40(id: Int, @mask email: String, something: String)
5370
@customize
5471
case class UserPrivate private(id: Int, @mask email: String)
5572

73+
@customize
74+
case class EmptyClass()
75+
76+
@customize
77+
case class SmallClass(@mask n: Int)
78+
5679
object UserPrivate {
5780
def apply(email: String) = new UserPrivate(1, email)
5881
}
5982

6083
object User30 {
6184
def apply(id: Int) = new User30(id, "default@email.com")
62-
}
85+
}

0 commit comments

Comments
 (0)