scala - 为什么我得到 No apply function found for scala.Enumeration.Value for enum 字段?
问题描述
我已经定义了我的枚举字段:
object ContractTypeEnum extends Enumeration {
type ContractTypeEnum = Value
val Key1 = Value("key1")
val Key2 = Value("key2")
}
并在 scala 中定义了它的映射Postgres
:
trait EnumImplicit {
implicit val ContractTypeEnumMapper = PostgresDriver.createEnumJdbcType("contract_type", ContractTypeEnum)
}
在我的表的案例类中,我将列定义为:
contractType: Option[ContractTypeEnum.ContractTypeEnum]
并创建Implicit Formatter
如下:
implicit val contractTypeFormat = new Format[ContractTypeEnum.ContractTypeEnum] {
def reads(json: JsValue) = JsSuccess(ContractTypeEnum.withName(json.as[String]))
def writes(myEnum: ContractTypeEnum.ContractTypeEnum) = JsString(myEnum.toString)
}
我得到的是以下错误:
Error:(61, 92) No apply function found for scala.Enumeration.Value
implicit val optionFormat: Format[ContractTypeEnum] = Format.optionWithNull(Json.format[ContractTypeEnum])
并且还编写了以下阅读器/编写器:
object ContractJsonModel {
implicit val ContractJsonModelFormat = {
implicit val optionFormat: Format[ContractTypeEnum] = Format.optionWithNull(Json.format[ContractTypeEnum])
Jsonx.formatCaseClass[ContractJsonModel]
}
}
错误是什么,我应该如何解决?
解决方案
我找到了一个按预期工作的解决方案:
object ContractTypeEnum extends Enumeration {
type ContractTypeEnum = Value
val Key1 = Value("key1")
val Key2 = Value("key2")
implicit val readsMyEnum = Reads.enumNameReads(ContractTypeEnum)
implicit val writesMyEnum = Writes.enumNameWrites
}
推荐阅读
- php - 将 src 组件链接为 https 而不是 http
- google-sheets - 如果 A 列包含某些文本,则根据 B 列中的值查询 + 转置
- c - 如何使用指向一个结构的多个指针,该结构包含指向另一个具有多个指针的结构的指针?
- reactjs - 在 React django 项目中遇到 git 问题
- go - 如何在 Go 中获取监听端口的服务的详细信息
- node.js - 如何使用 exceljs 在现有 excel 工作表的开头添加新行
- python - LightGBMError:不支持功能名称中的特殊 JSON 字符
- python - 使用可自定义的下拉列表对表格进行 Python 网络抓取
- reactjs - 如何向 Material UI Shadows 添加新属性 | 打字稿
- c# - 为什么将 blazor 部署到 IIS 时出现错误