Building up on the solution provided by 0__
, I wrote this approach that works with clases that use only a few primitive types:
object Main {
def toSourcePrimitive(l: Long): String =
l.toString + "L"
def toSourcePrimitive(i: Int): String =
i.toString
def toSourcePrimitive(s: String): String =
"\"" + s.replace("\"", "\\\"") + "\""
def toSourcePrimitive[T](li: List[T]): String =
"List(" + li.map(toSource).mkString(", ") + ")"
def toSourcePrimitive[T](op: Option[T]): String =
if (op.isEmpty) "None" else "Some(" + toSource(op.get) + ")"
def toSourcePrimitive(p: Product): String = {
p.productIterator.map(toSource).mkString(p.productPrefix + "(", ", ", ")")
}
def toSource(a: Any): String = a match {
case in: Int => toSourcePrimitive(in)
case ln: Long => toSourcePrimitive(ln)
case s: String => toSourcePrimitive(s)
case li: List[_] => toSourcePrimitive(li)
case op: Option[_] => toSourcePrimitive(op)
case p: Product => toSourcePrimitive(p)
case _ => "ERROR"
}
case class Example1(date: Int, numLong: Long, numInt: Int)
case class Example2(v1: String, v2: List[String], v3: List[Example1], v4: Option[String])
def main(args: Array[String]): Unit = {
println("\nExample 1:")
println(toSource(Example1(20220105, 6L, 10)))
println("\nExample 2:")
val elems = List(
Example2("blah", List("one", "two"), List(Example1(20220105, 6L, 10)), Some("three")),
Example2("blah", Nil, List(Example1(20220105, 6L, 10)), None)
)
println(toSource(elems))
}
}
The output is shown below:
Example 1:
Example1(20220105, 6L, 10)
Example 2:
List(Example2("blah", List("one", "two"), List(Example1(20220105, 6L, 10)), Some("three")), Example2("blah", List(), List(Example1(20220105, 6L, 10)), None))