Created
April 3, 2018 04:26
-
-
Save petitviolet/ab5529cc65452c22de597786e2c79e53 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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