Skip to content
This repository was archived by the owner on Apr 27, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ class CustomizeImpl(val c: whitebox.Context) {
case _ => c.abort(c.enclosingPosition, s"Cannot call .toString on field.")
}
}
val treesAsTuple = Apply(Select(Ident(TermName("scala")), TermName("Tuple" + fieldListTree.length)), fieldListTree)
val treesAsString = fieldListTree.reduceLeftOption { (commaSeparatedFieldsString, fieldString) =>
q"$commaSeparatedFieldsString + ',' + $fieldString"
}
val typeNameStrTree = Literal(Constant(typeName.toString))
q"""
override def toString = {
$typeNameStrTree + $treesAsTuple
$typeNameStrTree + '(' + ${treesAsString.getOrElse(Literal(Constant("")))} + ')'
}
"""
}
Expand Down Expand Up @@ -71,4 +73,4 @@ class CustomizeImpl(val c: whitebox.Context) {
}

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.softwaremill.macros.customize

import org.scalatest.{FlatSpec, Matchers}

class ToStringMaskOnBigClassesTest extends FlatSpec with Matchers {

behavior of "masking"

it should "work on classes with > 22 fields" in {
// given
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)

// then
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)")
}

@customize
case class BigClass(@mask a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int,
a11: Int, a12: Int, a13: Int, a14: Int,a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int,
a21: Int, a22: Int, a23: Int, a24: Int, a25: Int)

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.softwaremill.macros.customize

import java.time.ZonedDateTime

import com.softwaremill.macros.customize.{customize, mask}
import org.scalatest.{FlatSpec, Matchers}

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

it should "work on classes without any fields" in {
// given
val ec = EmptyClass()

// then
ec.toString should be("EmptyClass()")
}

it should "work on classes with 1 field" in {
// given
val sc = SmallClass(100)

// then
sc.toString should be("SmallClass(***)")
}

}

@customize
Expand All @@ -53,10 +70,16 @@ case class User40(id: Int, @mask email: String, something: String)
@customize
case class UserPrivate private(id: Int, @mask email: String)

@customize
case class EmptyClass()

@customize
case class SmallClass(@mask n: Int)

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

object User30 {
def apply(id: Int) = new User30(id, "default@email.com")
}
}