我有一个简单的案例类金额如下
case class Amount(value: Long, currency: Currency)
以及将字符串货币代码转换为货币对象的伴随对象
object Amount {
private val log = Logger(getClass)
def apply(value: Long, currencyCode: String) : Amount = {
try {
Amount(value, Currency.getInstance(currencyCode))
} catch {
case e: Exception =>
log.error(s"Invalid currency code [$currencyCode]")
throw new Exception(s"Invalid currency code [$currencyCode]")
}
}
}
调用:
val amount : Amount = Amount(1234, "USD")
当我从数据库中读取一些数据时,我有一个自定义解析器,例如
implicit val amountParser = Macro.parser[Amount]("value", "currencyCode")
但是,编译器抱怨
scala.ScalaReflectionException: value apply encapsulates multiple overloaded alternatives and cannot be treated as a method. Consider invoking `<offending symbol>.asTerm.alternatives` and manually picking the required method
[error] at scala.reflect.api.Symbols$SymbolApi$class.asMethod(Symbols.scala:228)
[error] at scala.reflect.internal.Symbols$SymbolContextApiImpl.asMethod(Symbols.scala:84)
[error] at anorm.Macro$.parserImpl(Macro.scala:70)
[error] at anorm.Macro$.namedParserImpl_(Macro.scala:25)
[error] implicit val amountParser = Macro.parser[Amount]("value", "currencyCode")
我如何使这项工作?
更新
了解从@MikeAllen的响应后,我决定离开case class Amount
和object Amount
为是,而不是我写了一个自定义的解析器金额如下
implicit private val amountParser = for {
value <- long("value")
currencyCode <- str("currency_code")
} yield {
Amount(value, currencyCode)
}
在斯卡拉编译器会自动生成一个Amount.apply
用于创建工厂方法case class
的情况下,这就是为什么你得到这个错误-因为你有多种Amount.apply
方法。其中一个采用 ( Long
, Currency
) 类型的参数,另一个采用 ( Long
, String
)类型的参数。该错误消息表明您需要从通过反射报告的重载替代方案中选择其中之一。
或者,您的案例类和同伴可能会重新设计如下:
final case class Amount(value: Long, currencyCode: String) {
/** Currency. Will create an exception on instantiation if code is invalid. */
val currency: Currency = {
try {
Currency.getInstance(currencyCode)
}
catch {
case e: Exception =>
Amount.log.error(s"Invalid currency code [$currencyCode]")
throw new Exception(s"Invalid currency code [$currencyCode]")
}
}
}
object Amount {
private val log = Logger(getClass)
}
诚然,这并不优雅,因为您现在有一个字段,currency
,它不是 case 类的参数之一,并且不可用于模式匹配,同时还带有字符串形式。
更好的解决方案是在创建实例之前保留原始数据case class
并将货币代码字段从 aString
转换为 a ,作为解析器的一部分:Currency
Amount
val amountMapping = {
get[Long]("value") ~ get[String]("currencyCode") map {
case value ~ currencyCode => {
val currency = {
try {
Currency.getInstance(currencyCode)
}
catch {
case e: Exception =>
Amount.log.error(s"Invalid currency code [$currencyCode]")
throw new Exception(s"Invalid currency code [$currencyCode]")
}
}
Amount(value, currency)
}
}
}
然后您可以使用它来解析行,例如:
def amounts(): List[Amount] = SQL("select * from amounts").as(amountMapping *)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句