5
为了实现ReSTfull API堆栈,我需要将从DB提取的数据转换为JSON格式。我认为最好的方法是从DB中提取数据,然后使用Json.toJson()在定义隐式序列化程序(写入)后作为参数传递给case类,然后将该行设置为JSON。油滑2.0:如何将提升的查询结果转换为案例类?
这里是我的情况下阶层和同伴对象:
package deals.db.interf.slick2
import scala.slick.driver.MySQLDriver.simple._
import play.api.libs.json.Json
case class PartnerInfo(
id: Int,
name: String,
site: String,
largeLogo: String,
smallLogo: String,
publicationSite: String
)
object PartnerInfo {
def toCaseClass(??) = { // what type are the arguments to be passed?
PartnerInfo(fx(??)) // how to transform the input types (slick) to Scala types?
}
// Notice I'm using slick 2.0.0 RC1
class PartnerInfoTable(tag: Tag) extends Table[(Int, String, String, String, String, String)](tag, "PARTNER"){
def id = column[Int]("id")
def name = column[String]("name")
def site = column[String]("site")
def largeLogo = column[String]("large_logo")
def smallLogo = column[String]("small_logo")
def publicationSite = column[String]("publication_site")
def * = (id, name, site, largeLogo, smallLogo, publicationSite)
}
val partnerInfos = TableQuery[PartnerInfoTable]
def qPartnerInfosForPuglisher(publicationSite: String) = {
for (
pi <- partnerInfos if (pi.publicationSite == publicationSite)
) yield toCaseClass(_) // Pass all the table columns to toCaseClass()
}
implicit val partnerInfoWrites = Json.writes[PartnerInfo]
}
我能不明白的是如何实现toCaseClass()方法以改造从油滑的2列类型Scala的类型 - 注意函数FX ()在toCaseClass()的主体中只是为了强调这一点。
我想知道是否可以从Slick列类型中获得Scala类型,因为它明确地在表定义中传递,但我找不到如何获取它。
有什么想法?