【发布时间】:2014-01-12 17:58:32
【问题描述】:
为了实现 ReSTfull API 堆栈,我需要将从 DB 中提取的数据转换为 JSON 格式。我认为最好的方法是从数据库中提取数据,然后使用 Json.toJson() 在定义隐式序列化程序(写入)后将行集作为参数传递给案例类。
这是我的案例类和伴随对象:
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() 方法以便将列类型从 Slick 2 转换为 Scala 类型 - 请注意 toCaseClass() 主体中的函数 fx() 只是为了强调那个。
我想知道是否可以从 Slick 列类型中获取 Scala 类型,因为它在表定义中明确传递,但我找不到如何获取它。
有什么想法吗?
【问题讨论】: