All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scalikejdbc.EntityUtil.scala Maven / Gradle / Ivy

There is a newer version: 3.2.4
Show newest version
package scalikejdbc

import scala.language.experimental.macros
import scala.reflect.macros.blackbox.Context
import scalikejdbc.MacroCompatible._

private[scalikejdbc] object EntityUtil {

  private[scalikejdbc] def constructorParams[A: c.WeakTypeTag](c: Context)(macroName: String, excludes: c.Expr[String]*) = {
    import c.universe._
    val A = weakTypeTag[A].tpe
    val declarations = decls(c)(A)
    val ctor = declarations.collectFirst { case m: MethodSymbol if m.isPrimaryConstructor => m }.getOrElse {
      c.abort(c.enclosingPosition, s"Could not find the primary constructor for $A. type $A must be a class, not trait or type parameter")
    }
    val allParams = paramLists(c)(ctor).head
    val excludeStrs: Set[String] = excludes.map(_.tree).flatMap {
      case q"${ value: String }" => Some(value)
      case m => {
        c.error(c.enclosingPosition, s"You must use String literal values for field names to exclude from #$macroName's targets. $m could not resolve at compile time.")
        None
      }
    }.toSet
    val paramsStrs: Set[String] = allParams.map(_.name.decodedName.toString).toSet
    excludeStrs.foreach { ex =>
      if (!paramsStrs(ex)) c.error(c.enclosingPosition, s"$ex does not found in ${weakTypeTag[A].tpe}")
    }
    allParams.filterNot(f => excludeStrs(f.name.decodedName.toString))
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy