stringmask icon indicating copy to clipboard operation
stringmask copied to clipboard

Proposal: library revision

Open vitaliihonta opened this issue 3 years ago • 0 comments

Hello @mkubala @kciesielski ! I already e-mailed Adam about my question and decided to discuss it directly with you.

stringmask is pretty useful in such a field as fintech (for masking data not only in logs, but even in API responses), and I used this library for a while.

But the caveat is macro annotations because it's unclear if Scala 3 will support them.

So I developed a library sharing the same purpose but with a different approach, based on typeclasses. I wonder if you're interested in adopting it as a part of stringmask? I could open a PR and do all the work.

Here is the source code. It looks pretty the same as chimney, having simple builders for describing masking logic. For instance:

import sensitive._
import scala.util.matching.Regex

case class CardData(number: String, expMonth: Int, expYear: Int, cvv: String)

object CardData {
  implicit val sensitiveCardData: Sensitive[CardData] = sensitiveOf[CardData]
    .withFieldMasked(_.number)(
      regexp(
        "([0-9]{4})-([0-9]{4})-([0-9]{4})-([0-9]{4})".r,
        replaceAll(replacer = {
          case Regex.Groups(first, second, _, fourth) =>
            s"$first-${second.take(2)}**-****-$fourth"
        })
      )
    )
    .withFieldMasked(_.cvv)(substitute("***"))
    .build

}

The only thing that should be improved is Scala 3 macros for the builder DSL. I already did some experiments and it seems to be possible to implement it in Scala 3

vitaliihonta avatar Jun 20 '22 12:06 vitaliihonta