I am looking at a piece of Scala macro which provides an implicit implementation of a class. This class converts a map of field values to a case class. The macro can be found here and this is the explanation behind it.
Currently the implementation ignores redundant field provided in the input map. I want to add a method similar to fromMap
which would throw an exception if the input map has redundant entries but I am not sure if I understand it well enough.
My understanding is that toMapParams
and fromMapParams
are expressions that take the inputs and apply either Map
or the Companion object's Apply method on them.
As such, fromMapParams
should be modified to output the redundant values as a list of string in an exception of the form:
case class CaseClassRedundantFieldException[T]
(redundantFields: List[String], cause: Throwable = None.orNull)
(implicit c: ClassTag[T])
extends Exception(s"Conversion between map of data-fields to ${c.runtimeClass.asInstanceOf[T]} failed"
+ "as redundant fields were provided: $redundantFields",
cause)
I think I need to simply have something like:
def fromMap(map: Map[String, Any]): $tpe = {
val redundant vals: fields diff $map
if(vals.size > 0){ CaseClassRedundantFieldException(vals) } //(these two lines don't have the right syntax.)
$companion(..$fromMapParams )
}
How can I do this?