Search code examples
scalaspraysorm

Spray / Sorm gives argument type mismatch


I am working on a Rest API using Scala - Spray - Sorm. I am trying to implement Sorm framework on an existing database. Saving, updating and deleting already works fine, but whenever I try to query something from the database, it is giving me an:

[ERROR] [07/22/2016 09:57:14.377] [on-spray-can-akka.actor.default-
dispatcher-5] [akka://on-spray-can/user/growficient-api] argument type mismatch:
Incorrect values of parameter types:
- long:
| class java.lang.Long:
| 91
- class java.lang.String:
| class org.joda.time.LocalDate:
| 2016-04-08
- class java.lang.String:
| class org.joda.time.LocalTime:
| 12:55:27.000
- int:
| class java.lang.Integer:
| 0
- int:
| class java.lang.Double:
| 2155.0
- int:
| class java.lang.Integer:
| 22
- int:
| class java.lang.Integer:
| 35
- int:
| class java.lang.Integer:
| -65

My model is:

object Samples extends DefaultJsonProtocol with SprayJsonSupport {
  implicit object samplesFormat extends RootJsonFormat[Samples] {
    override def read(value: JsValue) = {
      println(value)
      value.asJsObject.getFields("gatewayid", "sensorid", "date", "time", "wc", "ec", "temp", "battery", "rssi") match {
        case Seq(
          JsString(gatewayid),
          JsString(sensorid),
          JsString(date),
          JsString(time),
          JsNumber(wc),
          JsNumber(ec),
          JsNumber(temp),
          JsNumber(battery),
          JsNumber(rssi)
        ) =>
          new Samples(gatewayid, sensorid, date, time, wc.toInt, ec.toInt, temp.toInt, battery.toInt, rssi.toInt)
        case _ => throw new DeserializationException(s"$value is not properly formatted")
      }
    }

    override def write(s: Samples) = JsObject(
      "gatewayid" -> JsString(s.gatewayid),
      "sensorid" -> JsString(s.sensorid),
      "date" -> JsString(s.date),
      "time" -> JsString(s.time),
      "wc" -> JsNumber(s.wc),
      "ec" -> JsNumber(s.ec),
      "temp" -> JsNumber(s.temp),
      "battery" -> JsNumber(s.battery),
      "rssi" -> JsNumber(s.rssi)
    )
  }
}

case class Samples(gatewayid: String, sensorid: String, date: String, time: String, wc: Int, ec: Int, temp: Int, battery: Int, rssi: Int)

Right now for testing purposes I am just doing a simple query to get everything:

object DB extends Instance(
    entities = Set(
      Entity[Samples]()
    ),
    url = s"jdbc:mysql://$addr:$port/$database",
    user = username,
    password = password,
    initMode = InitMode.Create
  )

  DB.query[Samples].fetch().toList

Unfortunately it crashes at the query with given error output. I understand that something is getting the wrong types of parameters but I can't figure out what.

I would really appreciate if anyone could point me into the right direction.


Solution

  • The culprit is there:

    - class java.lang.String:
    | class org.joda.time.LocalDate:
    | 2016-04-08
    - class java.lang.String:
    | class org.joda.time.LocalTime:
    | 12:55:27.000
    

    You should use the suggested Joda types instead of String on the according fields.