Scala Macros: Making a Map out of fields of a class in Scala

Note that this can be done much more elegantly without the toString / c.parse business: import scala.language.experimental.macros abstract class Model { def toMap[T]: Map[String, Any] = macro Macros.toMap_impl[T] } object Macros { import scala.reflect.macros.Context def toMap_impl[T: c.WeakTypeTag](c: Context) = { import c.universe._ val mapApply = Select(reify(Map).tree, newTermName(“apply”)) val pairs = weakTypeOf[T].declarations.collect { case m: MethodSymbol … Read more

Scala case class update value

Define the case class so that the second member is a var: case class Stuff(name: String, var value: Option[String]) Now you can create an instance of Stuff and modify the second value: val s = Stuff(“bashan”, None) s.value = Some(“hello”) However, making case classes mutable is probably not a good idea. You should prefer working … Read more

Scala: Parse JSON directly into a case class

There are several frameworks which can exactly do that. circe Used a lot nowdays. Many great features. Will pull cats in. https://circe.github.io/circe/ https://github.com/circe/circe JSON4s JSON4s is quite mature and supports jackson or a native JSON-Parser. Used it in many projects to replace jerkson. https://github.com/json4s/json4s play-json Can be used without the full play stack. Great support … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)