Skip to content

Instantly share code, notes, and snippets.

@petitviolet
Created April 3, 2018 04:26
Show Gist options
  • Save petitviolet/ab5529cc65452c22de597786e2c79e53 to your computer and use it in GitHub Desktop.
Save petitviolet/ab5529cc65452c22de597786e2c79e53 to your computer and use it in GitHub Desktop.
case class MyObj()
def insert(c: MyObj, excludes: String*)(implicit s: DBSession): Id[MyObj] = {
import scala.reflect.runtime.{ universe => ru }
val clazz = c.getClass
def caseMap[T: ru.TypeTag: reflect.ClassTag](instance: T): Seq[(SQLSyntax, Any)] = {
val im = ru.runtimeMirror(clazz.getClassLoader).reflect(instance)
ru.typeOf[T]
.members
.filterNot { sym =>
excludes.contains(sym.name.toString)
}
.collect {
// case m: ru.MethodSymbol if m.isCaseAccessor =>
// abstract type pattern reflect.runtime.universe.MethodSymbol is unchecked since it is eliminated by erasure
case m if m.isMethod && m.asMethod.isCaseAccessor =>
val name = m.name.toString
val value: Any = im.reflectMethod(m.asMethod).apply()
// implicit val p = ParameterBinderFactory.asisParameterBinderFactory
// cannot find ParameterBinderFactory instance properly...
column.selectDynamic(name) -> value
}(collection.breakOut)
}
val namedValues = caseMap[MyObj](c)
MyObj.createWithNamedValues(namedValues: _*)(s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment