ただ楽しみのために。楽しむことがポイントだからですよね?
可変フィールドの使い方を忘れてしまったので、不変の犬を作るのが好きです。
偽装者は、PartialFunctionをリファクタリングすることでAnyValを拡張できます(匿名のネストされたクラスはAnyValで許可されていないため、「この制限は今後のリリースで削除される予定です。」)
package populate
import scala.reflect._
import scala.reflect.runtime.{ currentMirror => cm }
import scala.reflect.runtime.universe._
import java.util.{ Calendar, Date }
case class Person(var name: String, var birth: Date) { def this() = this(null,null) }
case class Book(var title: String, var pub: Date) { def this() = this(null,null) }
case class Dog(val name: String, val rabies: Date, val goodBoy: Boolean = true)
object Test extends App {
def dateOf(y:Int,m:Int,d:Int) = { val c = Calendar.getInstance; c.set(1974, 2, 14); c.getTime }
val input = Seq("id" -> "Sybil", "n" -> dateOf(1974, 2, 14), "garbage" -> "error")
trait Completer[A] extends Any {
def filler: PartialFunction[A, Unit]
def complete(info: Seq[A]) = info foreach (filler orElse {
case (k, v) => println(s"Can't use $k -> $v")
})
}
type Info = (String, Any)
implicit class Impersonator(val p: Person) extends Completer[Info] {
override def filler = {
case ("id", s: String) => p.name = s
case ("n", d: Date) => p.birth = d
}
}
implicit class Booker(val b: Book) extends Completer[Info] {
override def filler = {
case ("id", s: String) => b.title = s
case ("n", d: Date) => b.pub = d
}
}
def personify(p: Person, info: Seq[(String, Any)]) = info foreach {
case ("id", s: String) => p.name = s
case ("n", d: Date) => p.birth = d
case (k, v) => println(s"Can't use $k -> $v")
}
def bookish(b: Book, info: Seq[(String, Any)]) = info foreach {
case ("id", s: String) => b.title = s
case ("n", d: Date) => b.pub = d
case (k, v) => println(s"Can't use $k -> $v")
}
def inject[A: ClassTag](a: A, info: Seq[(String, Any)]): A = {
implicitly[ClassTag[A]] match {
case ClassTag(k) if k == classOf[Person] => personify(a.asInstanceOf[Person], info)
//case ClassTag(k) if k == classOf[Book] => bookish(classOf[Book] cast a, info)
case ClassTag(k) if k == classOf[Book] => a.asInstanceOf[Book] complete info
case k => println(s"Unknown $k")
}
a
}
def entity[A: ClassTag](info: Seq[(String, Any)]): A = {
val e = implicitly[ClassTag[A]].runtimeClass.newInstance.asInstanceOf[A]
inject(e, info)
}
val v = entity[Person](input)
println(v)
Console println entity[Book](input)
Console println Defaulter((input map {
case ("id", v) => ("name", v)
case ("n", v) => ("rabies", v)
case p => p
}).toMap).newCase[Dog]
}
case class Defaulter(input: Map[String, Any]) {
def newCase[A]()(implicit t: ClassTag[A]): A = {
val claas = cm classSymbol t.runtimeClass
val modul = claas.companionSymbol.asModule
val im = cm reflect (cm reflectModule modul).instance
defaut[A](im, "apply")
}
def defaut[A](im: InstanceMirror, name: String): A = {
val at = newTermName(name)
val ts = im.symbol.typeSignature
val method = (ts member at).asMethod
// either defarg or default val for type of p
def valueFor(p: Symbol, i: Int): Any = {
val defarg = ts member newTermName(s"$name$$default$$${i+1}")
if (input contains p.name.toString) {
input(p.name.toString)
} else if (defarg != NoSymbol) {
println(s"default $defarg")
(im reflectMethod defarg.asMethod)()
} else {
println(s"def val for $p")
p.typeSignature match {
case t if t == typeOf[String] => null
case t if t == typeOf[Int] => 0
case t if t == typeOf[Date] => new Date(0L)
case x => throw new IllegalArgumentException(x.toString)
}
}
}
val args = (for (ps <- method.paramss; p <- ps) yield p).zipWithIndex map (p => valueFor(p._1,p._2))
(im reflectMethod method)(args: _*).asInstanceOf[A]
}
}